Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Frame-rate independent linear damping!

Author
Message
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 3rd Nov 2010 03:14
This is probably massive overkill, but it may come in handy for somebody

Damping is usually implemented by multiplying the velocity of an entity by a constant each frame (usually about 0.9 so you lose 10% of the velocity each frame). Alternatively you can use "curvevalue" with a destination of zero, which works out to be the same.

The problem with this method is that the results vary massively depending on the frame-rate, so I decided to solve the equations properly for a variable timestep. I use a constant called 'f' which is the percentage velocity lost each second under zero external acceleration.

Working:


The example code:


The example simulates an entity experiencing damping on three different frame-rates. You can see that they all stay in line even though the third dot is simulated as though it has a 10x lower frame-rate than the middle dot. Using the normal method at this difference in frame-rate would result in a 55% difference in velocity between the slow computer and the fast computer after a single frame on the slow computer!

[b]
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 8th Nov 2010 03:18
It is a bit overkilly, but it's still cool. Best solution is to just fix your timestep.
bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 21st Nov 2010 12:08
This is sweet! I was looking for this a few years back but never found a working solution. I'm gonna bookmark this one!

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Nov 2010 03:43
Quote: "Damping is usually implemented by multiplying the velocity of an entity by a constant each frame (usually about 0.9 so you lose 10% of the velocity each frame). "


Damping another word for friction?

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th Nov 2010 19:32
No, friction works differently.

Friction doesn't depend on velocity, it opposes motion up to a certain maximum friction force:

F = mR

F is the maximum frictional force between the two bodies, and along the direction of motion.
R is the force perpendicular to the direction of motion. (ie. how much the bodies are pushing against each other)
m is a constant which is unique for any pair of materials. The higher the value of m, the greater the friction between bodies of those materials.

The actual friction force felt can range from 0 to F, but will never exceed F, regardless of the velocities of the bodies.

[b]

Login to post a reply

Server time is: 2024-11-24 10:24:36
Your offset time is: 2024-11-24 10:24:36