- #1
SeanCocteau
- 2
- 0
Hi
I'm a programmer and while my maths is OK, my understanding of physics is not the best so apologies in advance if I'm missing something obvious. My problem is an interaction and animation sequence I'm writing, the user can swipe an object from one position to another. No problem here. The issue is that I need the object to 'dock' to a specific position, again no problem calculating the final resting position.
My problem is that I applying inertia (I decrement the detected speed on each animation cycle so this value is undeterminent) causing the object to slow and then stop once it hits a specific figure. Originally I thought I could calculate the 'expected distance' the object would travel to, but I believe that I did not have enough parameters to do this, only the speed and deceleration value. To calculate the expected distance, I create a second loop, decrement the speed but adding to the expected distance until it hits the stop speed. Not overly elegant, but it returns the correct figure.
However, to get the object to end at a specific position I need to reverse this calculation, adjusting the speed of the object so it lands in the appropriate position. At the moment, I've been using Vf^2 = Vi^2 + 2ad but this hasn't been providing the correct values so far.
For instance, the speed is 38 pixels per animation frame, the initial starting velocity is 0.05 pixels per animation frame and we expect to move 721.0825 pixels in total. After the positioning calculation (which is fine), the expected distance is revised to 785. Note: during the animation, the speed is decremented by 95% on each animation frame so I'm setting the acceleration to 1.05225 (as an arbitary figure).
So if I done things correctly, the speed should be square root of ( ( 2 * 785 * 1.05225 ) + ( 0.05 ^ 2 ) ), which equals 40.65542.
But as you've guessed it, this isn't working and is providing very wonky results? Have I missed something out? Am I using the wrong formula as the speed is not constant (as it is decremented on each animation until the speed reaches 0.05)?
Any help, (of any kind) is gratefully received.
S
Note: I do have a workaround by adjusting the speed using a percentage, e.g. (revised distance / expected distance) * speed that works but isn't pixel perfect. But I'm sure there's a more elegant approach.
I'm a programmer and while my maths is OK, my understanding of physics is not the best so apologies in advance if I'm missing something obvious. My problem is an interaction and animation sequence I'm writing, the user can swipe an object from one position to another. No problem here. The issue is that I need the object to 'dock' to a specific position, again no problem calculating the final resting position.
My problem is that I applying inertia (I decrement the detected speed on each animation cycle so this value is undeterminent) causing the object to slow and then stop once it hits a specific figure. Originally I thought I could calculate the 'expected distance' the object would travel to, but I believe that I did not have enough parameters to do this, only the speed and deceleration value. To calculate the expected distance, I create a second loop, decrement the speed but adding to the expected distance until it hits the stop speed. Not overly elegant, but it returns the correct figure.
However, to get the object to end at a specific position I need to reverse this calculation, adjusting the speed of the object so it lands in the appropriate position. At the moment, I've been using Vf^2 = Vi^2 + 2ad but this hasn't been providing the correct values so far.
For instance, the speed is 38 pixels per animation frame, the initial starting velocity is 0.05 pixels per animation frame and we expect to move 721.0825 pixels in total. After the positioning calculation (which is fine), the expected distance is revised to 785. Note: during the animation, the speed is decremented by 95% on each animation frame so I'm setting the acceleration to 1.05225 (as an arbitary figure).
So if I done things correctly, the speed should be square root of ( ( 2 * 785 * 1.05225 ) + ( 0.05 ^ 2 ) ), which equals 40.65542.
But as you've guessed it, this isn't working and is providing very wonky results? Have I missed something out? Am I using the wrong formula as the speed is not constant (as it is decremented on each animation until the speed reaches 0.05)?
Any help, (of any kind) is gratefully received.
S
Note: I do have a workaround by adjusting the speed using a percentage, e.g. (revised distance / expected distance) * speed that works but isn't pixel perfect. But I'm sure there's a more elegant approach.