瀏覽代碼

Motorcycle: Fixed bug in lean angle calculation (#608)

Clamp target lean angle against max lean angle
Wheel base is now also overridden if the suspension force point is set
Jorrit Rouwe 2 年之前
父節點
當前提交
e48543ea67
共有 1 個文件被更改,包括 12 次插入3 次删除
  1. 12 3
      Jolt/Physics/Vehicle/MotorcycleController.cpp

+ 12 - 3
Jolt/Physics/Vehicle/MotorcycleController.cpp

@@ -75,8 +75,10 @@ float MotorcycleController::GetWheelBase() const
 	{
 		const WheelSettings *s = w->GetSettings();
 
-		// Measure distance along the forward axis by looking at the fully extended suspension
-		float value = (s->mPosition + s->mSuspensionDirection * s->mSuspensionMaxLength).Dot(mConstraint.GetLocalForward());
+		// Measure distance along the forward axis by looking at the fully extended suspension.
+		// If the suspension force point is active, use that instead.
+		Vec3 force_point = s->mEnableSuspensionForcePoint? s->mSuspensionForcePoint : s->mPosition + s->mSuspensionDirection * s->mSuspensionMaxLength;
+		float value = force_point.Dot(mConstraint.GetLocalForward());
 
 		// Update min and max
 		low = min(low, value);
@@ -110,9 +112,16 @@ void MotorcycleController::PreCollide(float inDeltaTime, PhysicsSystem &inPhysic
 		mTargetLean = mLeanSmoothingFactor * mTargetLean + (1.0f - mLeanSmoothingFactor) * target_lean;
 
 		// Remove forward component, we can only lean sideways
-		mTargetLean -= mTargetLean * mTargetLean.Dot(forward);
+		mTargetLean -= forward * mTargetLean.Dot(forward);
 		mTargetLean = mTargetLean.NormalizedOr(world_up);
 
+		// Clamp the target lean against the max lean angle
+		Vec3 adjusted_world_up = world_up - forward * world_up.Dot(forward);
+		adjusted_world_up = adjusted_world_up.NormalizedOr(world_up);
+		float w_angle = -Sign(mTargetLean.Cross(adjusted_world_up).Dot(forward)) * ACos(mTargetLean.Dot(adjusted_world_up));
+		if (abs(w_angle) > mMaxLeanAngle)
+			mTargetLean = Quat::sRotation(forward, Sign(w_angle) * mMaxLeanAngle) * adjusted_world_up;
+			
 		// Integrate the delta angle
 		Vec3 up = body->GetRotation() * mConstraint.GetLocalUp();
 		float d_angle = -Sign(mTargetLean.Cross(up).Dot(forward)) * ACos(mTargetLean.Dot(up));