Creating an Enemy with 2D Linear Movement: Calculating Projectile Angle

AI Thread Summary
The discussion centers on calculating the angle for a projectile fired by an enemy in a 2D top-down game, where gravity is not a factor. The developer seeks to determine the angle needed for the projectile to hit a moving player, given the player's position and velocity, as well as the enemy's position and the projectile's speed. Suggestions include using relative velocities and simplifying the problem by reorienting axes to place the enemy at the origin. The key equations involve ensuring the y-velocities match for a hit and calculating the time it takes for the projectile to reach the player. The conversation concludes with a more efficient method for implementation without complex transformations.
ChichoRD
Messages
4
Reaction score
0
Hi, I am a game developer and recently found myself in a situation where I wanted to create an enemy that shoots at you given a set of variables. The game takes place in a top down view so there is no need for gravity acceleration, just 2d linear constant movement.

The question to answer is: Given the known position for the player (p), his velocity (v); the position of the enemy (ep) and the modulus of the vector at which the projectile will be fired (ev), find the angle the bullet should use.
 
Last edited:
Technology news on Phys.org
ChichoRD said:
Hi, I am a game developer and recently found myself in a situation where I wanted to create an enemy that shoots at you given a set of variables. The game takes place in a top down view so there is not need for gravity acceleration, just 2d linear constant movement.

The question to answer is: Given the known position for the player (p), his velocity (v); the position of the enemy (ep) and the modulus of the vector at which the projectile will be fired (ev), find the angle the bullet should use.
Welcome to PF.

What is your math background? If you ignore air resistance effects on the bullet, the math is pretty simple algebra. Since you are dealing with gravity (a constant acceleration downward), you use the Kinematic Equations of Motion for a constant acceleration. :smile:

https://en.wikipedia.org/wiki/Equations_of_motion

(and scroll down to the simpler algebraic equations)
 
berkeman said:
Welcome to PF.

What is your math background? If you ignore air resistance effects on the bullet, the math is pretty simple algebra. Since you are dealing with gravity (a constant acceleration downward), you use the Kinematic Equations of Motion for a constant acceleration. :smile:

https://en.wikipedia.org/wiki/Equations_of_motion

(and scroll down to the simpler algebraic equations)
Hi, thank you for such a quick response, howver I may have not expressed with enough detail my problem.

All my movements occur in a surface plane, NO GRAVITY. Something like the xz plane.
All my movements have this ecuation for their position: dr = v · dt
However, time (t) is unknown for me as well as a velocity, the bullet velocity of which I only know its modulo.

After doing some calculations myself I ended up with this set of equations which not seem vvery friendly for computation:

p = ep p (player's position), ep (enemy's position)
p0 + vp · t = ep0 + ev · t p0(player's initial position), vp (player's instant velocity at the time of going to shoot), ep (enemy's initial position), ev (enemy bullet initial position)
(p0 + vp · t - ep0)/t = ev
(p0 + vp · t - ep0)/(t · ||ev||) = u u(unitary vector I'm trying to solve for (or theta))

Then I split this into x and y components:
(p0x + vpx · t - ep0x)/(t · ||ev||) = cos(theta)
(p0y+vpy · t - ep0y)/(t · ||ev||) = sin(theta)

If I try to solve that system of equations I fear that the computations required for theta are too expensive.
What can I do?
 
ChichoRD said:
p0 + vp · t = ep0 + ev · t
That equation says that after time ## t ## the player and the enemy player will be at the same position. Is that what you want?
Oh, you have some very confusing symbols. If you use vp for the velocity of the player then there is no sense in using ev for the velocity of the bullet - how about vb?
 
pbuk said:
That equation says that after time ## t ## the player and the enemy player will be at the same position. Is that what you want?
Yes, when I referred to a bullet was only to simplify the things as it would be easier to understand, but in reality it is the enemy itself who launches at you, and I want to find the perfect vector for it to launch so that it predicts perfectly your movement and collides with you. (I know the modulus because I got to choose it (choose its launching speed) but not the angle).
 
I suggest you use relative velocities. For simplicity, we can take the enemy at the origin and reorient the axes so that the player is on the x-axis at ##(x_0, 0)##.

The player will have some velocity ##(u_x, u_y)##. And the enemy will fire a projectile at some speed ##v## at some angle ##\theta## to the x-axis. The velocity of the projectile relative to the player will be ##(v \cos \theta - u_x, v \sin \theta - u_y)##.

In order to hit the player, the y-velocities must be equal. Hence ##v\sin \theta = u_y##. That gives you ##\theta##. The ##t## at which the player is hit is given by ##(v \cos \theta - u_x)t = x_0##.

Note that as long as ##v > u## the player should eventually be hit.

For the more general case, you either rotate the axes so that you have this scenario; or, generalise these calculations directly.
 
  • Like
Likes mfb
PeroK said:
I suggest you use relative velocities. For simplicity, we can take the enemy at the origin and reorient the axes so that the player is on the x-axis at ##(x_0, 0)##.

The player will have some velocity ##(u_x, u_y)##. And the enemy will fire a projectile at some speed ##v## at some angle ##\theta## to the x-axis. The velocity of the projectile relative to the player will be ##(v \cos \theta - u_x, v \sin \theta - u_y)##.

In order to hit the player, the y-velocities must be equal. Hence ##v\sin \theta = u_y##. That gives you ##\theta##. The ##t## at which the player is hit is given by ##(v \cos \theta - u_x)t = x_0##.

Note that as long as ##v > u## the player should eventually be hit.

For the more general case, you either rotate the axes so that you have this scenario; or, generalise these calculations directly.
Thanks! I think I'll be able to implement that in code, the Wolfram Aplha solution for my before stated equation seemed horrible to compute. I'll keep you guys informed!
 
You can use this method without doing a full coordinate transformation, too. Let's still put the enemy at the origin, but keep the player at ##\vec p=(x_0,y_0)## with velocity ##\vec u=(u_x,u_y)##. Define ##\vec p_o=(y_0,-x_0)/|\vec p|## as normalized vector orthogonal to the direction of the player. The velocity orthogonal to the player/enemy separation is then given by ##\vec u_o = (\vec p_o \cdot \vec u) \vec p_o##. The other velocity component will have a magnitude of ##\sqrt{v^2-u_o^2}## and go in the direction of p: It's ##\vec u_p = \pm \vec p \frac{\sqrt{v^2-u_o^2}}{|\vec p|}##. From here on it's a bit of casework, but the square root needs to be real to hit the player, and if you can hit at all then the "+" solution is available and will lead to the fastest possible hit. If ##|\vec v|>|\vec u|## then the square root will always be positive and you can always hit.

To skip even the first coordinate transformation, simply define ##\vec p## as difference in position between player and enemy, as we didn't use any other absolute coordinates.

There are two square roots in the calculation, everything else is just addition, multiplication and division. No trigonometry.
 
Back
Top