XNA - 2D Rotation of an object to a selected direction

Posted by lobsterhat on Game Development See other posts from Game Development or by lobsterhat
Published on 2013-05-01T07:09:00Z Indexed on 2013/06/30 16:29 UTC
Read the original article Hit count: 244

Filed under:
|
|

I'm trying to figure out the best way of rotating an object towards the directional input of the user. I'm attempting to mimic making turns on ice skates.

For instance, if the player is moving right and the input is down and left, the player should start rotating to the right a set amount each tick. I'll calculate a new vector based on current velocity and rotation and apply that to the current velocity. That should give me nice arcing turns, correct?

At the moment I've got eight if/else statements for each key combination which in turn check the current rotation:

// Rotate to 225
if (keyboardState.IsKeyDown(Keys.Up) && keyboardState.IsKeyDown(Keys.Left))
{
    // Rotate right
    if (rotation >= 45 || rotation < 225)
    {
        rotation += ROTATION_PER_TICK;
    }
    // Rotate left
    else if (rotation < 45 || rotation > 225)
    {
        rotation -= ROTATION_PER_TICK;
    }
}

This seems like a sloppy way to do this and eventually, I'll need to do this check about 10 times a tick. Any help toward a more efficient solution is appreciated.

© Game Development or respective owner

Related posts about xna-4.0

Related posts about movement