Sign in to follow this  
powerskill

xna Turret

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this