-------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--------------
scene = OgreKit.getActiveScene()
play = scene:getObject("plane")
logicM = OgreKit.LogicManager()
myLogicObject = logicM:getObject("plane")
mySensor = myLogicObject:getSensor("Collision")
maxVelocity=0.08
--print current plane posistion on screen
printPosition(play)
--using threshold to filter out unwanted small touch movement
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