Falling bodies - LUA

Dear contributers,

I’ve a question : Is it possible to make collision between objects without making statics “walls” ? I mean, the  object, physic body, can be pushed out of the screen and coming back from the other part of the screen ?

These is my code, i put 4 walls, but once I removed them, the “bodies” disappeared at all :

 local storyboard = require "storyboard" local scene = storyboard.newScene() storyboard.purgeOnSceneChange = true local physics = require("physics") physics.start() system.activate( "multitouch" ) physics.setDrawMode("hybrid") physics.setGravity(0,0) display.setStatusBar(display.HiddenStatusBar) local leftWall = display.newRect( 1, 0, 1, 1000 ) local rightWall = display.newRect(display.contentWidth, 0,1, 1000) local top = display.newRect( 1, 1, display.contentHeight+500, 0 ) local bottom = display.newRect( 1, display.contentHeight, display.contentWidth+500, 1 ) physics.addBody(leftWall, "static", { bounce = 1}) physics.addBody(rightWall, "static", { bounce = 1}) physics.addBody(top, "static", { bounce = 1}) physics.addBody(bottom, "static", { bounce = 1}) local background = display.newImage ("bg.png") background.x = centerX background.y = centerY local blueDest = display.newImageRect("blue\_hidden.png",150,100) blueDest.x = 200 blueDest.y = 200 local greenDest = display.newImageRect("green\_hidden.png",150,100) greenDest.x = 200 greenDest.y = 300 local violetDest = display.newImageRect("violet\_hidden.png",100,100) violetDest.x = 225 violetDest.y = 250 local blue = display.newImageRect("blue.png",150,100) blue.x = display.contentWidth/2.5 blue.y = display.contentHeight/2.5 blue.dest = blueDest physics.addBody(blue,"dynamic", {bounce=1,density=2,radius=50, friction=0}) blue.gravityScale = 0.25 local green = display.newImageRect("green.png",150,100) green.x = 300 green.y = 300 green.dest = greenDest physics.addBody(green,"dynamic", {bounce=1,density=2,radius=50, friction=0}) green.gravityScale = 0.25 local violet = display.newImageRect("violet.png",100,100) violet.x = (display.contentWidth/3)+20 violet.y = (display.contentHeight/2)-30 violet.dest= violetDest physics.addBody(violet, "dynamic",{bounce=1,density=2,radius=50, friction=0}) violet.gravityScale = 0.25 local shapes = {blue, green, violet} local timeLimit = 10 timeLeft = display.newText(timeLimit, 160, 100, native.systemFontBold, 50) timeLeft:setTextColor(255,0,100) local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit --if(timeLimit==0)then -- storyboard.gotoScene( "endtime") --end end timer.performWithDelay(1000,timerDown,timeLimit) function scene:createScene( event ) blue:setLinearVelocity(0,-200); green:setLinearVelocity(math.random(-100,300), math.random(-100,300)); violet:setLinearVelocity(math.random(-100,300), math.random(-100,300)); local function shapeTouch( event ) local self = event.target if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object self.focus = true display.getCurrentStage():setFocus( self ) elseif self.focus then if event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above if math.abs(self.x - self.dest.x) \< 20 and math.abs(self.y - self.dest.y) \< 20 then transition.to(self, { time=200, x = self.dest.x, y = self.dest.y }) physics.removeBody(self) self.done = true local done = true for i, v in pairs(shapes) do done = done and v.done end if done then local alert = native.showAlert( "You are in scene1!", "Congratulations!", { "OK" }) -- storyboard.gotoScene("level02") end end elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil) --physics.addBody(self, "dynamic",{bounce=1,density=2,radius=50, friction=0}) self.focus = false end end return true end blue:addEventListener( "touch", shapeTouch ) green:addEventListener( "touch", shapeTouch ) violet:addEventListener( "touch", shapeTouch ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) return scene

Any advice ?

Thank you :slight_smile:

Yeah, just set the object’s x (or y) value to the far left (or top) value you want when it goes too far right (or bottom).    

Yeah, just set the object’s x (or y) value to the far left (or top) value you want when it goes too far right (or bottom).