Page 1 of 2
Point an arrow to the right angle
Posted: Sat Aug 04, 2012 1:24 am
by coderm
Hi guys,
I am trying to make something in which an arrow would aim at another object. The problem that I am having is that I don't know how to get the correct angle/degrees.
I need it to automatically calculate the angle to aim at. The arrow would be point1 for example and the object would be at point2 but I don't know how to calculate the exact radius/degree angle to set it to aim right at it. I hope that explains it.
I am sure others might have done it. Its probably kinda simple. Hopefully someone can solve it or has.
Thanks.
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 9:28 am
by richapple
There's a function math.atan2(
y,
x) (not sure why 2, just because math.atan is not cool)
Something like this (not tested, you may have to reverse the ending and starting point)
Code: Select all
local guy = { x = 100, y = 100} -- your hero
local aim = { x = 10, y = 10} -- his point of aim
function love.draw()
local angle = math.atan2(guy.y - aim.y, guy.x - aim.x)
love.graphics.draw(guy_s_image, guy.x, guy.y, angle)
end
edit:
a full example
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 11:17 am
by pancakepalace
Code: Select all
angleInRadians = math.atan2(y2 - y1, x2 - x1)
angleInDegrees = math.atan2(y2 - y1, x2 - x1) * 180 / PI
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 2:07 pm
by coderm
No it doesn't give the right angle. I ran it several times, it just never points in the right direction.
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 2:15 pm
by dreadkillz
We don't have enough details about your code. The above codes for angles are correct where: (x2,y2) = point of what you want to look at. (x1,y1) point of where your arrow's origin is. Post your .love for us to assist you better. Your arrow's original image HAS to be pointing right for this to work by default.
If your arrow's image is originally pointing up, add 90 degrees to the above. If its pointing left originally, add 180 degrees. If its pointing down originally, subtract 90 degrees.
Then you do love.graphics.draw(arrow,x,y,r,...)
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 3:00 pm
by coderm
Yes the image is aiming up. But I added 90 to it.
Here is the link to the code
http://pastebin.com/XP6zh4rA.
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 3:08 pm
by dreadkillz
Your arrow is assigned objectx[2],objecty[2]. Look at the above formula's again.
math.atan(objectx - arrowx, objecty - arrowy). EDIT: woops meant to say math.atan2(objecty-arrowy,objectx-arrowx)
You have it reversed down there:
Code: Select all
function love.update(dt)
angleInDegrees = (math.atan2(objecty[2] - objecty[1], objectx[2] - objectx[1]) * 180 / math.pi)+90
end
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 3:51 pm
by Ref
coderm wrote:
Hi guys,
I am trying to make something in which an arrow would aim at another object. The problem that I am having is that I don't know how to get the correct angle/degrees.
I need it to automatically calculate the angle to aim at. The arrow would be point1 for example and the object would be at point2 but I don't know how to calculate the exact radius/degree angle to set it to aim right at it. I hope that explains it.
I am sure others might have done it. Its probably kinda simple. Hopefully someone can solve it or has.
Thanks
.
Can we get back to something interesting now (like the Olympics)?
FYI Tangent is not a well behaved function => infinity for some angles(division by zero) - tan2 takes care that.
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 3:53 pm
by coderm
I tried reversing the two but no difference.
Re: Point an arrow to the right angle
Posted: Sat Aug 04, 2012 4:28 pm
by dreadkillz
Here's a love for you. Remember that the origin of the image drawn is at the top left corner. So if you want an arrow to point the true center instead of the corner, you need to offset it.
R to reset.