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

Click For Summary

Discussion Overview

The discussion revolves around calculating the angle at which a projectile should be fired by an enemy in a 2D game environment, where gravity is not a factor. Participants explore the mathematical relationships between the positions and velocities of the player and the enemy to determine the optimal firing angle for the projectile.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Some participants suggest using relative velocities to simplify the problem by reorienting the axes, placing the enemy at the origin and the player on the x-axis.
  • One participant proposes that the y-velocities of the projectile and player must be equal for a successful hit, leading to a relationship between the projectile's speed and the player's velocity.
  • Another participant expresses concern about the complexity of the equations derived for calculating the angle and seeks alternatives to simplify the computations.
  • Some participants mention that as long as the projectile's speed is greater than the player's speed, it should eventually hit the player.
  • A later reply introduces a method that avoids full coordinate transformation, suggesting the use of normalized vectors and orthogonal components to derive the necessary velocities for hitting the player.

Areas of Agreement / Disagreement

Participants generally agree on the need to calculate the angle for the projectile but propose different methods and approaches to achieve this. There is no consensus on a single method, and the discussion remains unresolved regarding the most efficient computational approach.

Contextual Notes

Some limitations include the dependence on the definitions of velocities and positions, as well as the unresolved nature of the equations presented. The discussion also highlights the complexity of the calculations involved in determining the angle.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
2
Views
2K
Replies
14
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 3 ·
Replies
3
Views
10K