Determine Vector between 2 coordinates

Come in here to talk about your sky-net style world-destroying super bots!

Moderator: Defcon moderators

User avatar
roflamingo
level3
level3
Posts: 404
Joined: Fri Jan 19, 2007 10:25 am

Determine Vector between 2 coordinates

Postby roflamingo » Tue Feb 23, 2010 1:52 am

I am trying to find the formula for this.... maybe I am phrasing wrong.

E.g. starting at Moscow, calculate what compass-bearing would you need to travel to eventually reach Paris?

Bonus points if the solution is already in LUA.
User avatar
DinoSteve
level3
level3
Posts: 251
Joined: Fri Aug 21, 2009 10:36 pm
Location: California, US

Postby DinoSteve » Tue Feb 23, 2010 2:39 am

Code: Select all

function Bearing(x1, y1, x2, y2)
    local dx = x2 - x1
    local dy = y2 - y1

    return math.atan2(dx, dy)
end


Results in radians, not degrees
The above post is not intended as an attack on you. It's not about making you look stupid for not searching. It merely states the facts. Please don't be offended.
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Tue Feb 23, 2010 2:49 am

What happened to math.deg?
Smoke me a kipper, I'll be back for breakfast...
User avatar
DinoSteve
level3
level3
Posts: 251
Joined: Fri Aug 21, 2009 10:36 pm
Location: California, US

Postby DinoSteve » Tue Feb 23, 2010 2:57 am

Ace Rimmer wrote:What happened to math.deg?


Since all the trig functions work with radians it's best to stick to them. Only use math.deg for outputting results. That way you don't waste CPU converting types needlessly, less code, and you don't risk accidentally forgetting to convert back.
Last edited by DinoSteve on Tue Feb 23, 2010 3:01 am, edited 1 time in total.
The above post is not intended as an attack on you. It's not about making you look stupid for not searching. It merely states the facts. Please don't be offended.
User avatar
Ace Rimmer
level5
level5
Posts: 10803
Joined: Thu Dec 07, 2006 9:46 pm
Location: The Multiverse

Postby Ace Rimmer » Tue Feb 23, 2010 3:00 am

Thanks for the clarifications. :D
Smoke me a kipper, I'll be back for breakfast...

Return to “AI Bots”

Who is online

Users browsing this forum: No registered users and 5 guests