Angle required to hit coordinates including air resistance

AI Thread Summary
To determine the angle needed to hit a target in a game while accounting for air resistance, it's essential to first calculate the angle without air resistance. The projectile's motion is influenced by gravity and air resistance, which reduces velocity by 1% every 20 ticks per second. A numerical search method, such as binary search or Newton's method, can be employed to refine the angle based on simulated flight paths. Precomputing angles for various distances and elevations can also expedite the process. Ultimately, the simulation must closely approximate the game's physics for accurate targeting.
TheShermanTanker
Messages
13
Reaction score
4
Hi, I know I've asked this before but I didn't manage to solve the problem before. To give context I'm trying to find the angle to hit a target with given coordinates from my current location in a particular game. (I'm modding the game) I can do it with zero problems when not including air resistance, but the frustrating as hell thing is that solutions with air resistance are rare to come by online. It turns out the most promising solution I came across recently by desmos required the target be the same height at you (Ie both you and target have to be at the same y coordinate. Y is the up and down coordinate in this particular game's physics engine btw), and if you are above the target or below it wouldn't work. Would be great if i could get some help here :)

If it helps, here's how the physics engine models the projectiles:
Gravity: 20m/s2 (Easily solved)
Air resistance: Take the x, y, and z velocities of the projectile and multiply it by 99% 20 times every second. Ie:
Following code is run 20 times every second:
proj.motX = proj.motX * 0.99;
proj.motY = proj.motY * 0.99;
proj.motZ = proj.motZ * 0.99;

The physics engine fortunately does not calculate the surface area, mass or kinematic viscosity of air, phew XD
 
Physics news on Phys.org
Does the gravitational acceleration happen first? That is, is the update cycle for the z velocity (calling acceleration due to gravity ##g## and your game time tick ##\delta t##) ##v\rightarrow v-g\delta t\rightarrow 0.99(v-g\delta t)## or is it ##v\rightarrow 0.99v\rightarrow 0.99v-g\delta t##?
 
Ibix said:
Does the gravitational acceleration happen first? That is, is the update cycle for the z velocity (calling acceleration due to gravity ##g## and your game time tick ##\delta t##) ##v\rightarrow v-g\delta t\rightarrow 0.99(v-g\delta t)## or is it ##v\rightarrow 0.99v\rightarrow 0.99v-g\delta t##?
[CODE lang="java" title="Java"]//The block below is called 20 times a second

//This can be ignored
this.locX += this.motX;
this.locY += this.motY;
this.locZ += this.motZ;

float f4 = 0.99F; //Drag

f1 = 0.05F; //Gravity. Since this is called 20 times a second, 0.05 works out to 20m/s2

//Applying drag
this.motX *= (double) f4;
this.motY *= (double) f4;
this.motZ *= (double) f4;

this.motY -= (double) f1; //Applying gravity

//This isn't important given the context, can ignore
this.setPosition(this.locX, this.locY, this.locZ);[/CODE]
 
TheShermanTanker said:
If it helps, here's how the physics engine models the projectiles:
Run that simulation for different angles until you find one that is close enough. A binary search should converge quickly, Newton's method even faster. Use the angle without air resistance as the intial point, or precompute a look-up-table of good initial points for combinations of distance and elevation with air resistance.
 
Last edited:
A.T.'s method is best - note that you can combine your x and z targets into a "range", so it's a 2d problem.

You can approximate this with a differential equation and solve it to get a closed-form expression for the ##y(t)## and ##r(t)##, where ##r^2(t)=x^2(t)+z^2(t)##. But they both look like ##A+Bt+Ce^{\alpha t}## and you can't eliminate ##t##, ##x##, or ##r## between them. So you end up having to do some kind of numerical search anyway. Easiest to just search with the actual simulation, particularly using the zero-drag approximation and/or a lookup of common values as initial guesses.
 
If you are just making a game, then you only need to make it look valid to a casual observer. It doesn't have to be correct. Does your simulation look invalid?
 
FactChecker said:
If you are just making a game, then you only need to make it look valid to a casual observer. It doesn't have to be correct. Does your simulation look invalid?
Oh hi, I'm modding an existing game unfortunately, if i was making my own game i would just disable air resistance for special projectiles like these out of laziness XD
 
A.T. said:
Run that simulation for different angles until you find one that is close enough. A binary search should converge quickly, Newton's method even faster. Use the angle without air resistance as the initial point, or precompute a look-up-table of good initial points for combinations of distance and elevation with air resistance.
What i understand from this is use v^2 +- sqrt(etc etc) to calculate the angle without air resistance first, then create an imaginary (so called) projectile and simulate it's flight path, collect the data of the simulation and perform corrections to the angle until it's close enough, did i get that right? (Hope i did :P)
 
Ibix said:
You can approximate this with a differential equation and solve it to get a closed-form expression for the y(t)y(t)y(t) and r(t)r(t)r(t), where r2(t)=x2(t)+z2(t)r2(t)=x2(t)+z2(t)r^2(t)=x^2(t)+z^2(t). But they both look like A+Bt+CeαtA+Bt+CeαtA+Bt+Ce^{\alpha t} and you can't eliminate ttt, xxx, or rrr between them. So you end up having to do some kind of numerical search anyway. Easiest to just search with the actual simulation, particularly using the zero-drag approximation and/or a lookup of common values as initial guesses.
Could you explain what you mean by this?
Ibix said:
the y(t)y(t)y(t) and r(t)r(t)r(t), where r2(t)=x2(t)+z2(t)r2(t)=x2(t)+z2(t)r^2(t)=x^2(t)+z^2(t). But they both look like A+Bt+CeαtA+Bt+CeαtA+Bt+Ce^{\alpha t} and you can't eliminate ttt, xxx, or rrr between them.
(My background wasn't in physics, i apologise for being dumb)
Ibix said:
So you end up having to do some kind of numerical search anyway. Easiest to just search with the actual simulation, particularly using the zero-drag approximation and/or a lookup of common values as initial guesses.
This is just a guess here, but if i get you correctly what you mean by numerical search/actual simulation would be to get the angle from the equation that excludes air resistance first, fire the projectile at that angle and subject it to air resistance and gravity (Hidden from the player of course), collect data from the flight path, and use the data to perform corrections to the launch angle and repeat this over and over until the angle is correct? (If i vaguely recall someone has told me once that air resistance cannot be discovered from just an equation) Thanks so much for the help btw :)
 
  • #10
TheShermanTanker said:
I'm modding an existing game unfortunately,
This is the worst case. You are not free to make your own simplified model and they might not have valid physics in theirs. So you have to guess what they did. If you want it to be a very close match to their answers, it might be hard.
 
  • #11
TheShermanTanker said:
What i understand from this is use v^2 +- sqrt(etc etc) to calculate the angle without air resistance first, then create an imaginary (so called) projectile and simulate it's flight path, collect the data of the simulation and perform corrections to the angle until it's close enough, did i get that right? (Hope i did :P)
Yes. For each simulated angle compute the miss distance. Then you minimize the miss using Newton's method:
https://en.wikipedia.org/wiki/Newton's_method
For this you need the derivative of the miss(angle) function, which you can get using a finite difference:
https://en.wikipedia.org/wiki/Finite_difference#Relation_with_derivatives
 
  • Like
Likes TheShermanTanker
  • #12
  • Like
Likes TheShermanTanker
Back
Top