Reputation: 55
I am working on rotating an object at 160 degrees/second, and having it slow down to a complete stop at a pre-specified angle. For example, if the angle chosen was 30 degrees, it would spin very fast and slow down, eventually halting at 30 degrees. I am having trouble coming up with the algorithm to do it, which is what I'm asking for.
For the time being, assume that all you need to do to set the rotation is object.Rotation = 30 (degrees). Feel free to write this in Java/Lua/C++/JavaScript.
What I have so far (basically nothing):
//Assume that wait(1) waits 1 second
int angle = 70;//Fast rotations at first but slow down as time goes on
for (int i = 140; i > .1; i = i - 5)//Must work for every angle
{
for (int a = 0; a < i; a = a + 10)
{
object.Rotation = a;
wait(.05);
}
}
Upvotes: 1
Views: 1011
Reputation: 23757
pseudocode:
int max_speed = 10
int target_angle = 30
while (target_angle != object.Rotation) do
int delta = target_angle - object.Rotation
delta = max(min(delta / 5, max_speed), -max_speed) + max(min(delta, 1), -1)
object.Rotation = object.Rotation + delta
wait(.05)
end while
Upvotes: 2