If I have a point which I want to hit at the end or during a parabolic arc, how would I calculate the needed x and y velocity?
Parabolic trayectory function is defined as:
Fx = Vox*t + Ox; Fy = -0.5 * g * t * t + Voy*t + Oy;
P: the target point. O: the origin point. g: gravity. t: time needed to impact.
Vo: Initial Velocity
To calculate 'Vo', we can give values to F function :
't' = flight time 'duration' 'F' = target point 'P' (Px-Ox) Vox = -------- duration Py + 0.5* g * duration * duration - Oy Voy = --------------------------------------- duration
You can now get all the values to reach the target from the origin giving values to t into the F equation:
When t = 0 => F == O (Origin) When t = duration => F == P (Target)
I recently had to solve a similar problem, i came up with two solutions, based on the formula i found on the wikipedia page 'Dan the Man' already mentioned: Trajectory of a projectile
In this solution you acctually need eitherway the launch angle fixed or the x velocity. Y velocity is not needed as we launch the projectile in a specific angle.
Solution 1, launch angle is fixed, calculate the velocity:
g = 9.81; // gravity x = 49; // target x y = 0; // target y o = 45; // launch angle v = (sqrt(g) * sqrt(x) * sqrt((tan(o)*tan(o))+1)) / sqrt(2 * tan(o) - (2 * g * y) / x); // velocity
Solution 2, velocity is fixed, calculate launch angle:
g = 9.81; // gravity v = 40; // velocity x = 42; // target x y = 0; // target y s = (v * v * v * v) - g * (g * (x * x) + 2 * y * (v * v)); //substitution o = atan(((v * v) + sqrt(s)) / (g * x)); // launch angle
In my case this solutions worked quite well.
If you don't care if it is mathematically correct, only that it looks correct enough, calculate the straight path and make your projectile follow that path, but "push it up" along the normal of that line as a function of it's distance down the line segment, so it rises as it approaches the middle of the segment and falls as it goes away from the middle of the line segment.
You could use a sine wave for this, using the range of degrees from -90 to +90 (where -90 is the left point on the line segment, 90 is right point, and you lerp in the middle), and multiply the result by a constant to scale it up.
If you need the purely correct mathematical / physical answer this won't help. If you don't, this can probably work pretty well for you!
Don't forget, game programming is about using illusions that look correct (and are cheaper to compute), instead of realism.
If you just need something that is just about right and have a fixed velocity you can use this very simplified method.
distance = to.x - from.x; angleToPoint = atan2(to.y - from.y, to.x - from.x); distanceFactor = 1/1000; angleCorrection = (PI*0.18) * (distance * distanceFactor); velocity.X = cos(angleToPoint+angleCorrection) * power; velocity.Y = sin(angleToPoint+angleCorrection) * power;
Distance can be negative but it will still works because the angle depends on the distance too. If the distance is negative the angle that needs to be added is negative too.
You'll need to play around with distanceFactor to find the right value. It depends on the gravity and power of the projectile. It should be near 1 divided through the maximum distance the projectile can cover.