views:

723

answers:

3

Hi

Im making a 2D game where the player controls a tank.

I can make the tank, and all, but whats 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 with no real front and back on the tank.

Driving up, then left should make it rotate to the left. Driving up, then down should not make it rotate, just drive the other direction.

I red a tutorial a while back about some way to do that by dividing the degrees into 2 180 degree parts. But i have simply not been able to find that damn site again.

I hope you guys are able to understand what im trying to say. Thanks in advance :)

+4  A: 

I assume you're drawing your tank as a sprite? In that case there's an overload of the SpriteBatch.Draw method that allow you to specify the rotation angle around the origin.

SpriteBatch.Draw overload

Here's an example on how to use it from MSDN

The example above will keep rotating your sprite, so you will need to add some custom logic so it will only rotate it according to keyboard input. Here's a simple example on how to check for keyboard input. So add logic that checks if the right or left button has been pressed, and update the rotation angle if they have. If it's the up or down button that has been pressed you simply modify the position of your sprite.

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

Tchami
Yeah, i got all that :)What i want is for the tank to decide which way for the tank to rotate when i want it to move.
Moulde
A: 

Is your problem with the direction of movement based on the angle they have rotated?

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

Speed here would be a number for how fast you want to move in that direction. position is another Vector2 for the position of the sprite. As Tchami says you can draw it with the rotation using the SpriteBatch.Draw overload. Rotation for the 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 that.

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

Android
No my problem is that i want my tank to rotate the least possible when i tell it to go somewhere.
Moulde
+2  A: 

I think what you're looking for is simply the best way to minimize the rotation of the tank, modulo 180 degrees.

I would use the angle between the desired movement direction and the tank's current direction to start. Make sure this is the minimum angle, then compare that with the angle between the tank's current direction + 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 you'll need to play with your rotation signs to ensure you're rotating the right way. Also, I've assumed you know your rotation angles, if you have vectors you can simplify this a little bit by using the dot product between the two vectors instead of the angle for comparisons.

Ron Warholic