Targeting arrow, a trig problem.

Started by
4 comments, last by laeuchli 21 years, 9 months ago
Dear All, I''m trying to make a arrow it my game, that points toward a target. More specificaly, I have two points, the player, and the target, and I need to find the angle to roate a arrow so that it points toward the target. I''m having trouble figuring out how to do this. What I tried to do is: x=targetxpos-playerxpos y=targetypos-playerypos theta=atan(py/px); theta=theta+playerangle; I also drew a triangle and other diagraims(Which I can''t post here), which seem to show this is the right way to do it, but when I try, I don''t get what I want(I get odd random angles). Has anyone done this before? Is there any easier way? Thanks, Jesse
Advertisement
If you are working in 3d, and have an arrow object, you should be able to easily figure the rotation.

What I do for these things is ensure that the artist has created an arrow that points down the x-axis ( doesn''t matter what axis as long as you are on the same page as the artist ). Then I would create an vector the same way you did. This results in two vectors, (1, 0, 0) and the vector pointing from the player to the target.

I have a function to create a quaternion that would rotate a given vector to a second vector. Then I would apply this quaterion to the arrow object.

If you are not working with quats, then calculate your dx and dy from the differences of these two vectors. The atan function with then get you the angle that you need to rotate the arrow object. Be careful about which way you would need to rotate the arrow. I usually use a cross product to figure out if I need a CW or CCW rotation.
My guess would be that it is the range on the atan function that is a problem. Try using atan2 instead. atan only has a range of (-pi/2,pi/2) while atan2 has a range of [0,2pi]. I''ll assume the px and py versus the x and y used to calculate the displacement is a typo.
Keys to success: Ability, ambition and opportunity.
if you were only working with the positions of the player/target you do not have to add the players angle to the arrow angle.
-----The scheduled downtime is omitted cause of technical problems.
the angle of the player is important, because if the player ship was facing the away from the target, it would have to turn more to face the target, which is something I want the target arrow to reflect....
Okay, I guess you could not understand my post. Let me try again.

Is the arrow a seperate entity from the player''s ship? If it is then you do not need to take into account the ship''s orientation when you rotate it, b/c regardless of which direction the player''s ship is pointing, the target arrow will always point toward the target.

So you need to calculate the angle between the vector pointing from the players ship to the target, and the vector that the arrow object is facing before any orientation is applied to it.

I have all objects created facing down the x-axis. This allows me to know ahead of time that the object''s initial facing is (1, 0, 0).

Calculate your theta using the difference''s between these two vectors.

This topic is closed to new replies.

Advertisement