Public Group

# gun turrent control

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

## Recommended Posts

if i had a sprite that was a cannon tile. and i want a rotating gun that turns to the enemy when it shoots? i actually am having a rotating gun turrent ontop of my tower sprite so i can rotate it all by itself. what would i do to solve this problem? im sure it involves finding the angle from the creature and the gun turrent. btw this is a 2d tiled game. using directx and c++ any advice would be nice.

##### Share on other sites
Check out the function atan2().

##### Share on other sites
Say your turret is at x1y1 and the enemy is at x2y2, you're looking for the angle at x1y1 of the right triangle formed between x1y1, x2y1, and x2y2. There's a number of ways to solve this with trigonometry, I think one way would be arctan( (y2-y1) / (x2-x1) )

##### Share on other sites
You could use the atan2( y, x ) function to calculate the angle. You should let y be the vertical difference between the creature and the cannon, and x be the horizontal difference. Then rotate the sprite with the angle it produces. Remember that when the angle is zero the cannon should point in the positive x-direction and that it should rotate counter-clockwise to the angle produced.

##### Share on other sites
is there no sprite/model rotation function in directX? in openGL, what i would do is rotate the matrix of the turret, with the bottom middle being the bottom end of the turret. food for thought

EDIT: sorry, that doesnt take into account the direction that a projectile would need to fire in :l my bad.

EDIT: yeah, i didnt actually read your entire post when i wrote this. heh sorry :)
if you want to find the angle between the creature and the turret, couldn't you solve for it with sine law with a arbitrary right angled triangle?

EDIT: that sine law thing i said was wrong, but i just drew it and the solution is even simpler than i imagined. hehe.

[Edited by - AAAP on November 27, 2005 8:04:55 PM]

##### Share on other sites
Quote:
 if you want to find the angle between the creature and the turret, couldn't you solve for it with sine law with a arbitrary right angled triangle?
As has been mentioned, atan2() is really the best choice for this in c++.

##### Share on other sites
yeah, you're right about that, it seems, wizardpc :O

[Edited by - AAAP on November 27, 2005 8:14:39 PM]

##### Share on other sites
ty for the help guys. yes i can never ever say turret, as i always say turrent. i also say wiff instead of with... i think i have the bad grammer plague ... anyways thanks got it working

##### Share on other sites
<language nazi high command>Actually, that's spelling and pronunciation, not grammar.</language nazi high command>

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634835
• Total Posts
3019539
×