3
\$\begingroup\$

I'm currently working on a mobile game which mainly consists of a rotating line and a ball. The line rotates 0.500 degrees clockwise around its center every frame. The ball is at first positioned in the centre of the line. When the user touches the screen, the ball should move along the line and when the user stops touching the screen the ball should move in the opposite direction.

I have the line rotation, and the coordinates of the line(top-left corner).

I need to find the position of the ball if the ball moves along the line at a speed of 5.

\$\endgroup\$
1
\$\begingroup\$

If the ball didn't move on the line, its path would be a circle. You know the popular parametric equation of circle (=how you usually generate points on circle)?

x = r * cos(θ)
y = r * sin(θ)

It is all you need - just plug in the right numbers. First, the angle theta θ is, as you said 0.5 degrees a frame. Second, you said the ball moves along the line at speed of 5 (I assume you meant units per frame since your angle is per frame), not surprisingly the ball's distance from center is r or more precisely frames times speed. And lastly if your line does not start(the rotation center) form origin, you need to translate the results:

Vec2 getBallPosition(Vec2 center, float angularSpeed, float ballSpeed, int frames)
{
  Vec2 result;
  result.x = (ballSpeed * frames) * cos(angularSpeed * frames);
  result.y = (ballSpeed * frames) * sin(angularSpeed * frames);
  result += center;
  return result;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.