Started by Apr 11 2012 10:36 AM

,
15 replies to this topic

Posted 11 April 2012 - 10:36 AM

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

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

Posted 11 April 2012 - 10:44 AM

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.

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

Posted 11 April 2012 - 10:48 AM

Hey thanks, you actually answered a question that I was going to ask next.

Posted 11 April 2012 - 10:53 AM

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

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

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

Posted 11 April 2012 - 11:06 AM

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.

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.

Posted 11 April 2012 - 11:10 AM

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.

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.

Posted 11 April 2012 - 12:34 PM

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.

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.

Posted 11 April 2012 - 02:02 PM

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.

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.

Posted 11 April 2012 - 02:51 PM

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

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

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.

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.

Posted 11 April 2012 - 06:34 PM

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

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