gun turrent control

Started by
10 comments, last by willthiswork89 18 years, 4 months ago
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.
Advertisement
Check out the function atan2().
enum Bool { True, False, FileNotFound };
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) )
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.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
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]
|aaap.penopticon.com| My website, including game projects. Collaboration/comments are welcome, please visit :)
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++.
yeah, you're right about that, it seems, wizardpc :O

[Edited by - AAAP on November 27, 2005 8:14:39 PM]
|aaap.penopticon.com| My website, including game projects. Collaboration/comments are welcome, please visit :)
<grammar nazi squad>It's turret, not turrent</grammar nazi squad>
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
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
<language nazi high command>Actually, that's spelling and pronunciation, not grammar.</language nazi high command>

This topic is closed to new replies.

Advertisement