Showing posts with label game design. Show all posts
Showing posts with label game design. Show all posts

Saturday, November 9, 2013

Blender3D GameKit Lua Plane Top Bottom Constraint



The top bottom constraint is a bit tricky. As camera keeps moving with plane. I used camera as a reference to calcuate the top and bottom screen edge.
1. Get camera position
2. Calculate current top edge Y location
3. Calculate current bottom edge Y location
4. Compare with current plane location, and return signals, -1 as plane too low, 1 as plane too high, 0 for plane inside the screen
5. feed the signal and mouse movement to function edgeMoveFilter, which only permit plane move toward centre if it is on edge
The code is below and red marks are comments.
******Oninit.lua****************
    --Global variable
    --set touch move parameters
    Sensitivity = 0.005
    --invert = -1 -> is inverted
    Invert = 1
    Threshold=3.5
 -- Plane Contrain
    Xmin = -0.87
    Xmax = 0.87
 -- Plane Top Bottom Constrain
    Ymin = -1.50
    Ymax = 1.30
--filter movement so plane will only move towards center if touchs edge
function edgeMoveFilter(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   

--check if over boundary on Y axis
function YoverLimit(obj,cameraObj)
    local vec3 = obj:getPosition()
    local cameraV = cameraObj:getLinearVelocity()
    --get camero position
    local cameraP = cameraObj:getPosition()
    --calculate bottom edge ;ocation
    local nextYmin = cameraP.y+Ymin
    --calculate top edge location
    local nextYmax = cameraP.y+Ymax
    --check whether plane is on edge and return signals
    if vec3.y < nextYmin then
        return -1
    elseif vec3.y > nextYmax then
        return 1
    else 
        return 0
    end
end

--endNew-----------
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
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

******planeTouchMove.lua****************  

mouse=OgreKit.Mouse()
scene = OgreKit.getActiveScene()
play = scene:getObject("plane")
--new--get camera objcet
camera = scene:getObject("Camera")
--end new---------------------------
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) 
      --new----------------------
      --check if plane over screen boundary 
      limitSignalX = XoverLimit(play)
      limitSignalY = YoverLimit(play,camera)
      --the X Y directions are swap as the screen is in landscape
      if limitSignalX==0  and limitSignalY == 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 = edgeMoveFilter(limitSignalX,-velocityY * Invert)
        newX = edgeMoveFilter(limitSignalY,-velocityX * Invert)
        play:translate(newY,newX,0)
      end
      --end new---------------------
end


  


    

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




    

Tuesday, August 20, 2013

How to Test Blender 3D Gamekit on Android Device


You need to install adb on your computer and eclipse First.
The Android SDK contains the Android debug bridge (adb) which is a tool which allows you to connect to a virtual or real Android device for the purpose of managing the device or debugging your application. Check links below
http://developer.android.com/sdk/index.html
http://developer.android.com/sdk/installing/installing-adt.html


1. set your android device to debug mode, in the video is a google nexus 7
2.connect usb to your desktop
3.open eclipse and open the blender 3d gamekit android package, this package can be download at:
4.pack and save your blender file, test on desktop.
5.copy your blender file into the asset folder under the eclipse project
6.refresh asset to confirm you've got it.
7.go to main.java change file name to your blender file name
8.click run, the apk will be send to your device and should be running in several minutes.
The android package can be downloaded from:

Sunday, July 21, 2013

How to do Background Rolling On Mobile Device Using Blender & Gamekit

This demo showing how to create a rolling background as seen on arcade platform 2D/2.5D games using blender3D and gamekit. at the end test on a real android device Nexus 7.
Following are the Steps:
1. Create a 'mesh plane' rectangular as background
2. Move and Rotate Camera
3. Import pre-modelled Plane
4. Add shadeless material to background
5. Unwrap UV,1024 x 1024
6. export UV image
7. stitch background image on UV map in photoshop or gimp2
8. save the image
9. load image into texture under material
10. add game logic to camera and airplane
11. pack external data in file and save file
12. test on blender gamekit add-on using desktop
13. copy file into gamekit android package asset folder
14.open eclipse, change the file name reference in the main.java file
15. connect nexus 7 and run the test.

the blender file can be download at