I am trying to move my sprite in a specific time to a specific position while using frame-independent or time-dependent movement. My sprite is also accelerated every frame to simulate gravity. I know the acceleration, the distance and the needed time. So i used the formula for acceleration based movement: s = 0,5 · a · t2 + v₀ · t + s₀ which is equivalent v₀ = (s-s₀) / t - a · t / 2. However, in my implementation the sprite does not move to the specified y-coordinate. The lower the starting y-coordinate is the higher is the resulting y-coordinate. For the time I used a quarter second. Here is my implementation:

    float resultY = viewport.getWorldHeight() / 6f * 5f;
    float time = 0.25f;
    velocity.y = (resultY - circle.getY()) / time - (-25f * time / 2f);

and these are called every frame:

    velocity.y -= 25f;
    circle.translate(velocity.x * delta, velocity.y * delta);`

What did I do wrong or is there a better method to move an accelerated sprite to a specific position in a specific time?

EDIT: I didn't mention that I want the sprite to stop at that specific point. I figured out that using my formula the sprite moves to the specified point in the specified time but it keeps moving and does not stop there. Is there a formula to solve that problem?

EDIT2: Ok, I realize that my last question is nonsense because stopping the sprite at a specified position and a specified time with a given acceleration is impossible. Thanks for all the answers.

  • 1
    \$\begingroup\$ Possible duplicate of How to implement deceleration and stopping over a certain distance \$\endgroup\$ Mar 30 '16 at 19:01
  • \$\begingroup\$ Rearrange equation (3) from the duplicate above to obtain: u = (d / t) - (a t) / 2. \$\endgroup\$ Mar 30 '16 at 19:05
  • \$\begingroup\$ I actually did that as you can see in the equation for v₀ above. But based on your comment I assume that something with my implementation is wrong. Anyways thank you for your help. \$\endgroup\$
    – Comhex
    Mar 30 '16 at 19:15
  • 1
    \$\begingroup\$ The most common errors are (1) the units are incorrect; or (2) the sign on an initial value is incorrect. Remember that all values (even time, in a sense) are vectors, even if only positive and negative along a number line. Have you properly defined your displacement, velocity, and acceleration vectors with the same direction convention? Draw a diagram if you are not sure, and ensure that a positive velocity increases a positive displacement, and that a positive acceleration increases a positive velocity. Ensure you have a consistent origin convention. \$\endgroup\$ Mar 30 '16 at 19:27
  • 2
    \$\begingroup\$ You might be interested in something called "Steering behaviours", specifically the "arrive" behaviour. \$\endgroup\$
    – Vaillancourt
    Apr 1 '16 at 19:25

I've run into this kind of trouble a few times, but not yet on 3d.

First of all, I'm not really sure you transformed correctly the acceleration formula... maybe this source can help in that way.

Secondly, at the end of your post you mention you want to specify a specific TIME for the sprite from one point to the other, if that is your goal then you should really apply upon an easing formula, which basically applies trigonometry upon your starting and your finishing points, but maybe your framework has already some shortcuts to achieve that.

Finally, if you really want to write a formula to achieve your personalized acceleration in a specific timeframe, you would requiere an iteration to apply your formula. If you do, in that path I believe this source can help you a bit.

Maybe this is not a complete answer for your specific case, but might help you in the right path.

  • \$\begingroup\$ The second source you posted is close to my problem but a little bit different therefore I couldn't apply it to my problem. In my case I know the acceleration, the time and the position to reach adn I want to calculate the starting velocity. \$\endgroup\$
    – Comhex
    Mar 30 '16 at 13:25
  • \$\begingroup\$ v0 is usually the velocity your sprite has already before the iteration, so if it is sitting parked somewhere, it's v0 should be 0. If it was moving, then it should have a property to know at what velocity was it moving. \$\endgroup\$ Apr 5 '16 at 19:11

This can be done in a simple 3 step looped process.

Step1: Clear screen of all moving objects or sprites

Step2: Apply formula -- Height-(9.8)*T^2

Step3: Apply objects or sprites in new position

I even made you quick graph to show you what I mean.



I solved my problem using steering behaviours thanks to the suggestion by @Alexandre Vaillancourt. The "arrive" behaviour was the solution for me as suggested in his comment.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.