## Basic questions about rigid body physics

Member
Posts: 153
Joined: 2004.12
Post: #1
Im trying to create a really simple rigid body physics system.

I have a good grasp on acceleration and velocity and how they relate to each other. But im getting hung up on acceleration and 'force'.

I understand the basic concept of both, i just dont get how im supposed to implement them.

Acceleration:
Acceleration is the measure of velocity/time right? So naturally my time stepping code would look something like:

new velocity = old velocity + (acceleration * deltaTime);

What i dont get is what happens to acceleration. Does acceleration continue to accelerate every frame until an 'anti' acceleration is applied? Should i be dampening acceleration each time it gets added on to velocity? What I posted continues to accelerate my object right up into infinite. Shouldn't acceleration eventually somehow stop?

Force:
How do i add forces? force = m*a; But if i have a force and i want to apply it to an object how do i change the objects values based on the force. Do i simply add the force vector to the acceleration vector?... Add the force vector to the velocity vector?

Thanks in advance. Iv been reading about this stuff but most articles get caught up in fancy integration methods and gloss over this basic stuff.

There was a long silence...
'I claim them all,' said the Savage at last.
Moderator
Posts: 3,584
Joined: 2003.06
Post: #2
Before somebody gives you a REAL answer to this question, which is a good one, I'd like to point out that the highest order law of physics in >>>game programming<<< is this: Smoke and Mirrors is more real than E == MC^2.

We, as quasi-simulationists (is that a word?!), prize real physics. But this is games. If it don't work, chuck the book and make do with what you have! x = x + a * dt is just fine if that makes sense to you. x = x + 3 * myFramrate * someFunkyMadeUpConstantWhichSeemsToDoTheTrick is equally valid if it really does what you want.

Quote:Shouldn't acceleration eventually somehow stop?
In the computer world YES! In the real world it does too. E = MC^2 eventually prohibits acceleration because mass approaches infinity as velocity reaches the speed of light, but I'll stop there on that one. In the computer world, floating point math can only handle so much in the way of extremes. Always cap velocity/acceleration/mass/whatever physics you might encounter. "anti-accelerating" as you say is just fine to do if it makes a difference to the feel of the game. Most of the time just not accelerating past a certain point is good enough, but again, do what works best.
Member
Posts: 153
Joined: 2004.12
Post: #3
Always good to keep in mind, thanks for the tip

Let me rephrase my question: Im god. I have a big bouncy ball i like to play with in space. I put the ball down in the middle of space at <0,0,0> with no velocity and no acceleration. I push the ball forward. I assume that the act of "pushing" applies some type of acceleration to the ball. How does that acceleration dissipate after i stop pushing? Does it immediately stop the SECOND the ball leaves my touch? or does the acceleration continue until something stops it from accelerating?... Like if i run in front of the ball and push it the other direction.

There was a long silence...
'I claim them all,' said the Savage at last.
Moderator
Posts: 133
Joined: 2008.05
Post: #4
When you push a ball, you're applying a force which gives the ball acceleration. When you stop pushing the ball, there is no acceleration on it, so acceleration is 0.

In real physics, there's also friction which is an ever present force in the direction opposite of the force. That's the only way your ball will stop.

Here's a breakdown of what happens.

First you start out by pushing the ball... while you're pushing the ball, you're giving the ball acceleration(which in the real world is never constant, because you can't possibly always apply a constant force, but in a game that's fine).
Time = 0
Force = 1 made up unit
Acceleration = 1 m/s^2
Velocity = 0 (because we just now applied the force at time = 0)

Time = 1
Force = 1
Accel = 1 m/s^2 (because you're still pushing it)
Velocity = 1 m/s (because it's had an acceleration of 1 for a second)

Time = 2
Force = 1
Accel = 1
Velocity = 2 (because the force is still applied, the ball is still gaining speed, and already had speed from before)

Time = 3
Force = 0 ( no more pushing)
Accel = 0
Velocity = 2

Time = 4
Force = 0
Accel = 0
Velocity = 2

In order to have the ball stop to a slowdown, you'd have to have some kind of friction variable. Depending on how fast you want it to slow down, you'd set the acceleration to 1/some number * the opposite of the acceleration it just had. Then you'd check to see if the velocity is going to turn in the other direction, and when it does cut off all the acceleration and velocity(set to 0).
Member
Posts: 153
Joined: 2004.12
Post: #5
That break down was exactly what i needed to see, thanks a ton.

Its funny how perplexingly simple that is... Why so many tutorials feel the need to bring calculus into this...

There was a long silence...
'I claim them all,' said the Savage at last.
Moderator
Posts: 133
Joined: 2008.05
Post: #6
Well, that is calculus, but without all the math.

Position's derivative is velocity, velocity's derivative is acceleration.
Acceleration's integral is velocity, velocity's integral is position.

Real basic calculus, but can be solved algebraically.
Member
Posts: 153
Joined: 2004.12
Post: #7
Yeah i appreciate that, people who know calculus would probably rather read that than algebra. I just wish someone had written a tutorial for us none calculus savvy people.

Now to figure out angular velocity

There was a long silence...
'I claim them all,' said the Savage at last.
Moderator
Posts: 441
Joined: 2002.09
Post: #8
hangt5 Wrote:Im god. I have a big bouncy ball i like to play with in space.

I'm seriously considering making this my .sig.

Measure twice, cut once, curse three or four times.
Member
Posts: 153
Joined: 2004.12
Post: #9
Heh, go for it

Ok so i ran into a wall.

My stepping code looks like this:
Code:
```//update position based on velocity LCVector *scaledVelocity = [velocity times:dt]; [position add:scaledVelocity];      //update velocity based on acceleration LCVector *scaledAcceleration = [acceleration times:dt]; [velocity add:scaledAcceleration]; //update acceleration acceleration = [force slash:mass];      //reset force [force set:0:0:0];          [self updateMomentum];```

My problem is with acceleration.

Heres a scenario:
Velocity <0,0,0>
Position <0,0,0>
Acceleration <0,0,0>
Force <0,0,0>

I add a force to my object:
Velocity <0,0,0>
Position <0,0,0>
Acceleration <0,0,0>
Force <0,1,0>

I step the object: //acceleration just now becomes <0,1,0>
Velocity <0,0,0>
Position <0,0,0>
Acceleration <0,1,0>
Force <0,0,0>

I step it again say with a dt of 0.5 // Based on the DT and acceleration o f <0,1,0> velocity becomes <0,0.5,0>
Velocity <0,0.5,0>
Position <0,0,0>
Acceleration <0,0,0> //Notice how acceleration gets reset now
Force <0,0,0>

I step again with a dt of 0.1
Velocity <0,0.5,0>
Position <0,0.1,0>
Acceleration <0,0,0>
Force <0,0,0>

I step again with a dt of 0.2
Velocity <0,0.5,0>
Position <0,0.1+(0.5*0.2),0>
Acceleration <0,0,0>
Force <0,0,0>

Obviously thats not how its supposed to go. So my question is, how do i know when to reset acceleration? Every frame is obviously just not right... Do i need to have an anti acceleration accumulator? for example

velocity += acceleration * dt;
accelerationAcumulator += acceleration*dt;
if(accelerationAcumulator == acceleration)
{
acceleration = <0,0,0>
accelerationAcumulator = <0,0,0>
}

Edit: Just so you know, every frame i loop through my physics objects and pass each object the "step" message with the deltaTime as an argument. Im not using any fancy integrating method or anything.

There was a long silence...
'I claim them all,' said the Savage at last.
Member
Posts: 338
Joined: 2004.07
Post: #10
Actually, that seems right. What it looks like you're doing is only applying the force for one frame. Notice in LongJumper's breakdown, when the force is removed, so is the acceleration.

Try taking out the line of code that resets the force and then observing what happens.

Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Member
Posts: 153
Joined: 2004.12
Post: #11
If i dont reset the force, my acceleration (via the force) keeps on increasing my velocity. I was under the impression my velocity should stay constant if i only apply one force.

Edit: Heres a copy and paste of the first couple of frames of output.

---Last DT:0.000000----
Physics Object - M:1.000000
Position: <0.000000,0.000000,0.000000>
Velocity: <0.000000,0.000000,0.000000>
Acelerat: <0.000000,0.000000,0.000000>
Momentum: <0.000000,0.000000,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.049 LCEngine[3850]

---Last DT:0.000026----
Physics Object - M:1.000000
Position: <0.000000,0.000000,0.000000>
Velocity: <0.000000,0.000000,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.000000,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.050 LCEngine[3850]

---Last DT:0.006350----
Physics Object - M:1.000000
Position: <0.000000,0.000000,0.000000>
Velocity: <0.000000,0.006350,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.006350,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.051 LCEngine[3850]

---Last DT:0.000933----
Physics Object - M:1.000000
Position: <0.000000,0.000006,0.000000>
Velocity: <0.000000,0.007283,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.007283,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.052 LCEngine[3850]

---Last DT:0.000798----
Physics Object - M:1.000000
Position: <0.000000,0.000012,0.000000>
Velocity: <0.000000,0.008081,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.008081,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.053 LCEngine[3850]

---Last DT:0.001050----
Physics Object - M:1.000000
Position: <0.000000,0.000020,0.000000>
Velocity: <0.000000,0.009131,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.009131,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.054 LCEngine[3850]

---Last DT:0.000741----
Physics Object - M:1.000000
Position: <0.000000,0.000027,0.000000>
Velocity: <0.000000,0.009872,0.000000>
Acelerat: <0.000000,1.000000,0.000000>
Momentum: <0.000000,0.009872,0.000000>
Force: <0.000000,1.000000,0.000000>
2005-07-06 22:31:06.054 LCEngine[3850]

My velocity would increase to infinite.

There was a long silence...
'I claim them all,' said the Savage at last.
Member
Posts: 184
Joined: 2004.07
Post: #12
hangt5 Wrote:I step again with a dt of 0.1
Velocity <0,0.5,0>
Position <0,0.1,0>
Acceleration <0,0,0>
Force <0,0,0>

I step again with a dt of 0.2
Velocity <0,0.5,0>
Position <0,0.1+(0.5*0.2),0>
Acceleration <0,0,0>
Force <0,0,0>

Obviously thats not how its supposed to go.

Why not? It's perfectly valid to work with an engine where acceleration is only applied in a single frame. For example, in something like ODE if you have two rigid objects that are colliding, you know there is some penetration depth, and you can apply a force proportional to that depth for each frame that the two objects are colliding. Thus, when the objects are eventually pushed apart, there is no more force on either, which is the right behavior. If the acceleration applied is not high enough you can change how much of it is applied to the velocity- for example, you are using vel = vel * acc * time, though you could throw in a constant to make vel = vel * acc * time * constant if the amount of velocity added per force unit is not high enough.
Member
Posts: 153
Joined: 2004.12
Post: #13
phydeaux Wrote:Why not? It's perfectly valid to work with an engine where acceleration is only applied in a single frame. For example, in something like ODE if you have two rigid objects that are colliding, you know there is some penetration depth, and you can apply a force proportional to that depth for each frame that the two objects are colliding. Thus, when the objects are eventually pushed apart, there is no more force on either, which is the right behavior. If the acceleration applied is not high enough you can change how much of it is applied to the velocity- for example, you are using vel = vel * acc * time, though you could throw in a constant to make vel = vel * acc * time * constant if the amount of velocity added per force unit is not high enough.

Interesting, so its valid for me to wipe my forces clean at the end of every frame?

and is it supposed to be: vel = vel * acc * time, or it supposed to be vel = vel + acc*time?

There was a long silence...
'I claim them all,' said the Savage at last.
Member
Posts: 184
Joined: 2004.07
Post: #14
hangt5 Wrote:Interesting, so its valid for me to wipe my forces clean at the end of every frame?

and is it supposed to be: vel = vel * acc * time, or it supposed to be vel = vel + acc*time?

Er, whoops, you're right... vel += acc*time*k. But yes, the point is that you can have a force last just one frame. In fancier integration methods you might smooth that out a little bit, but to get something that works properly you don't need to do anything that complicated.
Moderator
Posts: 869
Joined: 2003.01
Post: #15
You dont need to smooth force whatsoever. Forces can be and are a very sudden thing in nature, think of your head hitting a wall. However, unpleasant things happen when velocity is not smooth, hence you integrate it from acceleration/force.

For that reason, acceleration is not one of the state variables you keep from frame to frame, but only temporary for each integration step. Also, force and acceleration are the same thing (you just need to mult/div by the mass), don't keep it in separate variables. What I usually do, is to keep a force accumulator variable for each particle, which is then converted to acceleration and subsequently cleared for the integration phase.