/**
* Returns Slope Pitch and Roll angles in degrees based on the following information:
*
* @param MyRightYAxis Right (Y) direction unit vector of Actor standing on Slope.
* @param FloorNormal Floor Normal (unit) vector.
* @param UpVector UpVector of reference frame.
* @outparam OutSlopePitchDegreeAngle Slope Pitch angle (degrees)
* @outparam OutSlopeRollDegreeAngle Slope Roll angle (degrees)
*/
void UKismetMathLibrary::GetSlopeDegreeAngles(const FVector& MyRightYAxis, const FVector& FloorNormal, const FVector& UpVector, float& OutSlopePitchDegreeAngle, float& OutSlopeRollDegreeAngle)
{
const FVector FloorZAxis = FloorNormal;
const FVector FloorXAxis = MyRightYAxis ^ FloorZAxis;
const FVector FloorYAxis = FloorZAxis ^ FloorXAxis;
OutSlopePitchDegreeAngle = 90.f - FMath::RadiansToDegrees(FMath::Acos(FloorXAxis | UpVector));
OutSlopeRollDegreeAngle = 90.f - FMath::RadiansToDegrees(FMath::Acos(FloorYAxis | UpVector));
}
GetSlopeDegreeAngles:求获取物体在坡面或地面上的角度朝向。
AXXXVehicle : public AWheeledVehicle
class USkeletalMeshComponent* Mesh;//载具mesh
//BalanceTick在tick函数调用,FloorNormal参数:物体所在的垂直于坡面或地面法线(可以通过LineTraceSingleForObjects等射线检测函数获取得到)
void AXXXVehicle::BalanceTick(const FVector& FloorNormal,float DeltaSeconds)
{
float fVehiclePitch = 0.f;
float fVehicleRoll = 0.f;
FVector VehicleAngularVelocity = FVector::ZeroVector;
FVector VehicleUpVector = Mesh->GetUpVector();
FVector VehicleRightVector = Mesh->GetRightVector();
UKismetMathLibrary::GetSlopeDegreeAngles(VehicleRightVector , FloorNormal,
VehicleUpVector , fVehiclePitch , fVehicleRoll );//求出当前载具在坡面或地面上角度
//判断载具大于一定角度 比如30度,开始固定载具角度,就是载具平衡在一个合适角度
if (FMath::Abs(fVehiclePitch ) > 30|| FMath::Abs(fVehicleRoll ) > 30)
{
VehicleAngularVelocity .X = FMath::ClampAngle(fVehicleRoll , -30, 30);
VehicleAngularVelocity .Y = -FMath::ClampAngle(fVehiclePitch , -30, 30);
//角速度转到载具模型空间下 然后调用SetPhysicsAngularVelocityInDegrees
VehicleAngularVelocity = VehicleMesh>GetComponentRotation().RotateVector(VehicleAngularVelocity );
Mesh->SetPhysicsAngularVelocityInDegrees(VehicleAngularVelocity , true);
}
}