Reverse circular motion
I have a query relating to reverse circular motion. I have the code below. case 1 is clockwise circular motion and case 2 is anticlockwise. The switch between case 1 and 2 is done with a user touch. When it switches from clockwise to anticlockwise it uses the same value in the sin()
and cos()
which is timer/1.5
. This causes the anticlockwise motion to begin from a different position on my planet. I want to calculate the value that is required for my timer in order for the anticlockwise motion to start from where it left off during the clockwise motion and thus have a smooth transition. Does anyone know the correct Math for this as my mind is stuck?
Just a small explanation of the code below. I am calculating the position of开发者_运维问答 my Robot by taking the x and y coordinates of the Planet it is on and performing a circular motion with a radius of the Planet's radius + 55 (which is a comfortable height for my sprite).
case 1:
positionX = AttachedPlanet.positionX + sin(timer/1.5)*(AttachedPlanet.planetRadius + 55);
positionY = AttachedPlanet.positionY + cos(timer/1.5)*(AttachedPlanet.planetRadius + 55);
timer = timer + 0.02;
break;
case 2:
positionX = AttachedPlanet.positionX + cos(timer/1.5)*(AttachedPlanet.planetRadius + 55);
positionY = AttachedPlanet.positionY + sin(timer/1.5)*(AttachedPlanet.planetRadius + 55);
timer = timer + 0.02;
Why not just keep the same function and switch to decrementing the timer? If you need some measure of total time, maintain a second timer that always goes up.
To switch direction, you need to subtract the sin/cos results. So something like
dir = +1; //initialization
if (switchDirection) //event handling
dir *= -1;
//calculation
positionX = AttachedPlanet.positionX + dir*cos(timer/1.5)*(AttachedPlanet.planetRadius + 55);
positionY = AttachedPlanet.positionY + dir*sin(timer/1.5)*(AttachedPlanet.planetRadius + 55);
精彩评论