Page 1 of 1

need help with lookAt function for two objects

Posted: Tue Feb 05, 2013 10:40 am
by jtomes123
Hello i've tried about ten tutorials and it didn't work at all i need basic function that'll return angle from enemies position and players position, thank for all respnds.

Re: need help with lookAt function for two objects

Posted: Tue Feb 05, 2013 10:47 am
by Robin
You'll need math.atan2. If you want more specific help, please read the rules and upload a .love of your game.

Re: need help with lookAt function for two objects

Posted: Tue Feb 05, 2013 9:40 pm
by FrogsFriend
Something like this gives an angle in Radians.

Code: Select all

-- Returns the angle between two points.
function utils.angle(x1,y1, x2,y2)
	local angle = math.atan2(y2-y1, x2-x1)
	if angle < 0 then angle = angle + math.pi * 2 end
	return angle
end
http://polygeek.com/1819_flex_exploring-math-atan2

Re: need help with lookAt function for two objects

Posted: Wed Feb 06, 2013 8:02 am
by Roland_Yonaba
FrogsFriend wrote:Something like this gives an angle in Radians.

Code: Select all

-- Returns the angle between two points.
function utils.angle(x1,y1, x2,y2)
	local angle = math.atan2(y2-y1, x2-x1)
	if angle < 0 then angle = angle + math.pi * 2 end
	return angle
end
http://polygeek.com/1819_flex_exploring-math-atan2
This was already featured on the wiki, as a snippet, without the angle correction, though.