- Home /
Attempting to add wheel friction to a 2d car
I am trying to create wheel friction in a 2d space. I am trying to use the equation f=m^2/r. This equation is used to calculate the forces acting on a vehicle perpendicular to its vector of travel. Currently the code isn't working correctly. It is hard to explain but basically it adds force bi natter what direction it goes. But I need it to apply the force in the relative x direction. Again it is hard to explain but I would like some advice on what to do. I will post all the code:
var power : float = 3;
var maxspeed : float = 5;
var turnpower : float = 2;
var mass : float;
var moving : boolean = false;
var turnright : boolean = false;
var turnleft : boolean = false;
var rightforce : float;
var leftforce : float;
var friction : float = 10;
var curspeed : Vector2;
var curspeedtrue : float;
function Update ()
{
mass = rigidbody2D.mass;
rightforce = -(rigidbody2D.velocity.x);
leftforce = rigidbody2D.velocity.x;
curspeed = Vector2(rigidbody2D.velocity.x, rigidbody2D.velocity.y);
if(curspeed.magnitude > maxspeed)
{
curspeed = curspeed.normalized;
curspeed *= maxspeed;
}
curspeedtrue = rigidbody2D.velocity.x + rigidbody2D.velocity.y;
if(moving == true)
{
rigidbody2D.AddForce(Vector3.right * mass * (curspeed.magnitude * curspeed.magnitude) / turnpower);
//rigidbody2D.AddForce(-(Vector2.right) * mass * (curspeed.magnitude*curspeed.magnitude));
}
if(curspeed.magnitude <= 0)
moving = false;
else
moving = true;
if(Input.GetKey(KeyCode.W))
{
rigidbody2D.AddForce(transform.up * power);
}
if(Input.GetKey(KeyCode.S))
{
rigidbody2D.AddForce(-(transform.up) * power);
}
if(moving == true)
{
if(Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * turnpower);
turnleft = true;
}
if(Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.forward * -turnpower);
turnright = true;
}
}
if(Input.GetKey(KeyCode.A) !=true)
turnleft = false;
if(Input.GetKey(KeyCode.D) !=true)
turnright = false;
}
Im confused, shouldn't wheel friction be applied perpendicular to the direction the wheel is traveling in?
Answer by Jeff-Kesselman · May 06, 2014 at 01:48 AM
Ah, okay, well if you are applying the force perpendicular to the wheel then what yo want to do is find the perpendicular vector and then multiply it by the force.
Every transform has three vectors you can get automatically that represent the three axes: right (x), up (y) forward (z). If you modled your tire in the typical fashion 'facing' +z then transform.right of the tire object will be the perpendicular vector.
Now, the question is, do you multiply it by your force scalar, or your negative force scalar. This depends on the direction of the force being applied. This depends on your direction of travel. Assuming again the car was molded with its nose facing +z, In a left hand turn, the force is going to be applied in the right (positive) direction. In a right hand turn the force will applied in a left (negative) direction.
I understand everything up to finding the correct force to apply in the perpendicular direction. This was the theory I have been working with.
Ah, well its your formula. If its not right or if you don't know what it means, then Id have to do research to see what I came up with in terms of a formula. As you presented it above Im not even sure what all the terms mean.
well I can tell you now that m=mass, v=velocity and r=radius of the turn. so at the moment, this is just a big circle.
Your answer
Follow this Question
Related Questions
2d rigidbody drag on x, y axis 0 Answers
Please help. I can't stop this light from moving!!! 1 Answer
Why is my GameObject rotation snapping to a certain angle when he reaches its destination? 2 Answers
Relation between two 3D vectors that are in the same plane, to translate to 2D 4 Answers
How can I get x, y, z spawnpoints from a csv or text file? 1 Answer