# physics simulation timing help?

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

## Recommended Posts

Okay I have this little problem with my physics simulation... if i run this on a fast computer the guys cant move ... but on a slower one they move just fine... time is founf by... 1 / (float)fps ... I tired using "elapsed time" for time but that caused jumpy movement any help would be much appreciated. here is my simualtion code...

time = 1 / (float)fps;

void CActor::simulate(float time)
{
m_VLastPosition		= m_VPosition;
m_VLinearVelocity       += (m_VTotalForce / m_mass) * time;
m_VPosition		+= m_VLinearVelocity * time;

//MAKE SURE FRICTION DOES NOT CAUSE MOVEMENT *X*
if(fabs(m_VLinearVelocity.m_x) >= fabs((m_VFrictionForce.m_x / m_mass) * time))
{
m_VLinearVelocity.m_x += (m_VFrictionForce.m_x / m_mass) * time;
}
else
{
m_VLinearVelocity.m_x = 0;
}

//MAKE SURE FRICTION DOES NOT CAUSE MEVEMENT *Y*
if(fabs(m_VLinearVelocity.m_y) >= fabs((m_VFrictionForce.m_y / m_mass) * time))
{
m_VLinearVelocity.m_y += (m_VFrictionForce.m_y / m_mass) * time;
}
else
{
m_VLinearVelocity.m_y = 0;
}

//DAMPING
if(fabs(m_VLinearVelocity.m_x) < .5f)
{
m_VLinearVelocity.m_x = 0;
}

if(fabs(m_VLinearVelocity.m_y) < .5f)
{
m_VLinearVelocity.m_y = 0;
}

//reset
m_totalTorque		= 0;
m_totalImpulse		= 0;
m_VTotalForce		= CVector(0,0);
m_VFrictionForce	= CVector(0,0);

}


any ideas on how to make it run the same at all speeds... I know the problem may be in the DAMPING... but i have to get the ACTORS to stop some how?? [Edited by - MTclip on August 13, 2005 1:05:25 AM]

##### Share on other sites
The reason you have different results with different frame rates is this code:
    m_VLastPosition     = m_VPosition;    m_VLinearVelocity  += (m_VTotalForce / m_mass) * time;    m_VPosition	       += m_VLinearVelocity * time;
Though your computation might seem correct because it follows the equations v = v0 + at and p = p0 + vt, the equations assume that a and v are constant, and v is not constant. The result is that your code is only approximately correct and the frame rate affects the amount of error. There are a couple things you can do to fix the problem. One would be to switch to a better approximation (look up verlet or Runga-Kutta integration). Another way is to simply use equations that give the correct results:

v = v0 + at
p = p0 + v0t + at2/2

Using these, your code would look like this:
    m_VLastPosition         = m_VPosition;    m_VLastLinearVelocity   = m_VLinearVelocity;    m_VLinearVelocity      += (m_VTotalForce / m_mass) * time;    m_VPosition	           += m_VLastLinearVelocity * time + (m_VTotalForce / m_mass) * time * time * 0.5f;
Now, assuming that the acceleration due to friction is constant, you need handle friction in your code in the same way. If friction depends on velocity, then there are still equations that give the correct results, but they are complicated and expensive (they involve ekt), and you might instead want to go with a better method of approximation.

##### Share on other sites
ok first of thanks for the help...

second.. I have still not gotten the timing correct...
with what you wote are you suggesting that i take out my whole simulation code and use what you provided.. simply replace the top portion of my code..

for my step i use 1/FPS ..but this is calculated and averaged only once per second.. thus allowing inacturate step... is that a problem...
should i instead switch to an elapsedtime on a perframe basis...

thanks agian

##### Share on other sites
Try replacing the top part and the friction part. I would also remove the damping until things work right.

Calculating 1/fps once per second might be ok. Generally, people use the duration of the frame directly. Sometimes, people use a moving average to smooth out any spikes in the frame rate.

##### Share on other sites
okay.. well replacing the friction part certainly helped alot.. though its not perfect (but what is)

one final question(hopefuly)
I have projectiles that recieve a large amount of force on a single frame wich causes error when there is a spike in delta Time...
so

where this = the projector
m_pEquipedItem = projectile

CVector2D dist((this->m_pEquipedItem->m_CollisionPoint) - (this->m_CollisionPoint));
CVector2D tempVelocity(this->m_VLinearVelocity);
tempVelocity.normalize();
dist += tempVelocity;
dist.m_x *= 30000;
dist.m_y *= 30000;
this->m_pEquipedItem->applyLinearForce(dist);

thats not the full source.. but basically it gets the angle the projector is facing and shoots/throws the projectile in that direction...

the 30000 is just some number that i found that works (for the most part)
idealy it will be based off the projectors strength..

so is there a way to go about correcting the error from frame spikes...
perhaps a better solution would be to limit the frame rate that way i can find a delta time from the constant frame rate ..

or maybe only do simulations at certain delta times ....

thanks for the help.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 10
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015832
×