Here some code snippets I ripped out of one of my scripts. The code was used to calculate the new projectile position based on frame time. This would terminate different projectile speeds based on FPS.
-- add function findRotation
local findRotation = function ( x1, y1, x2, y2 )
local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) )
return t < 0 and t + 360 or t
end
-- add function findPitch
local findPitch = function (camerax,cameray,cameraz,pointx,pointy,pointz)
local dX=camerax-pointx
local dY=cameraz-pointz
local dZ=cameray-pointy
local pitch=math.atan2(dY,math.sqrt(math.pow(dZ,2) + math.pow(dX,2)));
return pitch
end
local xr, yr, zr = getElementRotation(source, "ZXY") -- source is projectile
-- xr, yr, zr is saved inside of projectileData.rotation
local targetX, targetY, targetZ = getElementPosition(target) -- target is projectile
local rotZ = findRotation(newX, newY, targetX, targetY) + 180
local pitch = findPitch(newX, newY, newZ, targetX, targetY, targetZ)
local rotX = pitch*(180/math.pi)
local newRotX = projectileData.rotation.x
local newRotZ = projectileData.rotation.z
local rotationDistanceX = (projectileData.rotation.x - rotX + 360) % 360;
if (rotationDistanceX > 180) then
rotationDistanceX = 360 - rotationDistanceX
newRotX = newRotX + math.min(rotationDistanceX, behaviourData.rotationSpeed * speedMultiplier)
else
newRotX = newRotX + -math.min(rotationDistanceX, behaviourData.rotationSpeed * speedMultiplier)
end
local rotationDistanceZ = (projectileData.rotation.z - rotZ + 360) % 360;
if (rotationDistanceZ > 180) then
rotationDistanceZ = 360 - rotationDistanceZ
newRotZ = newRotZ + math.min(rotationDistanceZ, behaviourData.rotationSpeed * speedMultiplier)
else
newRotZ = newRotZ + -math.min(rotationDistanceZ, behaviourData.rotationSpeed * speedMultiplier)
end
projectileData.rotation.x = newRotX
projectileData.rotation.z = newRotZ
local rotationZ = -projectileData.rotation.z
if creatorVehicle then -- if the projectile creator was a vehicle, then add 180 to invert the rotation.
rotationZ = rotationZ + 180
end
setElementRotation(element, projectileData.rotation.x, projectileData.rotation.y, rotationZ, "ZXY")