Jump to content
  • Advertisement

Archived

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

fyhuang

Facing directions

This topic is 5321 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

Somehow, no matter how much I think, I can''t think of how to make something face in any of 8 directions when given a point. Does anyone know this?

Share this post


Link to post
Share on other sites
Advertisement
Uh... what?

How is this supposed to be related to AI?

If you''re talking about facing in a 2D world... you take your "target" position, substract your current position, and you get a vector towards your target: the way you should face. (works as well in other dimentions, though I thought you''re working in 2D since you only want 8 directions).

The rest is pretty obvious...

Share this post


Link to post
Share on other sites
Assuming I understood the original question ok:

Take the ''thing''s position and the point in question.
Subtract the first from the second to get the vector between them.
Use trigonometry to change that vector into an angle. (Inverse tangent I think... sorry, not my strong point.)
Round the angle to the nearest 45 degrees.
The angle will now be one of 8 values between 0 and 315 inclusive.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites

float PI = 3.141592654f;
double getAngle(float x1, float y1, float x2, float y2)
{
double angle=0;
if ((x1<x2) &&(y1<=y2))
angle = atan(abs((y1 - y2) / (x1 - x2)));

if ((x1>=x2)&&(y1<y2))
angle = atan(abs((x1 - x2) / (y1 - y2))) + (PI / 2);

if ((x1>x2)&&(y1>=y2))
angle = atan(abs((y1 - y2) / (x1 - x2))) + PI;

if ((x1<=x2)&&(y1>y2))
angle = atan(abs((x1 - x2) / (y1 - y2))) + (PI * 3 / 2);
return angle;
}

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyBravo

float PI = 3.141592654f;
double getAngle(float x1, float y1, float x2, float y2)
{
double angle=0;
if ((x1<x2) &&(y1<=y2))
angle = atan(abs((y1 - y2) / (x1 - x2)));

if ((x1>=x2)&&(y1<y2))
angle = atan(abs((x1 - x2) / (y1 - y2))) + (PI / 2);

if ((x1>x2)&&(y1>=y2))
angle = atan(abs((y1 - y2) / (x1 - x2))) + PI;

if ((x1<=x2)&&(y1>y2))
angle = atan(abs((x1 - x2) / (y1 - y2))) + (PI * 3 / 2);
return angle;
}



Excellent code, johnnyBravo.

And as others have pointed out this isn't really a game AI issue per se....


Ferretman

ferretman@gameai.com

From the High Mountains of Colorado

GameAI.Com
is Changing Providers--see the site for more details!


[edited by - Ferretman on January 16, 2004 4:11:04 PM]

Share this post


Link to post
Share on other sites
Devils Advocate:

Why wouldn''t implementation of an effector be considered part of AI? If you want to be strict, AI would only be what is between the sensor and effector, which would certainly make discussions more intimidating for those just starting out who are trying to figure out how it all fits together.

Share this post


Link to post
Share on other sites
quote:
Original post by cannelbrae
Devils Advocate:

Why wouldn''t implementation of an effector be considered part of AI? If you want to be strict, AI would only be what is between the sensor and effector, which would certainly make discussions more intimidating for those just starting out who are trying to figure out how it all fits together.


I''ll grant utterly that one can split a hair wherever one wishes, however this kind of thing is not typically associated with AI per se. It''s the decision making process that goes on inside the software agent that''s the "AI" here, not the mechanics of turning, the translation of thought and intent into action. The distinction is similar to your deciding what to say in reply and your fingers actually typing said response.



Ferretman

ferretman@gameai.com

From the High Mountains of Colorado

GameAI.Com

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!