Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

air resistance over time

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there, I'm trying to add correct air resistance into moving objects, currently I'm just multiplying the velocity by some number between 0-1 every frame so that it reduces, which is fine but when the framerate is low the objects take longer to slow down:


void Object::update(unsigned dTimeMs)
{
   this->velocity += this->acceleration * (dTimeMs/1000.0f);

   this->velocity *= 0.8;

   this->position += this->velocity * (dTimeMs/1000.0f);
}


I'm not looking for accurate air resistance but I need a way to scale the velocity towards zero at a fixed rate, so that the rate will remain the same when I call this function at different framerates. Any Ideas?

Share this post


Link to post
Share on other sites
Advertisement
I had a similar problem for rotation. varying frame rate was the cause.


I came up with a simple super duper elegant solution. :D


Calculate the frame rate, calculate the multiplying constant wrt the frame rate.

Hope that helps.


There is always a better solution, and a better solution should be always used.



Share this post


Link to post
Share on other sites


float airResistance = someConstant*this->velocity*this->velocity;
this->velocity += (this->acceleration-airResistance ) * (dTimeMs/1000.0f);

this->position += this->velocity * (dTimeMs/1000.0f);




"someConstant" depends on the object... but since u didnt want an accurate calculation i left that out...

Share this post


Link to post
Share on other sites
To expand on what Dragon_Strike said, the actual formula is as follows:

F = -1/2 * V^2 * A * Cd * rho

where:
F - value of the drag force [N]
V^2 - squared speed [m/s]
A - frontal area of object, where "frontal" means directed towards move direction [m^2]
Cd - drag coefficient (in range 0 - 1)
rho - density of air = 1,168 kg/m^3

As you can see A, Cd and rho can be chosen empirically so you end up with one constant as Dragon_Strike pointed out:

C = -1/2 * A * Cd * rho

And then the equation simplifies to:

F = C * V^2

Share this post


Link to post
Share on other sites
If you want frame-rate dependent damping (which isn't the same as air resistance), then you want your variable become half its original value in time T:

time t = 0, x = X
time t = T, x = X/2

over that period T, each update of time dt you multiply x by f. That means over the whole time period you want:

X * f * f * ... * f = X * f^(T/dt) = X/2

so f = 0.5^(dt/T)

i.e. each update multiply your damped variable by 0.5^(dt/T) where dt is the timestep, and T is a constant (the time you would want your variable to decay to half its original value). The bigger T is, the smaller the damping.

Obviously you only need to evaluate f once per system update, so calculating the power function shouldn't be a problem.

Share this post


Link to post
Share on other sites
It's not perfect but I often use this sort of thing. It's got a few slow function calls in it but at least it's resistant to changes in frame size



void Object::update(float timeDeltaSec)
{
float currentSpeed = this->velocity.Length();
float drag = currentSpeed * currentSpeed * someDragCoefficient;

this->velocity = this->velocity * exp(-timeDeltaSec * drag);
}


Share this post


Link to post
Share on other sites
Quote:
If you want frame-rate dependent damping (which isn't the same as air resistance), then you want your variable become half its original value in time T:


Thanks, this is what I was looking for.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!