I'm having a small issue where after applying an impulse or force it is impossible to actually get an object to stop, i.e. a velocity of zero.
This code is run every 1 / 60
of a second:
void RigidBody::Update(double deltaTime) {
double mass = GetMass();
//If static body, do nothing.
if(Math::IsEqual(mass, 0.0)) return;
Vector2D F;
Vector2D total_forces = std::accumulate(_curState._forces.begin(), _curState._forces.end(), Vector2D());
Vector2D total_impulses = std::accumulate(_curState._impulses.begin(), _curState._impulses.end(), Vector2D());
F = _curState._gravity + total_forces + total_impulses;
_curState.ClearImpulses();
SetAcceleration(F / mass);
SetVelocity(GetVelocity() + GetAcceleration() * deltaTime);
SetPosition(GetPosition() + GetVelocity() * deltaTime);
}
RigidBody::SetAcceleration(const Vector2D& acceleration) {
_prevState.SetAcceleration(_curState._acceleration);
_curState.SetAcceleration(acceleration);
}
//Similar for RigidBody::SetVelocity and RigidBody::SetPosition...
If I apply an impulse, say (-1.0, 0.0), the object's acceleration changes by -0.012 or -0.011 instantaneously and only once then reverts to (0.0, 0.0), which then changes the velocity to (-0.012/-0.011, 0.0). Equal and opposite changes in acceleration causes rounding errors to accumulate to the point where the velocity will never be zero and the object will never stop.
Similarly, if I apply a force, (-1.0, 0.0), the object's acceleration changes to (-1.0, 0.0), equal and opposite forces applied so the acceleration changes to (0.0, 0.0) works but the velocity changes so minutely that again, the object will never stop. (The closest I've ever gotten it was (0.009, 0.0) )