# 2d car with simple suspension physics

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

## Recommended Posts

Hi.

My first post here

I have recently started writing a simple game engine from scratch, mainly for the learning experience. The most difficult I've had yet is with the physics, and I feel I need some help from more experienced people.

The physics in question is for a side-scrolling 2d car game. The collisions seems to work out pretty good, but now I'm trying to implement suspension on the wheels of the car which has proven to be difficult. The suspension is very simple. On each wheel (of the two) there is a spring-damper-mass with one degree of freedom in the direction as seen as the red arrow in the illustration.

Take a look at the illustration. Without the suspension, things are pretty simple because we just apply the impulse normal to the collision surface, and get some new linear and angular velocities.

But with the suspension, I imagine some of this impulse is just added as spring/wheel velocity. And here I'm getting confused on how this changes the impulse on the car body. Here is what I currently do when a collision is detected:

1. Calculate the normal impulse as if the suspension was not there (unelastic collision)
2. Set new spring velocity such that it is the same as the velocity in the collision point (in the direction of it's freedom)
3. Subtract some of the spring's added velocity from the normal impulse
4. Apply normal impulse (from 1 & 3) to car body
5. Update spring velocity and position, add reverse directed impulse on the car body (done every frame regardless of collision)

This does work to a certain degree, but has some shortcomings, and it's not all theoretically founded.

If somebody could explain to me how the collision in my illustration will behave I will be eternally happy And I'm sure there is a more efficient way than how I resolve collisions. Any ideas and suggestions in this regard is also very welcome.

##### Share on other sites
I would suggest modeling the three objects (car body, two wheels) as their own rigid body objects.

Your collision response should keep the wheel out of the ground.
This will result in the spring length becoming shorter. This applies a force on your car body. Sum all the forces on your car body, and integrate. The details should work themselves out.

##### Share on other sites
I agree to this. Simulate car body and wheels as individual rigid bodies and connect them with springs or constraints. I've actually made something very similar to this using only damped springs, and it worked very well.

Cheers,
Mike

##### Share on other sites
That was a very good tip, my code is definitely cleaner and more intuitive now.

I still have one weird behaviour though. The spring force acting on the car body creates a torque as expected, but because there is no opposite directed force, this force creates a linear acceleration as well. This makes the car accelerate to the right or left if it has a rotation and only one wheel is connected to the ground.

The suspension feels perfect if I just cheat and add another force such that I only get a net torque and zero net force, and add a small force normal to the ground such that it counteracts the fall.

But I would like to understand what I'm doing wrong. Thanks again.

##### Share on other sites

The spring force acting on the car body creates a torque as expected, but because there is no opposite directed force, this force creates a linear acceleration as well.

In order to preserve Newtons third law you always need to apply two opposite equal forces or impulses (except if one of the involved objects is "immovable", eg. the ground). Almost all problems I've ever had in physics sims comes from "breaking" this law. Once you get this part right, your problem goes away and loads of fun and puzzling physical phenomena appears out of nowhere. In this case, simply add the opposite spring force to the wheel center, and you should be ok. As a rule of thumb you don't need to apply non-physical tweaks or hacks in order to make your sims feel and look right as long as you've got the physics right in the first place.

Edit: I've released the source for my vehicle experment for inspiration.

Cheers,
Mike

##### Share on other sites

[quote name='Cachinnus' timestamp='1311072121' post='4837308']
The spring force acting on the car body creates a torque as expected, but because there is no opposite directed force, this force creates a linear acceleration as well.

In order to preserve Newtons third law you always need to apply two opposite equal forces or impulses (except if one of the involved objects is "immovable", eg. the ground). Almost all problems I've ever had in physics sims comes from "breaking" this law. Once you get this part right, your problem goes away and loads of fun and puzzling physical phenomena appears out of nowhere. In this case, simply add the opposite spring force to the wheel center, and you should be ok. As a rule of thumb you don't need to apply non-physical tweaks or hacks in order to make your sims feel and look right as long as you've got the physics right in the first place.

Edit: I've released the source for my vehicle experment for inspiration.

Cheers,
Mike
[/quote]

That is imprecise at best. There's no "always", in fact, the force-counterforce equivalence is NOT truth most of the time. Only when dealing with static problems, but nothing in this world, including this particular problem is static. There are forces/torques and masses/rotational-inertias. What about falling objects for example?

##### Share on other sites

That is imprecise at best. There's no "always", in fact, the force-counterforce equivalence is NOT truth most of the time. Only when dealing with static problems, but nothing in this world, including this particular problem is static. There are forces/torques and masses/rotational-inertias. What about falling objects for example?

Well, if you don't believe so, that's entirely a problem between you and Newton. Where did you read that his third law only applies on static problems? It works equivalently for torque / moment of inertia as it does for force / mass. A falling object excerts exactly as much force on the Earth as vice versa, but, as I mentioned above, in a game-related context you can disregard this rule if one of the two objects is much larger than the other and in an every-day sense is thought of as beeing immovable, such as buildings, mountains or the ground as a whole. Within the field of classical mechanics, can you give me just one example where Newton's third law is not valid?

(Edit: Fleshed out the post a bit for clarity.)

Cheers,
Mike

##### Share on other sites
I think there's a misunderstanding, I'm not sure what. The 3rd law (as far as I see) is truth for systems, but not for a single object. The thing you posted implies that the sum forces on every objects are zero, which is not true. For the complete system, the sum will be zero but in games, you are dealing with objects one-by one, caclulate all forces and torques which produce linear and angular acceleration. Forces can come from collisions (like spring forces), from a field (gravity), from dampers (friction or drag), and so on. There are cases when you don't even care about the object that's acting as the counter-pair: for example you don't care about the gas coming from the engine, all you care about is the force you have to apply to the engine.

"Breaking the law" in this sense is nonsense... To be honest, I saw people too hung up with the principle of the 3rd law (I remember a thread about similar stuff but in 3D), and that actually broke the physics, the "solution" was a nonsense hack.
Maybe we are talking about the same thing and we both understand those laws just fine, but I see pretty much no use of the 3rd law (more precisely no explicit use) in games except for some cases. Sorry, my English is quite poor, I hope my post made sense.

##### Share on other sites
Ah, now I see what the trouble is all about. When you said that Newtons laws mostly did not apply, I assumed you ment the real world when in fact you were talking about games. So I suppose it turns out we pretty much agree anyway :-) I do have to emphathize that often strange, apparently inexplainable bugs start to appear if you do not take great care in these matters. I'm nearly 100% ceartain this is also the case with Cachinnus' vehicle sim. If an object hits the ground, then only one force is needed, but every time two objects that move in-game interact with each other, you must apply opposite equal forces, otherwis things screw up.

Cheers,
Mike

##### Share on other sites
Thanks for the help. You are probably right that I'm not obeying Newton's third law.

So if I understand you correct, I should not add a collision force/impulse to keep the car from the ground. Rather, I should just compress the spring. Next, this will result in the two masses connected to the spring each getting a force directed from each other, which should be equal in magnitude.

Hmm, or perhaps not? The spring can store potential energy (like gravity) so it doesn't completely obey newton's third law. At least on some level of abstraction, if I understand it correct.

Some questions:

• The wheel will get the most acceleration because it is the lightest of the two bodies, so it will probably collide again. I suspect this will result in jittery motion. Maybe I should do the same calculations several times each frame to reduce this?
• With the springs having only one freedom of movement, the spring length will in some cases have to become infinitely long to get out of the ground. That is when the car is colliding with a surface perpendicular to the spring axis. What is the best way to deal with this? I am thinking in the lines of having an angular spring as well.
• When compressing the spring, this will add energy to it. And since this energy is not based on the mechanical energy of the car, isn't there a danger for unstable and somewhat random motions? Proposed solution: Take the desired absorbed energy of the car, and use the spring constant to calculate the distance based on this energy. Artificially move rest of the car out of the collision area.
Bonus: This will also avoid the problem with the infinite spring length. What do you think about this solution?
Thanks for that demo h4tt3n, I had fun with it I will take a deeper look at that code.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631429
• Total Posts
3000033
×