2

I am programming a simple ball projectile in a game. the update pretty much looks like:

velocity += gravity;
velocity *=0.9;
pos += vel;

Is there a way to set the angle and power of the launch in order to hit a point that is specified with the mouse? like peggle, http://youtu.be/4KbNiWsgJck?t=45s

I know there is a solution that I have used several years ago, but I can't find it. I believed it turned my update into a quadratic formula, or derived it or something. It had two solutions that was solved with the quadratic equation.

ps- hopefully this could be in 3D, but I could use a 2D solution too because my curve would be 2D

any help? thanks, Dan

DJFountain
  • 31
  • 3
  • In that video it looks as if the user controls angle, but not power (at least at first-- I'm not going to sit through the whole thing). Is that what you want? – Beta Jan 07 '13 at 16:53
  • There is no single solution to both velocity and angle but rather a whole family of parametric solutions of the velocity as a function of the angle or vice versa. As for the 2D vs 3D, the actual trajectory is really 2D - it lies completely in a vertical plane (unless there is side wind blowing or other side force). – Hristo Iliev Jan 09 '13 at 17:41

1 Answers1

1

Yes, you can do this. If you can change the angle and speed, you have more variability than you need, so you have to find a reasonable set of parameters that will work, which isn't hard. The basic equations are:

x = x0 + t*v0x
y = y0 + v0yt + (1/2)ayt2

Here, x and y will be the points you want to hit, and t will be the time that you hit them. t won't show up in the final solution, but you'll use it as in intermediary to calculate the values you want.

Basically, then, pick a reasonable value for v0x. Using the x-equation, find what t will be when the target is hit. Then plug this value into the y-equation, and solve for v0y. This then will give you a pair of values of v0x and v0y that will work to hit the target.

tom10
  • 67,082
  • 10
  • 127
  • 137