Hi everyone!
I have stucked in the realizing game objects like walls, buildings and so on. My game movement logic is following - when joystick is pressed, new angle is calculating, and then whole other world objects is moving in the opposite direction. And so a player is always located in the center of the window. Everything was going perfect until I join to my project a physic objects. When hero collides with a wall, he should not move further, but I can’t understand how to handle this out. Should I use preCollision event to turn worldMovement off or somethink like this? I tried switch bodyType to find a better way, but nothing helped. Just tell in what direction I shoud moving on. Thank You!!
Code example:
-- SAMPLE COLLISION FILTERS local heroCollisionFilter = { categoryBits = 1, maskBits = 2 } local objectsCollisionFilter = { categoryBits = 2, maskBits = 1 } -- HELPS TO LOCATE OBJECTS window\_width = display.actualContentHeight window\_height = display.actualContentWidth -- PHYSICS ENGINE ON local physics = require ("physics") physics.start() physics.setGravity (0, 0) -- File with general functions. In this sample only getAngle() function used require ("function") -- In project this group consist information about all objects in the game local world = display.newGroup() -- Initialize objects for this project local circleMove, moveArea, hero, topBorder function move (obj, dx, dy) obj:translate(dx, dy) end function moveWorld (event) local dX, dY = getVector (world.direction, hero.speed) move (topBorder, -dX, -dY) end function world:startMove () world.enterFrame = moveWorld Runtime:addEventListener ("enterFrame", world) end function world:stopMove () Runtime:removeEventListener ("enterFrame", world) end -- When touch joystick calculate the angle, and give this information to moveWorld() function function onMoveTouch (event) world.direction = getAngle (moveArea.x, moveArea.y, event.x, event.y) circleMove.x = event.x circleMove.y = event.y if event.phase == "began" then world:startMove () end if event.phase == "cancelled" then print ("CANCELL") end if event.phase == "ended" then circleMove.x = moveArea.x circleMove.y = moveArea.y world:stopMove () end end -- Visualisation of sample inteface local background = display.newImage("background.png") local moveGroup = display.newGroup () moveArea = display.newRect (moveGroup, 0,0,10,10) moveArea:setFillColor (0,0,0,0) moveArea.height = window\_height / 2 moveArea.width = moveArea.height moveArea.x = moveArea.width / 2 moveArea.y = window\_height - (moveArea.height / 2) local circleMoveBorder = display.newImageRect (moveGroup, "circle.png", moveArea.width, moveArea.height) circleMoveBorder.x = moveArea.x circleMoveBorder.y = moveArea.y circleMove = display.newImageRect (moveGroup, "circle.png", moveArea.width/4, moveArea.height/4) circleMove.x = moveArea.x circleMove.y = moveArea.y moveArea:addEventListener ("touch", onMoveTouch) hero = display.newCircle (0,0, 10) hero:setFillColor (120, 44, 220) hero.x = window\_width / 2 hero.y = window\_height / 2 topBorder = display.newRect (-100, 100, window\_width + 200, 10) topBorder:setFillColor (0,0,0) -- The main trouble right here. What to do with collisions, should I use sensor or what? ---------------------------------------------------------------------------------------- hero.bodyType = "dynamic" hero.physicsOptions = { radius = 10, filter = heroCollisionFilter, } hero.speed = 4 physics.addBody (hero, hero.bodyType, hero.physicsOptions) topBorder.bodyType = "static" topBorder.physicsOptions = { density = 10, friction = 1.0, bounce = 0, isSensor = false, filter = objectsCollisionFilter } physics.addBody (topBorder, topBorder.bodyType, topBorder.physicsOptions) local function onCollision (self, event) -- MEET A HERO if (event.other.physicsOptions.filter.categoryBits == 1) then print (event.phase) if (event.phase == "began") then end if (event.phase == "ended") then end end end local function onPreCollision (self, event) end topBorder.collision = onCollision topBorder:addEventListener ("collision", topBorder) topBorder.preCollision = onPreCollision topBorder:addEventListener ("preCollision", topBorder)