Stack Overflow Asked by Lix on February 16, 2021
Consider the following statement for updating the velocity of a physical object during the integration process in a game:
velocity = velocity * 0.999f + acceleration;
Multiplying the velocity by a value slightly less than one is called "damping" and 0.999f
is the "damping factor". It is said that damping is necessary to remove excess energy caused by numerical instability.
I don’t understand this. I could also use 1.001f
as the "damping factor" and make the claim that it is for adding missing energy (as opposed to exceeding energy) caused by numerical instability:
velocity = velocity * 1.001f + acceleration;
Right? What am I missing?
I’m not talking about drag forces here, am I? Well, after all, a 0.999f
drag factor is removing a part proportional to the velocity. Is the damping just a drag force?
The damping factor 0.999f or DAMPING
is the α
in the digital low-pass filter to smooth the calculated velocity.
OP has
Consider the following statement for updating the velocity of a physical object during the integration process in a game:
velocity = velocity * 0.999f + acceleration;
What am I missing?
What is missing is the implied passage of time scale down by 1/1000.
// old velocity new velocity
// v------v v------------v
velocity = velocity * 0.999f + acceleration*t*0.001;
// factor1 factor2, their sum is 1.0
The t*0.001
is 1.0
and is certainly already factored in the period of updates and the units used to express acceleration
and velocity
.
An alternative would used real units and let the compiler see * (mm_to_m * UPDATE_PERIOD * DAMPING)
is scaling by 1 and trust efficient code will be emitted.
double acceleration; // mm/s/s
double velocity; // m/s
#define UPDATE_PERIOD (1.0 /* seconds */)
#define DAMPING 0.001
#define mm_to_m 1000.0
velocity = velocity * (1.0 - DAMPING) + acceleration * (mm_to_m * UPDATE_PERIOD * DAMPING);
Correct answer by chux - Reinstate Monica on February 16, 2021
Change source of knowledge, sine apparently author has poor knowledge in area of physics simulations.
Looks like some has used Forward Euler method which do not conserve total energy of system. Author do not understands the problem and used "duping" as evil hack.
Proper solution would be use of different algorithm. The simplest is Verlets algorithm (simple as Euler method). Other alternative is called Leap-Frog which is also easy to understand.
There are other more complex algorithms to simulate physics.
What is wrong with Forward Euler method? In this approximation each step introduces an error which is systematic. In each steps system gains a bit of energy. Error is not result of finite precision of calculation, but problem is how algorithm works. Changing time step length reduces this issue, but not quickly enough. Classic example is simulation of planet orbit, which in this algorithm is not elliptical (as it should), but spiral, each orbit will move planet away from sun.
If remedy is described It is said that damping is necessary to remove excess energy caused by numerical instability.
for me this is clear indication that author doesn't know that there are other algorithms which numeric complexity is same, but do not have this weakness. Adding a drag force here is a kind of cheating.
Now if you are implementing a game with physics this "cheat" is not harmful. If you are simulating some physical process then this is worst possible approach with will produce false results.
In alternative algorithms errors are not systematic and system conserves total energy.
Answered by Marek R on February 16, 2021
If you used the *1.001f
approach, your objects would tend to accelerate artificially. In fact, they would soon leave your game-world, probably, due to moving at an extreme speed.
The *0.999f
approach, on the other hand, can not lead to such conspicuous/weird behavior, because the player's brain can easily explain it as "drag force" (if they even notice it).
Answered by Stefan Monov on February 16, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP