# time independent damping

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

## Recommended Posts

Hello, I trying to make this behaviour : An object B follow an object A. If B is far from A, B will move to fast to A. When B approaches A, B will move slower to A. I use a damping function like so : void Update(float dt) { posB = lerp(posA, posB, 0.9); } This way B will move fast to A if A is far, and when the distance between A and B get smaller, B will move each frame from a shorter distance. I works, but only at a constant frame rate since it doesn't consider the delta during two frame. I'd like to have the same movement at 10fps or 500fps (I don't want to block my framerate). Does anyone have an idea on how can I use the delta in this interpolation, to make this damping function time independent ? Thanks !

##### Share on other sites
What you need to do is compute the FPS and take the reciprocal of it. You then simply use the seconds per frame as a coefficient in your algorithm. This adjusts for the different FPS. So, someone viewing at 60fps would view the same as someone at 30fps because each frame at 60fps moves half as much each frame. Half as much * twice as fast = looks the same.

Doing this also puts your animation in units of distance and time instead of distance and cycles which makes a lot more sence.

*edit*
I forget to add one thing-
Currently your animation is cycle/FPS dependent. You actually *want* to make it cycle/FPS independent and time dependent. Because regardless of the FPS of the animation, time will always remain the same. So an object moving at 10 pixels per second will run the same on all computers regardless of their FPS.
*/edit*

Hope that helps.

##### Share on other sites
Hello OuncleJulien, thanks for your answer, but I'm allready using the time for all my linear movements. As you can see, my Update function take a paramater : "float dt" which is the time during 2 frames. So when I need to update a posistion I allways do it like so :

pos = offset * dt;

But my problem is that I don't know how to use the delta (dt) in my equation, because the movement I want is not linear.

Let say object A is at position 0 and object B is at position 10.
And let use my wrong, and framerate dependent equation :

posB = lerp(posA, posB, 0.5);

during 1 second at 5 fps :
- frame 0 : posB = 10
- frame 1 : posB = 5
- frame 2 : posB = 2.5
- frame 3 : posB = 1.25
- frame 4 : posB = 0.625
final position after one second : 0.625

during 1 second at 2 fps :
- frame 0 : posB = 10
- frame 1 : posB = 5
final position after one second : 5

Ok, this is wrong.
Now what I'm looking for is how to use the delta in my coefficient ?

posB = lerp(posA, posB, ????);

Is it possible ?

Thanks

##### Share on other sites
This is what I use. You need to store the rate of change of your variable as well as the value.

//// SmoothCD for ease-in / ease-out smoothing // Based on Game Programming Gems 4 Chapter 1.10//#include "../maths/include/precision.hpp"namespace JigLib{  template <typename T>  inline void SmoothCD(T &val,          // in/out: value to be smoothed                       T &valRate,      // in/out: rate of change of the value                       const tScalar timeDelta, // in: time interval                       const T &to,     // in: the target value                       const tScalar smoothTime)// in: timescale for smoothing  {    if (smoothTime > 0.0f)    {      tScalar omega = 2.0f / smoothTime;      tScalar x = omega * timeDelta;      tScalar exp = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);      T change = val - to;      T temp = (valRate + omega * change) * timeDelta;      valRate = (valRate - omega * temp) * exp;      val = to + (change + temp) * exp;    }    else if (timeDelta > 0.0f)    {      valRate = (to - val) / timeDelta;      val = to;    }    else    {      val = to;      valRate -= valRate; // zero it...    }  }  }

##### Share on other sites
thanks for the answer MrRowl !
now I will look in Game Programming Gems 4 to understand :)

##### Share on other sites
If you don't mind looking at math, take a look at the following paper by David Eberly:

http://www.geometrictools.com/Documentation/MovingAtConstantSpeed.pdf

It does a good job at explaining this.

Have fun :)

Edit: hmmm, nevermind, I read to quickly and though you were trying to correct the speed so that it was constant when following a curve. I'll leave the article there since it's a good one anyway :)

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004467

×