Advertisement
suremarc

Untitled

Sep 29th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. function trajectory(start, target, speed, gPrime)
  2.     local dv = target - start;
  3.     -- displacement
  4.     local dx, dy, dz = dv.x, dv.y, dv.z;
  5.     -- horizontal displacement
  6.     local alpha, beta = sqrt(dx*dx+dz*dz), dy;
  7.     local f = 2*speed*speed / g;
  8.     -- discriminant
  9.     local d = f*(f - 2*beta) - 2*alpha*alpha;
  10.     if d<0 then
  11.         print "can't hit target";
  12.         return nil;
  13.     else
  14.         local root = sqrt(d);
  15.         local gamma0, gamma1 = (f+root)/2, (f-root)/2;
  16.         local theta0, theta1 = atan2(gamma0, alpha), atan2(gamma1, alpha);
  17.         return  theta0, Vector3.new(dx/alpha*cos(theta0), sin(theta0), dz/alpha*cos(theta0))*speed,
  18.                 Vector3.new(dx/alpha*cos(theta1), sin(theta1), dz/alpha*cos(theta1))*speed;
  19.     end
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement