xna Turret

Recommended Posts

powerskill    122
I’m trying to create c# xna code that will rotate a turret with a mouse. I have not found a code example or anything for this. How is it done? Edited: I forgot to mention this is a 2d game. I got the mouseState, however how do I use this to actually aim?
mouseState = Mouse.GetState();
double test1 = mouseState.X - ShipPosition.X;
double test2 = mouseState.Y - ShipPosition.Y;

[Edited by - powerskill on July 9, 2007 2:57:36 PM]

Share on other sites
eedok    982
do you want the turret to rotate to face where the pointer is or do you want to the turret to rotate based on the right/left movement of the mouse?

Share on other sites
powerskill    122
Where the pointer is.

Share on other sites
fitfool    113
Im no math wizard but i think this will work. Use GetCursorPos() to get the mouse position. Then subtract the Turrets position from it. Like

XYCoord Direction = (MousePos - TurretPos)

I think this will give you the direction the in were the mouse is reletive to the turret.

Hopefully im right and someone can start were i left and tell him how to use this to rotate the turret.

:D

Share on other sites
eedok    982
just use atan2 then

the code looks like:
turret.rotation=atan2(mouse.y-turret.y,mouse.x-turret.x)*180/PI

(you might have to subtract 90 from the result of the above calculation to accommodate for the way screen coordinates are handled vs graph coordinates, but it's been a while since I've done it so I've forgotten).