• Advertisement
Sign in to follow this  

Weapon Targeting using Trigonometry?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='CryoGenesis' timestamp='1334163223' post='4930300']
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
[/quote]
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[img]http://img841.imageshack.us/img841/1246/examplei.png[/img]
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.

Share this post


Link to post
Share on other sites
[list]
[*]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).
[/list]
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.

Share this post


Link to post
Share on other sites
I was having the same issue a few weeks ago and it was solved here

[url="http://www.gamedev.net/topic/622356-targeting-for-shooting/page__p__4924937"]http://www.gamedev.net/topic/622356-targeting-for-shooting/page__p__4924937[/url]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement