# XNA Correct tank rotation

I'm making a 2D game in which the player controls a tank.

I can make a tank and that's it, but what's really messing with my mind is how to make it rotate accordingly.

I want it to behave just like the Wii game, Tanks. Fixed directions and no real front and back of the tank.

Driving then left should make it spin to the left. Driving, then down, doesn't have to rotate it, just steer the other direction.

I edged out a tutorial a while ago on how to do this by dividing degrees in 2 by 180 degrees. But I just couldn't find this damn site again.

Hope you guys can understand what I am trying to say. Thanks in advance:)

+2

source to share

I think you are looking for simply the best way to minimize the modulo rotation of the tank 180 degrees.

I would use the angle between the desired direction of travel and the direction of the tank current. Make sure this is the minimum angle, then compare this to the angle between the direction of the tank current + 180 degrees. Something like:

``````// smallest angle between the current direction and the desired direction
minAngle1 = Math.Abs(Math.Min(tankAngle - desiredAngle, desiredAngle - tankAngle));

// smallest angle between the opposite direction and the desired direction
oppositeAngle = (tankAngle + 180) % 360;
minAngle2 = Math.Abs(Math.Min(oppositeAngle - desiredAngle, desiredAngle - oppositeAngle));

// get the smaller of two to rotate to
if (minAngle1 < minAngle2)  {
// we know that we should rotate the current direction to the desired direction
} else {
// rotate the opposing direction to the desired direction
}
```

```

Note that you will need to play with your turn signals to make sure you turn correctly. Also, I assumed you know your angles of rotation, if you have vectors you can simplify this a bit by using a dot product between two vectors instead of an angle for comparison.

+2

source

I am assuming you are drawing your tank as a sprite? In this case, an overload of the SpriteBatch.Draw method occurs, which allows you to specify the angle of rotation around the origin.

Here is an example on how to use it from MSDN

The above example will keep rotating your sprite, so you will need to add some custom logic to only rotate it on the keyboard. Here's a simple example on how to validate keyboard input. So add logic that checks if the right or left button was pressed and update the rotation angle, if any. If the Up or Down button is pressed, you just reposition your sprite.

I hope this makes sense, otherwise just let me know.

+4

source

Is your problem direction movement versus pivot angle?

``````Vector2 moveDir = new Vector2(Math.Cos(rotation), Math.Sin(rotation));
position += (moveDir * speed);
```

```

The speed here will be a number for how fast you want to go in that direction. position is another vector2 for the position of the sprite. As Chami says, you can draw it by rotating using the SpriteBatch.Draw overload. The rotation for the Cos and Sin methods should be in radians, but I think Draw should be in degrees if I remember correctly. MathHelper.ToRadians (degrees) and MathHelper.ToDegrees (radians) should solve this.

There are many XNA tutorials and examples on the site http://creators.xna.com/en-US/education/catalog/

0

source

All Articles