- Home /
How can I stabilize a vehicle's roll during a jump using forces?
I'm making a driving simulation that needs to be as close as possible to physically correct while avoiding extreme slides and barrel rolls. I'm using Edy's vehicle physics for this.
I'm pretty happy with where it's at right now but I'm hitting a snag when going over jumps diagonally: the vehicle starts rolling around the z axis (as it should, physically speaking). I need to stabilize its roll while allowing its yaw move freely along the jump
I've tried correcting it using rotation, but it ends up being jerky because the car keeps rolling one way and then correcting.
Here's what I had:
function Update () {
//determine whether or not we're grounded
var Hit : WheelHit;
var grounded=false;
for (var i=0; i<4; i++){
var touch = wheels[i].getWheelCollider().GetGroundHit(Hit);
if (touch) grounded = true;
}
var currentUp : Vector3 = transform.up;
var gravityUp : Vector3 = stageGrav.GetNormal();
var angle = AngleSigned(currentUp, gravityUp, Vector3.Cross(currentUp, gravityUp));
//debug
Debug.DrawLine(transform.position, transform.position+currentUp*3);
Debug.DrawLine(transform.position, transform.position+gravityUp*5);
Debug.DrawLine(transform.position, transform.position+transform.forward*5);
if (grounded) return; //dont stabilize when grounded
//we're not grounded on any wheel, let's stabilize roll:
Debug.Log(transform.localEulerAngles);
var angles : Vector3 = transform.localEulerAngles;
//determine how far we are from pointing up relative to z and correct it
var drift : Vector3 = new Vector3(0,0,angle*Time.deltaTime);
var newAngles :Vector3 = angles + drift;
newAngles.z = newAngles.z % 360;
transform.localEulerAngles = newAngles;
}
function AngleSigned(v1:Vector3, v2:Vector3, n:Vector3):float {
return Mathf.Atan2(
Vector3.Dot(n, Vector3.Cross(v1, v2)),
Vector3.Dot(v1, v2)) * Mathf.Rad2Deg;
}
I'm thinking I need to cancel out the rolling force rather than messing with the vehicle's rotation... does anyone know how I might achieve that?
Answer by Benoit Dufresne · May 31, 2013 at 10:37 PM
I've thought about torque mid-question and didn't think stopping and googling that instead. Turns out there was a very helpful thread around here.
Your answer
Follow this Question
Related Questions
Calculating Sphere vs. Plane Friction 1 Answer
WheelCollider slowing vehicle down at 0 torque 0 Answers
Physics object slides while turning. 1 Answer
How does unity add torque 1 Answer
Understanding how torque is applied 2 Answers