Here is the script. It has a lot from Roland's example for the 5580 Truck. Sorry, so far there are no comments inside.
Code:
function sineWave(time, freq, amp)
return math.sin(time*freq*2*math.pi)*amp
end
function register()
local ani=ldc.animation('Demo')
ani:setLength(20)
ani:setEvent('start', 'onStart')
ani:setEvent('frame', 'onFrame')
end
function onStart()
local mainSf=ldc.subfile()
doorUpper=mainSf:getGroup('doorUpper')
doorLower=mainSf:getGroup('doorLower')
doorUpperPos=doorUpper:getPos()
doorLowerPos=doorLower:getPos()
axleUpper=mainSf:getGroup('axleUpper')
axleLower=mainSf:getGroup('axleLower')
axleVert=mainSf:getGroup('axleVert')
pneu_cyl=mainSf:getGroup('pneumatic_cylinder')
pneu_pis=mainSf:getGroup('pneumatic_piston')
axleUpperRestOri=axleUpper:getOri()
axleLowerRestOri=axleLower:getOri()
axleVertRestOri=axleVert:getOri()
cylRestOri=pneu_cyl:getOri()
pisRestOri=pneu_pis:getOri()
pisPos=pneu_pis:getPos()
end
function calcDoors(displPerc)
local wavedisp= -50*math.cos(displPerc/100*math.pi)+50
local displ=64*wavedisp/100
doorUpper:setPos(doorUpperPos:getAdd(0,displ,0))
doorLower:setPos(doorLowerPos:getAdd(0,-displ,0))
local oriUpper=ldc.matrix()
local oriLower=ldc.matrix()
local oriVert=ldc.matrix()
local oriCyl=ldc.matrix()
local oriPis=ldc.matrix()
local angle=(360/16)/8*64*wavedisp/100
oriUpper:set(axleUpperRestOri)
oriUpper:mulRotateAB(angle,0,0,-1)
axleUpper:setOri(oriUpper)
oriLower:set(axleLowerRestOri)
oriLower:mulRotateAB(angle,0,0,1)
axleLower:setOri(oriLower)
local angleVert=16/24*(360/16)/8*64*wavedisp/100
oriVert:set(axleVertRestOri)
oriVert:mulRotateAB(angleVert,0,-1,0)
axleVert:setOri(oriVert)
local yPos=40*math.sin((30+angleVert)*math.pi/180)-20
local xPos=40*math.cos(30*math.pi/180)-40*math.cos((30+angleVert)*math.pi/180)
local lengthA=120
local lengthB=math.sqrt((120+xPos)^2+yPos^2)
local lengthC=math.sqrt(xPos^2+yPos^2)
local angleGamma=180/math.pi*math.acos((lengthA^2+lengthB^2-lengthC^2)/(2*lengthA*lengthB))
oriCyl:set(cylRestOri)
oriCyl:mulRotateAB(angleGamma,0,1,0)
pneu_cyl:setOri(oriCyl)
oriPis:set(pisRestOri)
oriPis:mulRotateAB(angleGamma,0,1,0)
pneu_pis:setOri(oriPis)
pneu_pis:setPos(pisPos:getAdd(yPos,0,xPos))
end
function calcUpDownAngle(curTime, ofs, len, minAngle, maxAngle)
if (curTime<=ofs) then
return minAngle
else
local endTime=ofs+len
if (curTime>=endTime) then
return minAngle
else
local relTime=curTime-ofs
local subLen=0.5*len
if (relTime<subLen) then
return minAngle+(maxAngle-minAngle)*relTime/subLen
else
return maxAngle-(maxAngle-minAngle)*(relTime-subLen)/subLen
end
end
end
end
function onFrame()
local ani=ldc.animation.getCurrent()
local curTime=ani:getFrameTime()
local totTime=ani:getLength()
calcDoors(calcUpDownAngle(curTime, 0, 20, 0, 100))
end
register()