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.
Point an arrow to the right angle
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Point an arrow to the right angle
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)
edit:
a full example
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
a full example
-
- Prole
- Posts: 40
- Joined: Wed Aug 03, 2011 3:13 pm
Re: Point an arrow to the right angle
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
No it doesn't give the right angle. I ran it several times, it just never points in the right direction.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Point an arrow to the right angle
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,...)
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
Yes the image is aiming up. But I added 90 to it.
Here is the link to the code http://pastebin.com/XP6zh4rA.
Here is the link to the code http://pastebin.com/XP6zh4rA.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Point an arrow to the right angle
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:
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.
Re: Point an arrow to the right angle
Can we get back to something interesting now (like the Olympics)?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
.
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 195 times
Re: Point an arrow to the right angle
I tried reversing the two but no difference.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Point an arrow to the right angle
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.
R to reset.
- Attachments
-
- arrow.love
- (8.7 KiB) Downloaded 195 times
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests