Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

When a sprite is rotating to follow an angle, how to prevent it from reversing direction after a point?

$
0
0

@pythonix wrote:

Below is a section of my code which handles a player's sprite rotating towards an angle after a user touches the screen:

touchState = TouchPanel.GetState();
Vector2 touchPosition;

if (touchState.Count > 0)
{
   touchPosition = new Vector2(touchState[0].Position.X, touchState[0].Position.Y);
   targetPosition = Math.Atan2(player.Position.X - touchPosition.X, player.Position.Y - touchPosition.Y);

if (angle_radians < targetPosition)
{
    angle_radians += 2 * fps;
}

if(angle_radians > targetPosition)
{
    angle_radians -= 2 * fps;
}

player.Angle = angle_radians * -1;
}

The problem I'm having is that when the angle goes past a certain point (I believe 3.15 radians?), the logic no longer functions correctly and the sprite reverses direction in a 360 circle until it meets the target position again.

I know there is something I'm missing from this logic, and I can see the problem, but I'm not sure how to approach handling it.

How would I prevent the sprite from reversing direction?

Thanks

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles