1

I have an enemy that can shoot projectiles, but my issue is I am trying to find the angle required to hit the player, but the projectile has to be shot at an exact velocity (i.e can't shoot the projectile more slowly). I have tried the range equation, but I can not simplify it such that I get the theta (angle), that factors in the y distance, the x distance, the velocity, and the gravity.

Sorry for the sprawling question, Teh Cosmic Sloth

  • What is the horizontal range between the enemy and player, or is that variable? Is the launch velocity the only constant (alongside gravitational acceleration)? – Neil Docherty Nov 26 '16 at 22:35
  • 1
    I'm voting to close this question as off-topic because it is not about software or software algorithms. It's a question about math, physics and gaming. – Rob Nov 26 '16 at 22:50
  • What is your range equation? Are you sure that the object is within range? – Teepeemm Nov 26 '16 at 23:01
  • The equation I use is in uneven ground and is the modification under uneven ground that has theta equaling (Insert equation here), That is not the issue I slightly misspoke, because I meant a algebraic simplification I guess you would call it, such that the theta is the isolated variable. My issue is that the equation I use does not factor in initial y. – Teh Cosmic Sloth Nov 30 '16 at 23:25

1 Answers1

0

Well... You have: D - distance to target

g = 9.8 - g- force

v - your firing velocity

Your velocity along X is v*cos(a)

Your velocity along Y is v*sin(a)

you need to travel distance D, hence your time of flight is t = D/v*cos(a)

in that time you need to reach the maximum height of your flight and fall back. At maximum point your vertical speed will be 0, at the end of flight your vertical speed will equal to the starting vertical speed, if we ignore the air resistance.

So, bearing in mind that the equation for speed is v = acc*t (where acc is g for you) t = v/acc. In your case you have to drop speed to 0 and get it back to original, so you have factor of 2.

t = 2*(v*sin(a)/g)

Lets sum it up:

t = D/v*cos(a)

t = 2*(v*sin(a)/g)


2*(vsin(a)/g) = D/vcos(a)

You know everything but angle a.

So, by doing some variable transfers you get

2sin(a)cos(a) = Dg / v^2

which is:

sin(2a) = D*g/v^2

2a = arcsin( D*g/ v^2)

So, this must be the answer.

a = arcsin( D * g / (v^2) ) / 2

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
  • I believe you're assuming that the target is on the same vertical level. – Teepeemm Nov 26 '16 at 23:01
  • Yes. If not, then the expression for the time with regards to the vertical speed will be a bit more complex. – Vladimir M Nov 26 '16 at 23:03
  • Vladmir Sorry for not being clear :/, I did mean with differing vertical levels, Sorry for the mixup :(, -TehCosmicSloth – Teh Cosmic Sloth Nov 28 '16 at 17:13
  • sounds similar to this: http://gamedev.stackexchange.com/questions/53552/how-can-i-find-a-projectiles-launch-angle – Vladimir M Nov 28 '16 at 19:59
  • Vladmir the equation works much better then what I used to use, but for some reason it seems... off here is my simplification (note my v was 10, and my g was 0.05) a = Math.atan((100-Math.pow(10000-0.05*(0.05*Math.pow(xDist,2)+200*yDist),0.5))*(Math.PI/180),(0.05*xDist)*(Math.PI/180))+Math.PI; – Teh Cosmic Sloth Nov 30 '16 at 23:31