Weapon Targeting using Trigonometry?

Started by
14 comments, last by CryoGenesis 12 years ago
The arctangent returns the angle in radians. If you are going to use angles, using radians is the natural choice for several reasons. But if you use vectors, none of that matters.

The description of your problem is still a bit confused. The radius is not 20*20 pixels: It's just 20 pixels. It's unclear what you mean by the "position" of the circle. One usually specifies where the circle is by its center. And I don't know what you mean by the first point and the second point. It reminds me of a scene in Analyze This ("Are you talking about the first thing or the second thing?"). :)

Perhaps a picture would make it all clear...
Advertisement
examplei.png
The P1 represents the first point of a line (a line has two points x1,y1 and x2, y2)
The line's position represents the circle's angle which should point to the target.
This is so turrets can turn to point at an enemy and shoot at it.

I want to work out:
the x and y position of the lines second point using the circle's angle.
work out the circle's angle using the target's x and y position.

I want to work out the x and y position of the second point so the turret can set a trajectory for bullets.
I want to work out the angle of the circle so I can rotate the turrets image based on its angle.

  • Compute the vector V = T - P1
  • Normalize V . I.e., multiply each component by 1/length(V), where the length of a vector (x,y) is sqrt(x*x+y*y).

You can use this vector V to spew bullets in that direction. The velocity of the bullet would be V*bullet_speed. [EDIT: And the point you were looking for is P1+radius*V (sorry, I forgot).]

As for the rotation of the image, if you are using an API that takes an angle, you'll have to use atan2(V.y, V.x) to get it. The default pose of your turret should point to the right so things work out well. If the API expects the angle in degrees, you need to multiply by 180/PI before you call it. The sad thing is that the first thing your API will do with that value is multiply it by PI/180 to convert it back to radians, and then probably compute its cosine and sine, which are V.x and V.y.
Thanks!
Haven't tried it but I'll try it tonight.
I was having the same issue a few weeks ago and it was solved here

http://www.gamedev.net/topic/622356-targeting-for-shooting/page__p__4924937
Oh wow thanks

This topic is closed to new replies.

Advertisement