Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fallen_Angel

Simple turning problem (working out angles etc)

This topic is 6171 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''ve just started to add an AI to my game and am trying to make the computer opponent turn to face me when i move around it. At the moment it sort of works when i''m directly above, below or to the side of it. There''s only 8 directions for consideration. Any help would be appreciated. Here''s the code anyway: if (opponentY == m_nY) { if (opponentX < m_nX) m_nDesiredDirection = 6; else m_nDesiredDirection = 2; break; } if (opponentX == m_nX) { if (opponentY < m_nY) m_nDesiredDirection = 0; else m_nDesiredDirection = 4; break; } if (opponentY < m_nY) { if (opponentX < m_nX) addAngle = 270; else addAngle = 0; } else { if (opponentX < m_nX) addAngle = 180; else addAngle = 90; } distanceX = opponentX - m_nX; distanceY = opponentY - m_nY; checkAngle = abs(distanceY) / abs(distanceX); checkAngle += addAngle; if ((checkAngle <= 22) || (checkAngle > 338)) m_nDesiredDirection = 0; if ((checkAngle > 22) && (checkAngle <= 68)) m_nDesiredDirection = 1; if ((checkAngle > 68) && (checkAngle <= 112)) m_nDesiredDirection = 2; if ((checkAngle > 112) && (checkAngle <= 158)) m_nDesiredDirection = 3; if ((checkAngle > 158) && (checkAngle <= 202)) m_nDesiredDirection = 4; if ((checkAngle > 202) && (checkAngle <= 248)) m_nDesiredDirection = 5; if ((checkAngle > 248) && (checkAngle <= 292)) m_nDesiredDirection = 6; if ((checkAngle > 292) && (checkAngle <= 338)) m_nDesiredDirection = 7; break; opponentX & Y are my x,y co-ordinates m_nX & Y are the computer''s x,y co-ordinates m_nDesiredDirection just represents the direction to turn to, (0 is N, 1 is NE etc)

Share this post


Link to post
Share on other sites
Advertisement
If you have only 8 directions you can face - N, NE, E, SE, S, etc - then use combinations to obtain the direction:

#define NORTH 1
#define EAST 2
#define SOUTH 4
#define WEST 8
int direction = 0;
if( vx<0 ) // negative x-velocity
direction &= WEST; // left
else if( vx>0 )
direction &= EAST; // right
if( vy<0 ) // negative y-velocity, assume means up
direction &= NORTH; // up
else if( vy>0 )
direction &= SOUTH;

direction will hold the sum of the appropriate orientations; if you're heading up and right, direction will hold 3 (NORTH + EAST). The technique employed is called bitmasking . Look it up.

If you want the angle in degrees, multiply direction by 45.

Edited by - Oluseyi on September 26, 2001 1:29:34 PM

Share this post


Link to post
Share on other sites

  • 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!