I am programming a 2D game. When a ball is launched it should decelerate and eventually come to a stop. I have implemented the deceleration part but when the ball nearly stops it starts accelerating. I understand that I should implement some kind of condition for when it should stop (when velocity is 0), however all of my attempts have failed.

These are my variables:
v, v0 - velocity
acc - acceleration
dt - delta time

Here is a code sample of how the velocity gets updated on every game tick:

v.setX(v0.getX() + acc.getX()*dt);
v.setY(v0.getY() + acc.getY()*dt);
  • \$\begingroup\$ How are you computing your acceleration? \$\endgroup\$ – DMGregory May 5 at 14:06
  • \$\begingroup\$ @DMGregory acceleration = friction coefficient * g, acceleration is constant \$\endgroup\$ – lucyKel May 5 at 14:13
  • \$\begingroup\$ If you want the object to stop and stay stopped, then acceleration cannot be constant. (A stationary object subjected to constant acceleration will begin to move) Acceleration due to friction should depend on the direction of the velocity vector too, so it will be non-constant in that regard too. \$\endgroup\$ – DMGregory May 5 at 15:06
  • \$\begingroup\$ @lucyKel Welcome to GDSE. While this could probably be addressed in a language agnostic manner, you might also want to edit the question to include a tag for the language &/or framework you're using. \$\endgroup\$ – Pikalek May 5 at 15:09
  • \$\begingroup\$ @DMGregory I have implemented acceleration as a vector that points in the opposite direction of the velocity. Do you mean the acceleration also has to be updated on every game tick? If so, the how? \$\endgroup\$ – lucyKel May 5 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.