Bookmarked it to read latter tonight. I want to have only the necessary code for each ship to come with the model, and the main lua in a seperately held tbm file. Course there needs to be some editing on whoever uses it, but only trivial adding a few lines.
The way I'm doing it right now every .01 of a second i adjust the rotation the some amount (total rotation / total amount of time [seconds])
#Conditional Hooks
$Application: FS2_Open
$On Game Init:
[
lastFrameTime = mn.getMissionTime ()
animationQueue = {}
function CreateOrientation (x, y, z)
return ba.createOrientation (math.rad(x),math.rad(y),math.rad(z))
end
function SetupAnimation(shipName, subsystemName, destOrientation, rotationTime)
if type(shipName) ~= "string" then
return false
end
if type (subsystemName) ~= "string" then
return false
end
local shp = mn.Ships[shipName]
-- Make sure we actually have a ship
if shp == nil then
return false
end
if shp:isValid() then
subsystem = shp[subsystemName]
-- Make sure we actually have a subsystem
if subsystem == nil then
return false
end
if subsystem:isValid () then
-- Simply add it to the animation queue
AddAnimationToQueue (shp, subsystem, destOrientation, rotationTime)
return true
end
end
return false
end
function AddAnimationToQueue (ship, subsystem, destOrientation, timeToLive)
totalNumberOfSteps = timeToLive * 100
animation = {}
animation.ship = ship
animation.subsystem = subsystem
animation.destOrientation = destOrientation
animation.runTime = timeToLive
-- Find the delta difference between the source and destination angles
animation.deltaX = destOrientation.p - subsystem.Orientation.p
animation.deltaY = destOrientation.b - subsystem.Orientation.b
animation.deltaZ = destOrientation.h - subsystem.Orientation.h
-- Number of degrees to move during each animation step
animation.deltaX = (animation.deltaX / totalNumberOfSteps)
animation.deltaY = (animation.deltaY / totalNumberOfSteps)
animation.deltaZ = (animation.deltaZ / totalNumberOfSteps)
table.insert (animationQueue, animation)
end
function ProcessAnimation (animation)
pitch = animation.subsystem.Orientation.p
bank = animation.subsystem.Orientation.b
heading = animation.subsystem.Orientation.h
newX = pitch + animation.deltaX
newY = bank + animation.deltaY
newZ = heading + animation.deltaZ
animation.subsystem.Orientation = ba.createOrientation (newX, newY, newZ)
end
--require (".\\scripts\\Bearcat.lua")
function LowerBearcatLandingGear (shipName)
SetupAnimation (shipName, "FrontLandingGear", CreateOrientation (-90, 0, 0), 1.5 )
SetupAnimation (shipName, "PortMainLandingGear", CreateOrientation (0, -90, 0), 1.5)
SetupAnimation (shipName, "StarMainLandingGear", CreateOrientation (0, 90, 0), 1.5)
SetupAnimation (shipName, "PortDoor01", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "PortDoor02", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "PortDoor03", CreateOrientation (0, -90, 0), .5)
SetupAnimation (shipName, "StarDoor01", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "StarDoor02", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "StarDoor03", CreateOrientation (0, 90, 0), .5)
SetupAnimation (shipName, "MissileBayPortDoor", CreateOrientation (0, -90, 0), 2.5)
SetupAnimation (shipName, "MissileBayPortDoor01", CreateOrientation (0, -90, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor", CreateOrientation (0, 90, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor01", CreateOrientation (0, 90, 0), 2.5)
end
function RaiseBearcatLandingGear (shipName)
SetupAnimation (shipName, "FrontLandingGear", CreateOrientation (0, 0, 0), 1.5 )
SetupAnimation (shipName, "PortMainLandingGear", CreateOrientation (0, 0, 0), 1.5)
SetupAnimation (shipName, "StarMainLandingGear", CreateOrientation (0, 0, 0), 1.5)
SetupAnimation (shipName, "PortDoor01", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "PortDoor02", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "PortDoor03", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor01", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor02", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "StarDoor03", CreateOrientation (0, 0, 0), 3.5)
SetupAnimation (shipName, "MissileBayPortDoor", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayPortDoor01", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor", CreateOrientation (0, 0, 0), 2.5)
SetupAnimation (shipName, "MissileBayStarDoor01", CreateOrientation (0, 0, 0), 2.5)
end
function OpenLandGear(ship)
--if (ship.Class.Name == "F-104D Bearcat") then
LowerBearcatLandingGear (ship)
--end
end
function CloseLandGear(ship)
--if (ship.Class.Name == "F-104D Bearcat") then
RaiseBearcatLandingGear (ship)
--end
end
]
$State: GS_STATE_GAME_PLAY
$On Frame:
[
-- Make sure animationQueue exists and isn't empty (no sense in processing nothing)
if (animationQueue == nil) then
return false
end
if (#animationQueue == 0) then
return
end
currentSecond = mn.getMissionTime ()
-- Make sure the game has moved by X amount of seconds
if (currentSecond < (lastFrameTime+.01)) then
return
end
for loop = 1, #animationQueue do
anim = animationQueue [loop]
-- Make sure the subsystem still exists
if not (anim.subsystem:isValid ()) then
table.remove (animationQueue, loop)
break
end
ProcessAnimation (anim)
-- Decrement the amount of time the animation has left to live
anim.runTime = anim.runTime - .01
if (anim.runTime <= 0) then
-- Animation must be done by now.. deleting it
table.remove (animationQueue, loop)
break
end
end
lastFrameTime = currentSecond
]
#End
Edit: Hmmm it looks like Orientation does return a local angle... If I grab the p,b & h from a subsystem before it starts rotating it's 0,0,0. But it rotates like it's rotating on the parent objects axis instead.
double edit: For the fun of it I put in the ship space angles in and the results were definitely not right! So I'm not sure what exactly is going wrong.
triple edit: Just to see where the rotation goes, I rotated one of the bay doors into a completely different orientation, but the script is still applying the same rotation as before, so it's definitely not using the subobjects local.