Point an arrow to the right angle

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.
coderm
Prole
Posts: 6
Joined: Sat Aug 04, 2012 1:14 am

Point an arrow to the right angle

Post 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.
User avatar
richapple
Citizen
Posts: 65
Joined: Sun Dec 25, 2011 10:25 am

Re: Point an arrow to the right angle

Post 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
pancakepalace
Prole
Posts: 40
Joined: Wed Aug 03, 2011 3:13 pm

Re: Point an arrow to the right angle

Post by pancakepalace »

Code: Select all

angleInRadians = math.atan2(y2 - y1, x2 - x1)
angleInDegrees = math.atan2(y2 - y1, x2 - x1) * 180 / PI
coderm
Prole
Posts: 6
Joined: Sat Aug 04, 2012 1:14 am

Re: Point an arrow to the right angle

Post by coderm »

No it doesn't give the right angle. I ran it several times, it just never points in the right direction.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Point an arrow to the right angle

Post 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,...)
coderm
Prole
Posts: 6
Joined: Sat Aug 04, 2012 1:14 am

Re: Point an arrow to the right angle

Post 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.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Point an arrow to the right angle

Post 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
Last edited by dreadkillz on Sat Aug 04, 2012 4:23 pm, edited 1 time in total.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Point an arrow to the right angle

Post 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.
Attachments
arrow.love
position of mouse relative to center
(648 Bytes) Downloaded 201 times
coderm
Prole
Posts: 6
Joined: Sat Aug 04, 2012 1:14 am

Re: Point an arrow to the right angle

Post by coderm »

I tried reversing the two but no difference.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Point an arrow to the right angle

Post 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.
Attachments
arrow.love
(8.7 KiB) Downloaded 222 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests