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

In summary, the player needs to know the position of the enemy, their velocity, and the angle at which the enemy will fire. They then use this information to calculate their own position relative to the enemy, and find the angle at which they should be shooting in order to hit the enemy.
  • #1
ChichoRD
4
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
  • #2
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)
 
  • #3
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?
 
  • #4
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?
 
  • #5
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).
 
  • #6
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
  • #7
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!
 
  • #8
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.
 

1. What is 2D linear movement?

2D linear movement is the movement of an object in two dimensions, typically represented on a flat surface or plane. It involves the object moving along a straight line in one direction.

2. How do I calculate the projectile angle for an enemy with 2D linear movement?

The projectile angle can be calculated using trigonometric functions, specifically the tangent function. The formula is: angle = arctan(distance / height). This will give you the angle at which the projectile needs to be launched in order to hit the enemy.

3. What factors should be considered when creating an enemy with 2D linear movement?

Some important factors to consider include the speed and direction of the enemy's movement, the speed and trajectory of the projectile, and the distance between the enemy and the target. It is also important to consider any obstacles or terrain that may affect the movement of either the enemy or the projectile.

4. Can I use any programming language to create an enemy with 2D linear movement?

Yes, you can use any programming language that supports mathematical calculations and graphics. Some popular options include Java, C++, and Python.

5. How can I test and adjust the projectile angle for accuracy?

One way to test the accuracy of the projectile angle is to run simulations with various starting positions and angles and see if the projectile successfully hits the enemy. You can also adjust the angle and run the simulation again until you find the optimal angle for hitting the enemy consistently.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
Replies
16
Views
4K
  • Special and General Relativity
Replies
6
Views
1K
  • Other Physics Topics
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Replies
3
Views
6K
Replies
14
Views
3K
  • Beyond the Standard Models
Replies
14
Views
3K
  • Mechanics
Replies
1
Views
2K
  • Materials and Chemical Engineering
Replies
2
Views
6K
Back
Top