I’ve converted the code on this page into Lua:
http://nial.me/2012/09/calculating-bullet-trajectories/
The output is strange, seems to show that Lua operators don’t work. In JS this works…
local gravity = 9.8
function calculateLaunchAngle(position, target, velocity)
local targetX = target.x - position.x
local targetY = -(target.y - position.y)
local r1 = math.sqrt(math.pow(velocity, 4) - gravity*(gravity*(math.pow(target.x, 2)) + ((2*target.y)*(math.pow(velocity,2)))))
if (r1) then
local a1 = (math.pow(velocity, 2) + r1)/(gravity*target.x)
local a2 = (math.pow(velocity, 2) - r1)/(gravity*target.y)
a1 = -math.atan(a1)
a2 = -math.atan(a2)
if (targetX < 0) then
a1 = a1 - 180/180*math.pi
a2 = a2 - 180/180*math.pi
end
return a1 – or a2
end
return null
end
function calculateTrajectory( launchAngle, launchVelocity, time )
local vx = launchVelocity * math.cos(launchAngle) – initial velocity in x
local vy = launchVelocity * math.sin(launchAngle) – initial velocity in y
local x = (vx * time)
local y = (0.5 * gravity * time + vy) * time
return {x=x, y=y}
end
local angle = calculateLaunchAngle( {x=0, y=0}, {x=10,y=10}, 15 )
print( angle ) – not good output