Physics problem

Why the cube passes  through the ground ??

local W = display.contentWidth local H = display.contentHeight local physics = require( "physics") physics.start()     local obj = display.newGroup()   local ground = display.newRect( W/2, H/2+800, 300, 500 ) physics.addBody( ground,"static",{} ) local cube = display.newRect( W/2, H/2, 100, 100 ) obj:insert( cube) physics.addBody(obj)  

There are a few problems with your code.

  1. You named your group ‘obj’ which isn’t necessarily a problem, but a curious name choice.  Others reading the code may find it confusing.

  2. On your last line, you try to add the body to ‘obj’ (your group), which is wrong.  Only (display) objects like rectangles, circles, images, and text can have physics bodies.  I think this was actually a typo and may relate to #1 above.

  3. You probably have a config.lua file, but without one, the sizes and positions you used were off screen when simulating without a config file.  Again, not an error, but hard to debug w/o changing.

I took your sample and made a few changes below.  You’ll find it works. as expected.  Cheers!

local W = display.contentWidth local H = display.contentHeight local CX = display.contentCenterX local CY = display.contentCenterY local physics = require( "physics") physics.setDrawMode( "hybrid" ) physics.start() local group = display.newGroup() local ground = display.newRect( 0, 0, W, 40 ) ground:setFillColor(0,1,0) ground.x = CX ground.y = H - 40 physics.addBody( ground,"static",{} ) local cube = display.newRect( 0, 0, 20, 20 ) cube.x = CX cube.y = 0 cube:setFillColor( 0, 0, 1 ) group:insert( cube ) physics.addBody( cube )

Just for fun, here is a more advanced version of your code utilizing several Lua Corona features you may find fun:

local W = display.contentWidth local H = display.contentHeight local CX = display.contentCenterX local CY = display.contentCenterY local mRand = math.random local physics = require( "physics") --physics.setDrawMode( "hybrid" ) physics.start() local layer = display.newGroup() local function newPBody( group, x, y, w, h, color, bodyType ) local bodyType = bodyType or "dynamic" local tmp = display.newRect( group, 0, 0, w, h ) tmp:setFillColor(unpack(color)) tmp.x = x tmp.y = y physics.addBody( tmp, bodyType, { bounce = 0.8 } ) return tmp end newPBody( layer, CX, H - 40, W, 40, { 0, 1, 0 }, "static" ) for i = 1, 24 do timer.performWithDelay( 100 \* i, function() newPBody( layer, i \* 20, 0, 10, 10, { mRand(0,1), mRand(0,1), mRand(0,1) } ) end ) end

There are a few problems with your code.

  1. You named your group ‘obj’ which isn’t necessarily a problem, but a curious name choice.  Others reading the code may find it confusing.

  2. On your last line, you try to add the body to ‘obj’ (your group), which is wrong.  Only (display) objects like rectangles, circles, images, and text can have physics bodies.  I think this was actually a typo and may relate to #1 above.

  3. You probably have a config.lua file, but without one, the sizes and positions you used were off screen when simulating without a config file.  Again, not an error, but hard to debug w/o changing.

I took your sample and made a few changes below.  You’ll find it works. as expected.  Cheers!

local W = display.contentWidth local H = display.contentHeight local CX = display.contentCenterX local CY = display.contentCenterY local physics = require( "physics") physics.setDrawMode( "hybrid" ) physics.start() local group = display.newGroup() local ground = display.newRect( 0, 0, W, 40 ) ground:setFillColor(0,1,0) ground.x = CX ground.y = H - 40 physics.addBody( ground,"static",{} ) local cube = display.newRect( 0, 0, 20, 20 ) cube.x = CX cube.y = 0 cube:setFillColor( 0, 0, 1 ) group:insert( cube ) physics.addBody( cube )

Just for fun, here is a more advanced version of your code utilizing several Lua Corona features you may find fun:

local W = display.contentWidth local H = display.contentHeight local CX = display.contentCenterX local CY = display.contentCenterY local mRand = math.random local physics = require( "physics") --physics.setDrawMode( "hybrid" ) physics.start() local layer = display.newGroup() local function newPBody( group, x, y, w, h, color, bodyType ) local bodyType = bodyType or "dynamic" local tmp = display.newRect( group, 0, 0, w, h ) tmp:setFillColor(unpack(color)) tmp.x = x tmp.y = y physics.addBody( tmp, bodyType, { bounce = 0.8 } ) return tmp end newPBody( layer, CX, H - 40, W, 40, { 0, 1, 0 }, "static" ) for i = 1, 24 do timer.performWithDelay( 100 \* i, function() newPBody( layer, i \* 20, 0, 10, 10, { mRand(0,1), mRand(0,1), mRand(0,1) } ) end ) end