Archived

This topic is now archived and is closed to further replies.

jeffB

Physics & framerates

Recommended Posts

I've been using a fixed frame rate for my game but I now want to change this so that all the physics are calculated from the time elapsed between each update. I'm just a little confused as to what values to change. At the moment I have a spaceship that is updatedlike this...
  
#define MASS        1
#define MAXVELOCITY 10
#define MAXTHRUST   5

Spaceship::Update()
{
	accel += current_thrust / MASS;

	velocity += accel;

	velocity.truncate(MAXVELOCITY);

	pos += velocity;
}
 
I have now changed this to:
     
Spaceship::Update(float time_elapsed)
{
	accel += current_thrust / MASS;

	velocity += accel * time_elapsed;

	velocity.truncate(MAXVELOCITY);

	pos += velocity * time_elapsed;
}
 
Which I think is correct, but I'm now unsure how to adjust the MAXVELOCITY, MAXTHRUST values so that my ship behaves the same as it did with a constant frame rate. Can anyone help? [edited by - jeffB on April 28, 2003 6:23:29 AM]

Share this post


Link to post
Share on other sites
Edit: oops silly random key jabbing

Spaceship::Update()
{
accel += (current_thrust / MASS) * time_elapsed;
velocity += accel * time_elapsed;
velocity.truncate(MAXVELOCITY);
pos += velocity * time_elapsed;
}


In this you multiply values by current time when you want to factor in their current effects. But you want the results of all of your calculations to be time independent. So for instance never store velocity as it was last frame, you should always store it on a per second basis.

[edited by - gleno on April 28, 2003 6:35:27 AM]

Share this post


Link to post
Share on other sites