Jump to content
  • Advertisement
Sign in to follow this  
KniForz

Determine Initial velocity of a projectile.

This topic is 2831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been struggling on a problem lately. Let's say an enemy at some point launch a projectile towards the player at a given angle. I'd like to be able to determine the initial velocity of that projectile so that it hit the player if he doesn't move.

So, What do I Know:

-The angle of the projectile
- Enemy's position
- Player's position
- the gravity constant.

I've been looking for formulas and they all require the time. I don't care wether it takes 2-4-6 seconds. I Just want this projectile to hit the position of the player.

The problem is that the enemy can be higher, lower or at the same level.

I've found this page which gives me a bunch of formulas where I could possibly isolate the initial velocity.
http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
But it doesn't take into account the distance in z ( height)
It's for a 2D game (platformer)

Any help?

Thanks

Share this post


Link to post
Share on other sites
Advertisement
I found this link: http://www.gamedev.net/topic/107074-calculating-projectile-launch-angle-to-hit-a-target/

Asks similar question to yours. Since you know the angle, the only variable left for you is the initial velocity.

[color=#1C2837][size=2]y = x tan a - (g / (2v[sub]0[/sub]² (cos a)²))x² is your original formula.. so.. V0 is your initial velocity so... all you have to do is make V0 the subject of the equation!! (rather you than me mate)
[color=#1C2837][size=2]

[color=#1C2837][size=2]

Share this post


Link to post
Share on other sites
[color="#1C2837"]y = x tan a - (g / (2v[sub]0[/sub]² (cos a)²))x²
[color="#1C2837"]
[color="#1C2837"]x tan a - y = [color="#1C2837"](g / (2v[sub]0[/sub]² (cos a)²))x²
[color="#1C2837"]
[color="#1C2837"](x tan a - y) / [color="#1C2837"]x² = [color="#1C2837"]g / (2v[sub]0[/sub]² (cos a)²)
[color="#1C2837"]
[color="#1C2837"]g / ([color="#1C2837"](x tan a - y) / [color="#1C2837"]x²) = [color="#1C2837"]2v[sub]0[/sub]² (cos a)²
[color="#1C2837"]
[color="#1C2837"] ([color="#1C2837"]g / ([color="#1C2837"](x tan a - y) / [color="#1C2837"]x²)) / 2 = [color="#1C2837"]v[sub]0[/sub]² (cos a)²
[color="#1C2837"]
[color="#1C2837"]SQUAREROOT ([color="#1C2837"]([color="#1C2837"]g / ([color="#1C2837"](x tan a - y) / [color="#1C2837"]x²)) / 2) = [color="#1C2837"]v[sub]0[/sub] (cos a)
[color="#1C2837"]
[color="#1C2837"]([color="#1C2837"]SQUAREROOT ([color="#1C2837"]([color="#1C2837"]g / ([color="#1C2837"](x tan a - y) / [color="#1C2837"]x²)) / 2)) / (cos a) = [color="#1C2837"]v[sub]0[/sub]
[color="#1C2837"][sub]
[/sub]
[color="#1C2837"]SO!! :
[color="#1C2837"]
[color="#1C2837"]v[sub]0 = [/sub][color="#1C2837"]([color="#1C2837"]SQUAREROOT ([color="#1C2837"]([color="#1C2837"]g / ([color="#1C2837"](x tan a - y) / [color="#1C2837"]x²)) / 2)) / (cos a)
[color="#1C2837"]
[color="#1C2837"]Right?
[color="#1C2837"]
[color="#1C2837"]I hope so. blink.gif

[EDIT[ I just noticed you want to do it at different elevations... I give up sad.gif

Share this post


Link to post
Share on other sites
The Wikipedia article has a sufficiently general nice formula for "Angle ? required to hit coordinate (x,y)" from (0,0).
Normally the velocity is fixed and the angle is variable, but you can still turn it around; negative or imaginary velocities aren't going to be acceptable solutions, just like angles through solid obstacles in the usual case.

However, in a game I'd rather simulate the actual projectile multiple times and apply a simple bisection procedure to guess the right speed: there would be no approximations, some error to keep the player guessing, and opportunity for variation of AI opponents that compute closer solutions (and perhaps react slowly), bracket the target between short and long shots, adjust more or less rapidly from their previous shots, etc.

Share this post


Link to post
Share on other sites
IMHO Andy's equation is easier to derive the answer from. It gives you distance traveled without regard to time. That being said, it's in the same Wikipedia article :)

y = (x * Tan [font=arial, helvetica, verdana, sans-serif]?[/font]) - (x[color=#1C2837][size=2]² * g) / (2 * v[size="1"][sub]0[/sub][color=#1C2837][size=2]² * Cos[color=#1C2837][size=2]² [font=arial, helvetica, verdana, sans-serif]?[/font])

From which you get:

v[sub]0[/sub] = Sqrt( ( (x[sup]2[/sup] * g) * (x * Tan [font=arial, helvetica, verdana, sans-serif]? - y[/font]) ) / (2 * Cos[font="arial, helvetica, verdana, sans-serif"][sup]2[/sup] [/font][font=arial, helvetica, verdana, sans-serif]?[/font]) )


Since x and y in the equation above are distance traveled, you will need to zero out your shooter's position, so given:


Shooter's position = (x[sub]0[/sub], y[sub]0[/sub])
Target's Position = (x[sub]1[/sub], y[sub]1[/sub])
Shooting angle = [font=arial, helvetica, verdana, sans-serif]?[/font]
Gravity = g

v[sub]0[/sub] = Sqrt( ( ( (x[sub]1[/sub]-x[sub]0[/sub])[sup]2[/sup] * g) * ( (x[sub]1[/sub]-x[sub]0[/sub]) * Tan [font=arial, helvetica, verdana, sans-serif]? - [/font](y[sub]1[/sub]-y[sub]0[/sub]) ) ) / (2 * Cos[font="arial, helvetica, verdana, sans-serif"][sup]2[/sup] [/font][font=arial, helvetica, verdana, sans-serif]?[/font]) )

Share this post


Link to post
Share on other sites
Not including air resistance has quite a massive impact on trajectory path. Air resistance is proportional to v[sup]2[/sup],
so velocity has a big impact on deacceleration.
With air resistance however you would need to simulate. However, you don't have to simulate real time. Usually,
a specific type of missile will be fired with one start velocity, and will have characteristic trajectory features.
You can use this to make an approximation to trajectory path dependant on what angle you fire from.
So you will have a look up table that finds the approximation for your specific missile (same shape, mass, start velocity).
I haven't tried, but I guess it's doable :).

edit:
Maybe I misunderstood what you meant. If there's a missile in the air, and you want to calculate where it will hit without knowing the velocity, you would have to determine the velocity. You can do that by sampling it's location over time, but that sounds like a bit overkill in a game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!