• Create Account

Banner advertising on our site currently available from just \$5!

# 2d car with simple suspension physics

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Cachinnus  Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 July 2011 - 05:32 PM

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:

• Calculate the normal impulse as if the suspension was not there (unelastic collision)
• Set new spring velocity such that it is the same as the velocity in the collision point (in the direction of it's freedom)
• Subtract some of the spring's added velocity from the normal impulse
• Apply normal impulse (from 1 & 3) to car body
• 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.

### #2CombatWombat  Members   -  Reputation: 530

Like
0Likes
Like

Posted 18 July 2011 - 06:14 PM

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.

### #3h4tt3n  Members   -  Reputation: 1512

Like
0Likes
Like

Posted 19 July 2011 - 12:14 AM

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

### #4Cachinnus  Members   -  Reputation: 100

Like
0Likes
Like

Posted 19 July 2011 - 04:42 AM

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.

### #5h4tt3n  Members   -  Reputation: 1512

Like
0Likes
Like

Posted 19 July 2011 - 05:33 AM

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

### #6szecs  Members   -  Reputation: 2338

Like
0Likes
Like

Posted 19 July 2011 - 09:25 AM

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

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?

### #7h4tt3n  Members   -  Reputation: 1512

Like
0Likes
Like

Posted 19 July 2011 - 10:35 AM

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

### #8szecs  Members   -  Reputation: 2338

Like
0Likes
Like

Posted 19 July 2011 - 11:01 AM

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.

### #9h4tt3n  Members   -  Reputation: 1512

Like
0Likes
Like

Posted 19 July 2011 - 12:34 PM

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

### #10Cachinnus  Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 July 2011 - 07:25 PM

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.

### #11CombatWombat  Members   -  Reputation: 530

Like
1Likes
Like

Posted 22 July 2011 - 10:22 AM

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.

Strictly speaking "you" don't do anything explicitly in your code to compress the spring. The motion of the bodies should do this automagically.

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.

You should be worrying about "the equal and opposite" on a per joint basis.
IE: Wheel pushes on spring with 10N. Spring pushes on wheel with 10N.
Will you get 10N out the other side of the spring? Probably, but not always. What happens if you want to model your spring as having some mass?

You might want to look into the basics of drawing "free-body-diagrams". This might help you understand how forces interact between components.

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?

This is true of about any spring simulation. Doubly true where you have a maintained collision (resting contact). I would suggest using RK4 integration with a suitably small timestep, and using a damper (shock absorber to us 'mericans) along with your spring. Maybe someone can come along and make suggestions on how to make the collision response more robust.

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.

Springs don't work perpendicularly. You need another constraint to handle this. IE: Model a linkage that actually holds your wheel to the car, and then let the spring just worry about springynes. So when your wheel runs into this perpendicular surface, then load goes into this new constraint (and then into the body of the car, probably stopping it abruptly). The spring likely never deflects.

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.

You put energy into your car to move it in the first place. Or it had some, as a byproduct of elevation. The spring is just storing some of this temporarily, and will give it back (minus any absorbed by the dampers) to the car body when it extends to original length.

In my opinion you are trying to over think this a bit. Model your components so that they behave individually. Now model constraints to glue them together. The details should sus themselves out.

### #12Cachinnus  Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 July 2011 - 11:31 PM