Jump to content
  • Advertisement
Sign in to follow this  
powerskill

xna Turret

This topic is 4027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!