UE4简单处理载具平衡(摩托车、 四轮、坦克等)防止翻车等现象

本文介绍了一种在游戏开发中计算物体在坡面或地面上的倾斜角度的方法,使用了Unreal Engine的Kismet Math Library。通过物体的右向量、地板法线和向上向量,可以获取物体的俯仰角和滚动角,从而实现物体在不同地形上的平衡调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


	/**
	* 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);
	}


}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值