I'm following the exact approach as Wizardry here. I've derived the final answer so you don't have to do as much work.
Lets see, you *know* the following:
start (throw) position (x0, y0)
end (hit) position (x1, y1)
lets say x is positive to the right, y positive up.
start angle, call it theta0 (in radians), measured from horizontal
gravity (-g in y direction)
You want to find the following:
tv = throwing velocity
th = time of hit
Your projectile motion path is given by these two scalar equations:
x(t) = x0 + tv*cos(theta0)*ty(t) = y0 + tv*sin(theta0)*t - g*t*t/2
Of course, you could write it as the two vector equations, with x acceleration component being 0.
The time, t, is measured from the time of the throw---so that the time of the throw is t = 0.
At the time of hitting your target, (time = th), you have
x(th) = x1 = x0 + tv*cos(theta0)*thy(th) = y1 = y0 + tv*sin(theta0)*th - g*th*th/2
There are two unknowns, th and tv. You have to solve the last two equations simulataneously. Start by solving the first equation for tv:
tv = (x1-x0)/(th*cos(theta0))
Then rewrite the second equation
y1 = y0 + (x1-x0)*sin(theta0)*th/(th*cos(theta0)) - g*th*th/2
Simplifying (note that th drops out of second term and
sin/cos = tan),
y1 = y0 + (x1-x0)*tan(theta0) - g*th*th/2
Or,
g*th*th/2 = y0-y1 + (x1-x0)*tan(theta0)
Solving for th,
th = sqrt((2/g) * (y0-y1 + (x1-x0)*tan(theta0)))
Take the positive square root since negative time isn't physically consistent here. If the discreminant of the sqrt term is negative then it means you *cannot* hit the target for the given throwing angle (i.e., you cannot hit a target at a higher position if object is thrown horizontally).
Then, from the earlier equation
tv = (x1-x0)/(th*cos(theta0))
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Edited by - grhodes_at_work on June 29, 2001 4:26:56 PM
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net