Make a Picture look to the Mouse-y&&x-pos..

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
dataFRAME
Prole
Posts: 14
Joined: Sat Feb 07, 2009 9:12 pm
Location: Germany

Make a Picture look to the Mouse-y&&x-pos..

Post by dataFRAME »

Hello Everybody!

I got a question 'bout to make a Picture via the "r" Variable look at the MousePosition..
I dont know how to start! :C

Thanks from yet till end :X!
Intrested in making music? Theres a Virtual Drum - Software for LÖVE
http://love2d.org/forum/viewtopic.php?f ... bumm#p4193
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Make a Picture look to the Mouse-y&&x-pos..

Post by osuf oboys »

Problem:
Find the angle a of the line from (px, py) to (mx, my). (where the angle is computed from the line segment (0,0)-(1,0) )

Solution:
Assume (px, py) != (mx, my) and let dx = mx - px, dy = my - py, and d = (dx^2 + dy^2)^0.5. Then,
if dx >= 0 then
a = math.deg(math.acos(dx / d))
else
a = 180 - math.deg(math.acos(dx / d)),
where math.acos is the "inverse" of cos. e.g. cos(45 degrees) = 1/sqrt(2) and acos(1/sqrt(2)) = 45 degrees. However, acos(-1/sqrt(2)) is also 45 degrees and so you must take 180 minus the value if dx is less than 0. math.deg just goes transforms radians to degrees, radians being the default unit for the trigonometric functions.

The necessary rotation r is the angle a plus some offset (0,90,180 or 270) depending on the direction the image looks in, e.g. looking to the left or up.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Make a Picture look to the Mouse-y&&x-pos..

Post by Kaze »

Use math.atan2 to get the angle from a position:

Code: Select all

local angle = math.atan2( y, x ) -- Where x and y are "mouse x - picture x" and "mouse y - picture y"
-- angle is in radians, use math.deg( angle ) to get the degrees.
Post Reply

Who is online

Users browsing this forum: No registered users and 203 guests