Saturday, September 28, 2013

Blender Gamekit Lua Constrain Plane Side Movement

Blender 3D game engine provides location constraint to limit object in a certain area. However this was not implemented in the Gamekit package. I have to do some scripting to get around it. So my plane will not fly out of the screen. Below is the script and red bits are my explanations. This is apart of series tutorials to create a 2d arcade plane game using Blender 3D and gamekit. Check this blog for more details.


-------BELOW IS OnInit.lua--------------

--Global variable
    --set touch move parameters
    Sensitivity = 0.005
    --invert = -1 -> is inverted
    Invert = 1
    Threshold=3.5
 -- Plane Constrain parameter
    Xmin = -0.87 --This is the left boundary, adjust if necessary
    Xmax = 0.87 --This is the right boundary, adjust if necessary
--this function is to stop plane moving cross the boundary
function xMoveFilter(limitSignal,oldX)
    if limitSignal == -1 and oldX > 0 then
        return oldX
    elseif limitSignal == -1 and oldX < 0 then
        return 0
    elseif limitSignal == 1 and oldX >0 then
        return 0
    elseif limitSignal ==1 and oldX <0 then
        return oldX
    end
        return 0
end
--this function is to determine whether plane is over the broundary
function XoverLimit(obj)
    local vec3 = obj:getPosition()
    if vec3.x < Xmin then
        return -1
    elseif vec3.x > Xmax then
        return 1
    else
        return 0
    end
end
--this function is for debug used to display plane position
function printPosition(obj)
    local vec3 = obj:getPosition()
    dPrintf("position x: " .. vec3.x)
    dPrintf("position y: " .. vec3.y)
    dPrintf("position z: " .. vec3.z)
    
end    
function velocityCap(v,vmax)
    if (v>vmax) then
        return vmax
    elseif (v < -vmax) then
        return -vmax
    else return v
    end
end

-------BELOW IS planeTouchMove.lua--------------

mouse=OgreKit.Mouse()
scene = OgreKit.getActiveScene()
play = scene:getObject("plane")
logicM = OgreKit.LogicManager()
myLogicObject = logicM:getObject("plane")
mySensor = myLogicObject:getSensor("Collision")
maxVelocity=0.08


mouse:capture()
--print current plane posistion on screen
printPosition(play)
--using threshold to filter out unwanted small touch movement

if ((mouse.xrel>Threshold or mouse.xrel<-Threshold)or(mouse.yrel>Threshold or mouse.yrel<-Threshold)and (not(play.touchBoundary))) then
      velocityY=velocityCap((mouse.yrel) * Sensitivity, maxVelocity)
      velocityX=velocityCap((mouse.xrel) * Sensitivity, maxVelocity)
      --check if plane over screen boundary 
      limitSignal = XoverLimit(play)
      --the X Y directions are swapped as the screen is in landscape
      if limitSignal==0  then
      --when plane is inside boundary
        play:translate(-velocityY* Invert,-velocityX * Invert,0)
      else
      --when plane touches boundary it can only move away from boundrary
        newY = xMoveFilter(limitSignal,-velocityY * Invert)
        play:translate(newY,-velocityX * Invert,0)
      end
end




    

No comments:

Post a Comment