Skip to content

Instantly share code, notes, and snippets.

@nothings
Last active April 25, 2016 23:24
Show Gist options
  • Select an option

  • Save nothings/692256b3c97619570f66fe1d350569cd to your computer and use it in GitHub Desktop.

Select an option

Save nothings/692256b3c97619570f66fe1d350569cd to your computer and use it in GitHub Desktop.
// x = vx*t
// y = vy*t - 0.5g*t^2
//
// vx = x/t
// vy = (y+0.5g*t^2)/t
//
// vx*vx + vy*vy = v0*v0
//
// v0*v0 = (x/t)^2 + ((y+0.5g*t^2)/t)^2
// v0*v0*t^2 = x^2 + (y+0.5g*t^2)^2
//
// Let s = t^2
//
// v0*v0*s = x^2 + (y+0.5g*s)^2
// = x^2 + y^2 + 2*y*0.5*g*s + 0.25*g^2*s^2
// v0*v0*s = x^2 + y^2 + y*g*s + 1/4*g^2*s^2
//
A = 0.25*g^2
B = -v0*v0 + y*g
C = x^2 + y^2
// s = (B +- sqrt(B^2-4AC))/2A
// multiple solutions correspond to shooting up or shooting down?
// favor the slower path to avoid negative s, but could use logic
// to test both cases and take faster non-negative case
s = B + sqrt(B^2-4*A*C)/(2*A)
t = sqrt(s)
vx = x/t
vy = (y+0.5*g*t*t)/t
theta = atan2(vy,vx) // may need to negate one of these
// theta is in radians
// no further expansion of the math is needed since this is going into a
// computer program, so it can compute A,B,C,s,t,x as above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment