Thoughts on animation scripting language


Some progress (was re: Thoughts on ani...)
#30
Hi all,

I've been playing around with the scripting functionality this weekend after having taken a break for some time.

So I wanted to show the progress as I'm still open to suggestions on the api / core functions available to to end user.

Short clip of a test animation

This animation uses the new direct LDraw ref line access. This makes it possible to do some mass mutating fun stuff e.g. effecting every brick in a model.

The lua source code for this information is shown at the end of the clip (has clearer highlighting), but I'll give it here too:

Code:
function limit(v, min, max)

  if v<min then
    v=min
  end
  
  if v>max then
    v=max
  end
    
  return v;
end

function register()

  --global defs
  rcktTime=0;  --timespan (seconds) for rocket to get to target
  exTime=3;    --timespan for explosion (seconds)
  totTime=rcktTime+exTime;
  exSpeed=250;
  grav=250;
  
  --create/link animation obj
  local myAni=ldc.animation('Weapons test');
  myAni:setLength(totTime); --seconds
  myAni:setFPS(25); --low fps for screencap
  myAni:setEvent('play', 'preCalcDeps');
  myAni:setEvent('frameChange', 'calcFrame');
end

function preCalcDeps()

  --Link submodels and their main refs
  local sfMain=ldc.subfile();
  local refShack=sfMain:getRef('shack.ldr');
  sfShack=refShack:getSubfile();
  local refDude=sfMain:getRef('dude.ldr');
  local sfDude=refDude:getSubfile();
  local refWeapon=sfDude:getRef('rocketLauncher.ldr');
  
  --Link needed groups
  grpRocket=ldc.group('rocket');  
  local grpTarget=ldc.group('Group 1'); --helper grp so user can drag target around

  --get floor from first tree ref
  floor=sfMain:getRef('3470.dat'):getPos():getY();

  --target
  local impCen=ldc.vector(grpTarget:getPos());
  grpTarget:setVisible(false); --hide helper part(s)
  
  --point dude to target
  -- todo
  local ori=ldc.matrix();
  local angle=45;
  ori:setRotate(angle, 0, -1, 0);
  refDude:setOri(ori);
  
  --Prep the pos and direction vectors for all refs in the shack model
  refPos={}
  refDir={}
  refCnt=sfShack:getRefCount();
  for i=1, refCnt do
    refPos[i]=ldc.vector(sfShack:getRef(i):getPos());
    local tmp=ldc.vector(refPos[i]);
    tmp:sub(impCen);
    tmp:normalize();
    refDir[i]=ldc.vector(tmp);
  end  
end

function calcFrame()

  local curAni=ldc.animation.getCurrent();

  local curTime=curAni:getFrameNr()/curAni:getFrameCnt()*totTime;
  if curTime<rcktTime then
    --Rocket animation
  else
    --explode the shack
    for i=1, refCnt do
      local dx, dy, dz=refDir[i]:get();
      local x, y, z=refPos[i]:get();
      
      x=x + curTime*exSpeed*dx;
      y=y + curTime*exSpeed*dy + 0.5*grav*curTime*curTime;;
      z=z + curTime*exSpeed*dz;
      
      --x=limit(x, -500, 500);
      y=limit(y, -10000, floor);
      --z=limit(z, -500, 500);

      local ref=sfShack:getRef(i);
      ref:setPos(x, y, z);
    end
  end
end

register();
Reply
« Next Oldest | Next Newest »



Messages In This Thread
Some progress (was re: Thoughts on ani...) - by Roland Melkert - 2014-07-07, 2:24

Forum Jump:


Users browsing this thread: 1 Guest(s)