Sign in to follow this  
wizardpc

gun turrent control

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


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


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


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


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


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


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


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void MasterMan::spinTank(const int& i, const int& j, const int& k, const int& l)
{
if(k == i && l == j)
return;
if(i > 0 && i 0 && j i2)
{
if(j2 - i2 >= 8)
j2++;
else
j2--;
} else
if(i2 - j2 >= 8)
j2--;
else
j2++;
if(j2 >= 16)
j2 -= 16;
else
if(j2 <

Share this post


Link to post
Share on other sites
if(k == i && l == j)
return;
if(i > 0 && i < 17 && j > 0 && j < 17)
{
int i1 = i * 24 + 12;
int j1 = j * 16 + 8;
int k1 = k * 24 + 12;
int l1 = l * 16 + 8;
double d = atan2(i1 - k1, l1 - j1);
d = ((double)8 * (d + (double)3.1415926535897931 + (double)0.19634954084936207)) / (double)3.1415926535897931;
int i2 = (int)d % 16;
int j2 = zona[j][i].direction & 0xf;
if(j2 != i2)
{
if(j2 > i2)
{
if(j2 - i2 >= 8)
j2++;
else
j2--;
} else
if(i2 - j2 >= 8)
j2--;
else
j2++;
if(j2 >= 16)
j2 -= 16;
else
if(j2 < 0)
j2 += 16;
zona[j][i].direction = (byte)((zona[j][i].direction & 0xf0) + j2);
buildScreen = true;
}

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