Box2D & Top Down Car Physics

This topic is 3176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, I am currently developing a top down car game. I originally developed my own car class which handled movement and handling and it worked really well. However I started to run into problems with the collision reaction. I was using colour maps to detect my collisions, which did work, however when working out how to react to these collisions I had hit a brick wall. Therefore I took a few step backs and decided to utilise Box2D for my collision detection and reaction and this works really well. I now need to figure out how to incorporate my handling into this. A player will be able to choose a different number of cars whcih will in turn have there own unique handling abilities. I implemented a car body which was made up of two boxes, one for the front and one for the rear So I could handle basic weight distribution. The car body also has 4 wheels attached, 2 revolutew joints at the front wheels and two prismatic joints for the rear wheels. http://www.kokodev.co.uk/demolitionderby/ As you can see from the above example the car moves ok but as soon as you hold down either the left or right arrow key, the car begins to spin on the spot almost. Does anybody have any suggestions or know of any examples that I could follow? Essentially I would like the car to behave like the following: http://vimeo.com/8723304 I like the idea of being able to create a traction value that will in turn mange how well the car handles (Slip/Grip). One thing I have tried is the following:
var wheel:b2Body = _wheelBody;
var localPoint = new b2Vec2(0, 0);
var velocityVector:b2Vec2 = wheel.GetLinearVelocity();
var travelVector = Trig.findHypotenuseLength(velocityVector.x, velocityVector.y);
var newVec = Trig.findNewXY(0, 0, wheel.GetAngle(), travelVector);
var newVec2:b2Vec2 = new b2Vec2(newVec.x, newVec.y);
wheel.SetLinearVelocity(newVec2);


As you can see I am getting the speed of travel from the current linear velocity and then applying this velocity in the direction the wheels are facing using some basic Trig in a class I created. This doesn't affect the travelling body at all and doesn't seem to do much. Does anybody have anyt sugegstions on how I could tackle this so Box2D will handle the car correctly? Any help is greatly appreciated. Cheers Stu

Share on other sites
Hi guys,

What I am going to try and do is the following:

Calculate the lateral force (The force acting on the side of the wheel)
Then apply an opposing force to compensate for this lateral force

Does anybody know how to calculate the lateral force. The problem I seem to be having is all the forces calculated are in a world position and I need to calculate these force locally to each wheel.

I can clearly see the lateral axis in red and this is where I need to calculate the forces along.

I have updated the file online so you can see the movement forces in yellow lines. This is where each wheel is heading towards on the world map. I need to use this force and then convert it to local forces on each wheel so I can calculate my opposing force.

Cheers
Stu

Share on other sites
Google "Slip Angle"...but it depends how realistic you want to make it.

Share on other sites
I figured it out in the end, the trick was to find the lateral and linear forces acting on the wheel and then multiply the lateral force based on a sideways traction value (Between 0 & 1). This will give you a new lateral force.

When you have figured out the new lateral force, using some basic pythag you can figure out a new linear force so the total velocity of the cars motion stays the same.

Once you have your new linear and lateral force you can calculate the new linear velocity using some simple trig.

The hardest part was Box2D stores velocity vectors in world co-ordinates, so you have to convert these vectors to local vectors of each wheel so you can calculate accurate angles. This is required to accuratly calculate the lateral forces. Also the new linear force is always a positive value so you need to know when the car is moving backwards or forwards. This can be calculated from the actual linear force vector. If the y is negative then you know the cars is moving bacwards. If you know the car is moving backwards you simply multiply the new linear velocity by -1!

I applied the traction on each wheel individually, the beauty about this is that car acts as it should and allows you to implement handbrakes etc.

Cheers
Stu

Share on other sites

I think (but could be wrong) but the lateral force may also be affected by the weight shifting which applies more or less weight to each wheel. For example in a tight left hand turn the weight shifts to the right hand side so that there is less traction on the left hand side wheels and more on the right hand side.

Share on other sites
That is a good point and this could be applied to the wheels in question, I will look at this accordingly.

I have built the car with two boxes to allow for front/rear weight distribution. The car also has the ability to be eather fronbt wheel, rear wheel or four wheel drive which changes the dynamics also.

The turn weight distrubution will be something I could look at the make it feel more realistic, thanks for the thought.

Cheers
Stu

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• Forum Statistics

• Total Topics
633269
• Total Posts
3011153
• Who's Online (See full list)

There are no registered users currently online

×