How to determine compass direction between two points?

For AI and campaign script related discussions and questions
Post Reply
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

How to determine compass direction between two points?

Post by aubergine »

In the AI I'm making, I want the AI to build anti-aircraft defences closer the enemy, but have artillery a bit further away from the enemy (eg. behind shorter-range defences, sensors, etc).

At a later point, assuming the JS API allows interaction with the in-game chat system, I'd also want the AI to relay information to human players using chat.

So, if I have point A (x1,y1) and point B (x2,y2), I want to get a compass direction from the perspective of point A as to where point B is, something like this:

getCompassDirection(x1,y1,x2,y2)

And the return value would be in degrees from north or, perhaps more eloquently, N, NE, E, SE, S, SW, W, NW. Problem is that it's baking my brain trying to find an elegant way of achieving this.

I'd also want a "clock direction" version of that, based on the player's map orientation - eg. "Base under attack at 6 o'clock" - that's also baking my brain. (and there's no JS API method yet to work out what map orientation a given player is using)

Any ideas?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Cyp
Evitcani
Evitcani
Posts: 784
Joined: 17 Jan 2010, 23:35

Re: How to determine compass direction between two points?

Post by Cyp »

Don't know about in Javascript, but something like atan2(x2 - x1, y2 - y1) gives the angle (where 0 = S, and π/2 = E, ±π = N, -π/2 = W).
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: How to determine compass direction between two points?

Post by aubergine »

JS has atan2 on the Math object: http://www.w3schools.com/jsref/jsref_obj_math.asp

I'm crap at math - using return value from atan2 (in radians), how would I convert that in to degrees with 0º being North?
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
User avatar
JDW
Regular
Regular
Posts: 1669
Joined: 18 May 2010, 20:44

Re: How to determine compass direction between two points?

Post by JDW »

aubergine wrote:JS has atan2 on the Math object: http://www.w3schools.com/jsref/jsref_obj_math.asp

I'm crap at math - using return value from atan2 (in radians), how would I convert that in to degrees with 0º being North?
Is this what you're looking for? --> http://www.teacherschoice.com.au/maths_ ... angles.htm
"Speak when you are angry and you will make the best speech you will ever regret."
-- Ambrose Bierce
User avatar
aubergine
Professional
Professional
Posts: 3459
Joined: 10 Oct 2010, 00:58
Contact:

Re: How to determine compass direction between two points?

Post by aubergine »

Awesome - thanks!
"Dedicated to discovering Warzone artefacts, and sharing them freely for the benefit of the community."
-- https://warzone.atlassian.net/wiki/display/GO
Post Reply