• ### What is your GameDev Story?

#### Archived

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

# Facing directions

This topic is 5479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
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 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 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 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 on other sites

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 on other sites
quote:
Original post by cannelbrae

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015442
×