Declaration of control
I need help in a Math logic issue. Let say I have an object that can be manipulate (move) by user. After the user moved the object I would like the object to continue moving and decelerate to a stop.
Example, when a user move an object from point A to B with 开发者_Go百科a total distance of 100pixel in X axis, after user release a finger, I want to let the object continue moving and decelerate to a stop from point B to point C.
So how can I calculate the new distance of point C if I set the time for it to decelerate and stop in 2sec?
Thank you!
d = ½at² + vit + d0
d0 is the point at which the user "let go". Calculate vi from the motion before letting go. Set a to something negative; you'll have to fiddle with this to get it to feel right. Increment t from 0 through 2. d is where the object will end up. Remember that a and vi are vectors pointing in opposite directions, and that d0 and d are points.
If the acceleration, a, is constant you can use the formula:
d = ½at²
With t = 2s you get d = 2a.
The acceleration is enough to accelerate to v in 2s, so a = v/2.
You get:
d = v
So if v= 100 pixels per second, the object should continue 100 pixels if it stops in 2 seconds under constant deceleration.
If all you want is the distance from B to C, that's easy. If the object went a distance N pixels (say 100) from A to B in k seconds, then the distance from B to C will be N/k.
If you want the location of C, so that A, B and C are integers on the x-axis, C = B+(B-A)/k.
If you want to animate the motion, so that you can update the position x at every time step dt (say 0.1 seconds), then start (at B) with v=N/k, and at each step,
x += v * dt
v -= N * dt/(2 * k)
(Be sure to use floats, not integers, or the round-off error will ruin the effect.)
精彩评论