**0**

# Weapon Targeting using Trigonometry?

###
#1
Members - Reputation: **528**

Posted 11 April 2012 - 10:36 AM

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

###
#2
Crossbones+ - Reputation: **13064**

Posted 11 April 2012 - 10:44 AM

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.

*“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”*

###
#4
Members - Reputation: **528**

Posted 11 April 2012 - 10:53 AM

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

###
#5
Crossbones+ - Reputation: **13064**

Posted 11 April 2012 - 10:58 AM

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?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

*“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”*

###
#6
Members - Reputation: **528**

Posted 11 April 2012 - 11:06 AM

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.

###
#7
Crossbones+ - Reputation: **13064**

Posted 11 April 2012 - 11:10 AM

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.

*“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”*

###
#9
Crossbones+ - Reputation: **19947**

Posted 11 April 2012 - 12:34 PM

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.

###
#10
Members - Reputation: **528**

Posted 11 April 2012 - 02:02 PM

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.

###
#11
Crossbones+ - Reputation: **19947**

Posted 11 April 2012 - 02:51 PM

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...

###
#12
Members - Reputation: **528**

Posted 11 April 2012 - 03:17 PM

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.

###
#13
Crossbones+ - Reputation: **19947**

Posted 11 April 2012 - 04:44 PM

- 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).

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.

###
#15
Members - Reputation: **118**

Posted 11 April 2012 - 06:34 PM

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