Simulating vehicle body

Official forum for the Chipmunk2D Physics Library.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simulating vehicle body

Post by slembcke »

Yeah, you don't want to be setting the velocity explicitly each frame. Making small adjustments is ok, but making large adjustments effectively kills the ability of the physics to deal with collisions.

That car physics tutorial I mentioned above talks about how to calculate the friction forces you need. Citrys's solution kinda fakes them, and is much simpler to implement. Try that first. I've always surmised that they do a similar thing for the friction of the jeep in Halo 1 due to how nice and slidey it is, though I've never tried it.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
joe
Posts: 10
Joined: Sun Dec 02, 2007 3:27 pm
Contact:

Re: Simulating vehicle body

Post by joe »

Hi people,

first off: thanks for you replies. The problem currently is not so much as to get the vehicle to drift (it already does if I want it to), but to get the new velocity vector into the system without touching the velocity vector (instead using forces or impulses). But your approach in "faking", citrys, is a lot cleaner than my current one (I basically let the rotation "lag" behind in some cases). I've read through the paper on car physics and implemented some (not all) of it: the tangential function, for example - which I in my case replaced by a sigmoid function. It gives the car a nice feeling of oversteering at a special point (e.g. when a combination of too high speed and slippery underground combines - there it is).

But as I said - I solved that. Now the real problem is changing the rotation and speed of the vehicle without touching the velocity vector. So I thought of the forces which would need to be applied.

I've made an illustration (yeah, I'm kind of an artist): http://bildrian.de/n/b/3cad8c19c6a250ae.png

Top is accelerating, Bottom is decelerating/reverse. Left to right is steer left, drive forward, steer right. So the arrows I drew in there should be the points where the (impulse?) force should apply to the body (leaving the driting thing completely out here for now). The problem is that I obviosly don't have a clue when to use impulses and when to use forces. I also totally don't get whe the offset vector "r" should be in world coordinates - sure the doc doesn't mean body coordinates here? When I set "r" to zero I get the results I'd expect when setting the *body* coordinates to zero.

Anyways, I just can't get it done. When I use forces the vehicle continues to accelerate infinitely or rotating like crazy. Resetting the forces every time step every time can't be the solution, or can it? When I use impulses the vehicle first turns but then bounces back like a spring.

The forces thing is so frustrating. I can't even get a freaking car simulated. Currently I'd just like to print out all the code I wrote and make some huge bonfire. God, guess that is currently because I'm really suffering from cerebral shortcircuiting after 8 hours of college classes, maybe I should just go to bed and have a look at it sometime else.

I really appreciate all the help you're giving me here. Please be patient with me, I'd be so proud if I got it running and you could see those little vehicles (yeah the sprites are ugly, but, hey) driving around :-) It's all in my head, I can clearly see it ;-)

Greetings,
Johannes
citrys
Posts: 16
Joined: Thu Nov 15, 2007 12:26 am
Contact:

Re: Simulating vehicle body

Post by citrys »

joe wrote:I also totally don't get whe the offset vector "r" should be in world coordinates - sure the doc doesn't mean body coordinates here? When I set "r" to zero I get the results I'd expect when setting the *body* coordinates to zero.
I think I had this question, too, and it was answered in another thread.
joe wrote:Anyways, I just can't get it done. When I use forces the vehicle continues to accelerate infinitely or rotating like crazy. Resetting the forces every time step every time can't be the solution, or can it?
Yeah, actually, that seems to be the way to do it. Forces are cumulative; if you don't reset them every step, any new forces you apply will compound with the old ones.

If your vehicles have a speed limit, you might also want to cap their bodies' velocities in some way. I make a bounds check on my velocity and decide whether or not to apply an acceleration force that step. I don't remember why I got away from directly capping the velocity at each step except that it may not have looked as natural.
joe wrote:The forces thing is so frustrating. I can't even get a freaking car simulated. Currently I'd just like to print out all the code I wrote and make some huge bonfire. God, guess that is currently because I'm really suffering from cerebral shortcircuiting after 8 hours of college classes, maybe I should just go to bed and have a look at it sometime else.
Keep plugging and keep thinking about what's going on. Dump some values to the console as the thing runs. You'll figure it out.

Simulating a car's pretty hard work. I prefer spaceships. =)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simulating vehicle body

Post by slembcke »

First of all, simulating cars realistically is quite difficult. Even simplified models get rather complicated. Don't get too discouraged yet. Like I said, Chipmunk doesn't really have any code to help you make top down driven vehicles yet, so you are kinda doing all the work yourself. Don't give up too easily. This sort of stuff is hard to visualize and even harder to debug.

When to use impulses and when to use forces? Use forces for things that are small and constant. Impulses are sort of like forces applied without regard to time. (impulse = force*time) Think of a gun, the recoil is caused by a force that is applied over so short of a time that it is practically irrelevant. For all practical purposes of a simulation, the gun's momentum simply changes instantly. Did that help?

As for how to apply the forces, you are correct that the docs are not clear. Even worse, I was wrong in the thread mentioned by citrys <_<. (Re-read it now) What you want to do is something like this:

Code: Select all

cpVect r = cpvrotate(position_on_body, the_body->rot);
cpBodyApplyForce(the_body, force, r);
Depending on if you are calculating the force in body local coordinates, you will want to rotate that too.

When to reset forces? Chipmunk does not zero the forces applied to a body during the cpSpaceStep() call. If you are calculating and applying a force to your vehicle every frame without calling cpBodyResetForces() (which just sets the force and torque to 0), you are accumulating those forces. This can quickly cause things to explode out of control.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
joe
Posts: 10
Joined: Sun Dec 02, 2007 3:27 pm
Contact:

Re: Simulating vehicle body

Post by joe »

Hello again,

okay, I've now set up a pretty cool car simulation which works by only using implied forces (drag, axis, centripedal force) thanks to your help. So I'm not manipulating the velocity vector explicitly, but let the engine do everything via ApplyForce calls. Problem is: the tank still freaks out when colliding. This is a video of the tank colliding with a static object (invisible line shape, infinite mass/inertia): http://www.file-upload.net/download-546 ... e.mpg.html

What can I do about that weird behavior?

Greetings,
Johannes
joe
Posts: 10
Joined: Sun Dec 02, 2007 3:27 pm
Contact:

Re: Simulating vehicle body

Post by joe »

Sorry for my previous posting, I've fixed the problem now. It was with the inertia of the body which needs to be calculated from the mass and shape of the object. I currently just kind of made them up.

If inertia and mass/shape are interlinked in such a way - why seperate them in the first place? Or to paraphrase that: when is it necessary to have an object of high mass an low inertia (or vice versa)?

For those who like pretty videos, here's one: http://www.file-upload.net/download-546 ... n.mpg.html

I'm so proud! Wouldn't have ever worked without you guys, thank you so much. I sure hope the game is finished at some point in time, it's going to be cool :-)

Greetings,
Johannes
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simulating vehicle body

Post by slembcke »

Well, the mass doesn't really have any thing to do with the shape of the object. The moment of inertia on the other hand has to do with both the mass and the shape. To be even more specific, the moment of inertia has to do with the distribution of the mass about the center of gravity of the object. The distribution of mass isn't exactly linked to the shape.

For instance, imagine two spheres of the same size and mass. The first is something hollow like a ping pong ball, while the second is solid. Despite having the same size and shape, the moment of inertia will be lower on the solid ball. This is because some of the mass is very close to the center of gravity (having little effect on the moment of inertia) and some is far away. In the hollow ball, all of the mass is at the same distance from the center of gravity. If you were to roll both balls down a ramp, the solid ball would get to the bottom first due to it's lower moment of inertia providing less resistance to angular acceleration.

So to answer your question, you could have an object with a large mass and low moment of inertia if most of it's mass was concentrated near the center of gravity. Likewise for an object with a low mass and large moment of inertia if most of the mass is concentrated far away from the center of gravity.

Wikipedia knows all: http://en.wikipedia.org/wiki/Moment_of_inertia
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests