Facing directions
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?
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...
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...
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]
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]
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;}
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]
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.
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.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement