Public Group

# Weapon Targeting using Trigonometry?

This topic is 2381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I need some help with weapon targeting.
Say, for instance, I have a turret.
This turret knows it's position(x,y)
where it is looking (in degrees)
and where the enemy is (x,y)
Does anyone know the formula to find out the new degrees of the turret based on the enemy's position against the turrent's current degree and position?
Anyone who helps me will be rewarded a +1 rep

##### Share on other sites
Let the turret position be (Tx, Ty), and the enemy position (Ex, Ey)
The direction vector is going to be (Ex - Tx, Ey - Ty).

Thus the direction vector's slope is (Ey - Ty) / (Ex - Tx).
Therefore the direction vector's angle from the horizontal is equal to A1 = arctan((Ey - Ty) / (Ex - Tx)). You will need to correct the angle so it lands in the correct quadrant, otherwise it won't work properly (look up arctan2 or atan2).

Now assume the turret is already looking at some angle A2 with respect to the horizontal axis. Then it only needs to move an angle of A = A1 - A2 to face the enemy.

This is really easier using vectors instead of trigonometry though.

##### Share on other sites
Hey thanks, you actually answered a question that I was going to ask next.

##### Share on other sites
Oh and I have another question.

Say I have a blank circle. There is a line going from the middle of the circle going outwards based on an angle.
So the circle's radius is something like 20*20 pixels
the circles x and y position is 0,0
so the first point of the line is 10,10 (the middle of the circle)
How do I work out the second point of the line based on the circles angle (lets just say 90 degrees for example).

I think the equation involves PI but I'm not too good with this sort of math

##### Share on other sites

Oh and I have another question.

Say I have a blank circle. There is a line going from the middle of the circle going outwards based on an angle.
So the circle's radius is something like 20*20 pixels
the circles x and y position is 0,0
so the first point of the line is 10,10 (the middle of the circle)
How do I work out the second point of the line based on the circles angle (lets just say 90 degrees for example).

I think the equation involves PI but I'm not too good with this sort of math

You're going to have to reformulate, I don't really see what you mean. Do you mean intersecting a line with a circle and finding both intersection points? Does the line always pass through the circle's center?

##### Share on other sites
Ok I'll try to rephrase.

I want to work out a second point of a line based on a angle.
So I have the first point of a line x and y.
and I want to convert an angle in Degrees to an x position and y position.
other variables you have is the max length of the line (the radius of a circle) and the first point of the line (the centre of a circle : x = (Cx + (Cw/2) ) y = (Cy + (Cw/2) )
I want to find this formula out so I can see where the turret is looking using a line.

##### Share on other sites
Well if you have an initial point (x, y) and an angle theta, then the line's direction vector is just going to be (cos(theta), sin(theta)). You just add that vector to the initial point to produce a second point for the line (x + cos(theta), y + sin(theta)).

If you need the segment between (x, y) and (x + cos(theta), y + sin(theta)) to be bigger, you can scale it by an arbitrary factor c by multiplying the direction vector by c, therefore obtaining (x + c * cos(theta), y + c * sin(theta)) for the second point.

Btw the centre of a circle is generally defined as just (x, y), with bounds (x +- radius, y +- radius). Makes the math easier.

##### Share on other sites
Thanks once again!

##### Share on other sites
Just one note about using angles: If you think about vectors instead, your code will have fewer special cases, it will be easier to read and easier to get right.

As an example, after you use atan2 to obtain an angle, whenever you actually want to use the angle for anything, you'll probably need to compute its sine and cosine. If you store a unit vector instead, its coordinates are precisely (cos(angle), sin(angle)), and you save yourself the trouble of going back and forth between vector and angle representation.

If you need help "deanglifying" some piece of code, I'll be happy to assist.

##### Share on other sites
Hey, do you know of any web tutorials on this because I cant seem to get this working
The arctan doesn't return an angle in degrees it returns a float between -1 and +1.
Can you explain the vector version? The vector version sounds a lot simpler.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632959
• Total Posts
3009458
• ### Who's Online (See full list)

There are no registered users currently online

×