In the code of the PID camera on Day 504 (https://guide.handmadehero.org/code/day504/#10452 ), it seems that Casey is doing an analytic integration of the acceleration to compute the camera position. But I have always been told that analytic integration of the equations of movement only works in simple cases, when the acceleration is constant, and otherwise we have to use the Euler method or something.
In the camera code, the acceleration is computed with a spring-like equation, but Casey computes the position (DeltaP
) by integrating this acceleration twice, like if it was a constant. Here is the code in question:
v3 ddP = A*TotalDeltaP + B*TotalDeltadP; v3 dP = dt*ddP + Camera->dP; v3 DeltaP = 0.5f*dt*dt*ddP + dt*Camera->dP; if((LengthSq(DeltaP) < LengthSq(TotalDeltaP)) || (LengthSq(Hadamard(dPTarget - dP, Camera->MovementMask)) < Square(0.001f))) { P += DeltaP; }
If this is correct, why would we ever want to use approximate numerical methods like
dP += ddP * dt; P += dP * dt;
if we can always compute the position with the full equation?