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!
Make a Picture look to the Mouse-y&&x-pos..
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Make a Picture look to the Mouse-y&&x-pos..
Intrested in making music? Theres a Virtual Drum - Software for LÖVE
http://love2d.org/forum/viewtopic.php?f ... bumm#p4193
http://love2d.org/forum/viewtopic.php?f ... bumm#p4193
-
- Party member
- Posts: 215
- Joined: Sun Jan 18, 2009 8:03 pm
Re: Make a Picture look to the Mouse-y&&x-pos..
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.
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.
Re: Make a Picture look to the Mouse-y&&x-pos..
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.
Who is online
Users browsing this forum: No registered users and 203 guests