I appologise for what could be a really silly question, but I havn't found an answer that made sense to me, yet.
What I'm after is how to find the angle (in degrees or radians) between two co-ordinate points
eg: what is the angle between x1,y1 and x2,y2
any and all help greatly appreciated!
Cheers,
How to find angle
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to find angle
My Development Diary - http://shanegadsby.info
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to find angle
I took this code from FlashPunk:
Code: Select all
function math.angle(x1, y1, x2, y2)
local a = math.deg(math.atan2(y2 - y1, x2 - x1))
if a < 0 then
return a + 360
else
return a
end
end
Re: How to find angle
Thanks heaps guys, sorry I fail at math so hard
My Development Diary - http://shanegadsby.info
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: How to find angle
I prefer radians, as, well, all functions are using radians, so degrees are only useful when you are outputting it, also, if you replace the if by the modulus operator you get this:
Which is.. perhaps a bit nicer. On yet another note, I dislike putting your own functions in the std libs, but I guess, as most of the things I mentioned, it's a matter of preference.
Code: Select all
function math.angle(x1, y1, x2, y2)
return math.deg(math.atan2(y2-y1, x2-x1))%360
end
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: How to find angle
Wow, that's really clever. Modulo is a pretty awesome operator.bartbes wrote:Code: Select all
function math.angle(x1, y1, x2, y2) return math.deg(math.atan2(y2-y1, x2-x1))%360 end
Re: How to find angle
heck yes it is, thanks heaps bartbes
My Development Diary - http://shanegadsby.info
Who is online
Users browsing this forum: Google [Bot] and 94 guests