2

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.

Comhex
  • 76
  • 6
  • 1
  • Rearrange equation (3) from the duplicate above to obtain: u = (d / t) - (a t) / 2. – Pieter Geerkens Mar 30 '16 at 19:05
  • 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. – Comhex Mar 30 '16 at 19:15
  • 1
    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. – Pieter Geerkens Mar 30 '16 at 19:27
  • I actually only use a vector for the velocity. The acceleration is just -25 and the displacement is also a value stored in a float. But don't think that this causes the problem because I am moving the sprite only into y-direction and overall it does work but the resulting y-coordinate is not everytime the same although it should. – Comhex Mar 30 '16 at 19:32
  • In physics, only unit-less quantities are eligible to be scalars. Since displacement and acceleration are not unit-less, one must sign them properly as vectors (or in some cases as pseudo vectors, but that distinction is not relevant here), even if only 1D, in order to obtain consistently correct results. – Pieter Geerkens Mar 30 '16 at 21:20
  • Also, be sure to use average values for velocity over each time interval when calculating displacement, not initial or final. That is the third most common source of error. – Pieter Geerkens Mar 30 '16 at 21:25
  • 2
    You might be interested in something called "Steering behaviours", specifically the "arrive" behaviour. – Vaillancourt Apr 01 '16 at 19:25

3 Answers3

3

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.

  • 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. – Comhex Mar 30 '16 at 13:25
  • 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. – DavidTaubmann Apr 05 '16 at 19:11
0

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.

https://www.desmos.com/calculator/f0bglhnjjg

0

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.

Comhex
  • 76
  • 6