Hitting moving target with projectile (w/ gravity) 2D

In summary, Vince is looking for an equation that will give the angle of firing if the equation is solvable (if it's not solvable, then the bot is out of range, and just won't fire at all). Vince knows the following variables: Bot X, Y, Bot V, Target X, Y, Target V, Acceleration of the bullet due to gravity, Firing speed of the bullet (Vo).
  • #1
evesira
6
0
Hello,

I'll give you a little background information on the problem first. It might help.

I'm working on a video game actually. It's a 2d side view shooter. In it, there are bots (computer controlled players) that have perfect aim. They can hit (assuming it's in range) a target, accounting for the velocity of the target and the effect of gravity on the projectile (though most of the projectiles in the game will travel nearly straight paths, some will curve slightly).

Here's what I need. I need an equation that gives:

2 thetas (angle of firing) if the equation is solvable (if it's not solvable, then the bot is out of range, and just won't fire at all)

And here are the variables I know. These will be plugged into the equation I'm looking for:

--- Bot X, Y
--- Bot V (velocity vector)
--- Target X, Y
--- Target V
--- Acceleration of the bullet due to gravity
--- Firing speed of the bullet (Vo)Just to be clear, there are three objects we're dealing with:

--- the bot (the thing that's firing the bullet)
--- the bullet (affected by gravity)
--- the target (has constant velocity)
Here's what I have so far. This works perfectly, EXCEPT for the fact that it doesn't account for the target's velocity. This is pseudocode, which I'm sure most of you should be able to understand.

____________________________________________________________________
Root = (FireSpeed^4)-(Gravity*( (Gravity*(TargetX^2)) + (2*TargetY*(FireSpeed^2)) ));

if (Root >= 0)
{
Angle1 = arctan(((FireSpeed^2)+sqrt(Root))/(Gravity*TargetX));
Angle2 = arctan(((FireSpeed^2)-sqrt(Root))/(Gravity*TargetX));

if (TargetX < 0)
{Angle1 += pi; Angle2 += pi;}
}

____________________________________________________________________The equation I used there is straight from this wikipedia article:
http://en.wikipedia.org/wiki/Trajec....CE.B8_required_to_hit_coordinate_.28x.2Cy.29
If I'm not a complete moron, I'd say that the velocity of the bot doesn't even matter, since it could just be subtracted from the target's velocity (essentially doing the whole problem with a relative velocity). Correct me if I'm wrong, please. You can solve it using vectors, if you wish. Components would be fine as well.

Any help you can give is appreciated, even if it's just a push in the right direction. If you do explain something, please try to explain it thoroughly. I do understand basic (relative the crazy stuff you guys talk about here) physics, but I'm really stumped on this one.Thanks,
Vince
 
Last edited:
Physics news on Phys.org
  • #2
Anyone?
 
  • #3
really no one knows at all? or are you just unwilling to give guidance for some reason..
 
  • #4
I'm pretty sure that the velocity of the bot fire at the target does matter. The object that you are firing from the bot is moving at the same velocity of the bot, therefore you have to take in consideration the velocity of the bot. because your doing velocity in 2 dimensions, you need to calculate the velocity of the x component, and y component. I think that's what you were asking, seeing how I can't read that code. I hope I could be of some help, and I'm not just telling you something you already know. Good luck anyway.
 

Related to Hitting moving target with projectile (w/ gravity) 2D

1. How does gravity affect hitting a moving target with a projectile in a 2D environment?

Gravity plays a crucial role in the trajectory of a projectile, as it causes the projectile to follow a parabolic path. This means that the projectile will continuously be pulled towards the ground, which can make it more challenging to hit a moving target.

2. What factors influence the accuracy of hitting a moving target with a projectile in a 2D environment?

The accuracy of hitting a moving target with a projectile in a 2D environment can be influenced by several factors, including the initial velocity of the projectile, the angle at which it is launched, the distance between the projectile and the target, and any external forces such as wind.

3. Is it easier to hit a moving target with a faster or slower projectile in a 2D environment?

In general, it is easier to hit a moving target with a faster projectile in a 2D environment. This is because a faster projectile will cover a larger distance in a shorter amount of time, giving the target less time to move out of the way.

4. How can I calculate the trajectory of a projectile to hit a moving target in a 2D environment?

To calculate the trajectory of a projectile, you will need to know the initial velocity of the projectile, the angle at which it is launched, and the acceleration due to gravity. With this information, you can use equations of motion to determine the projectile's trajectory and adjust the angle and velocity as needed to hit a moving target.

5. Are there any techniques to improve my accuracy when hitting a moving target with a projectile in a 2D environment?

One technique to improve your accuracy is to use the concept of relative motion. This means predicting where the target will be in the future based on its current velocity and direction, and adjusting your aim accordingly. Additionally, practicing with different initial velocities and angles can help you develop a better understanding of how gravity affects the trajectory of a projectile.

Similar threads

Replies
4
Views
1K
  • Mechanics
Replies
2
Views
1K
Replies
6
Views
3K
Replies
2
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
Replies
5
Views
1K
Replies
3
Views
4K
Replies
4
Views
1K
Replies
2
Views
1K
Replies
3
Views
16K
Back
Top