Problem with physics.addBody

Hi,

I have noticed that physics.addBody gives me different results depends on first argument i.e. rect interact only with platform object. Why?

--main.lua local physics = require( "physics" ) physics.start() physics.setDrawMode( "hybrid" ) local \_T  = display.screenOriginY local \_B  = display.viewableContentHeight - display.screenOriginY local \_L  = display.screenOriginX local \_R  = display.viewableContentWidth - display.screenOriginX local \_CX = display.contentCenterX local \_CY = display.contentCenterY local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = \_CX platform.y = \_B-25 local rect = display.newRect(  200, 100, 100, 300 ) local bottomWall = display.newRect( \_CX, \_B- 120, \_L - \_R, 20 ) physics.addBody( platform, "static" ) physics.addBody( rect, 'dynamic', { bounce=0.3 } ) physics.addBody( bottomWall, "static" )

GIF:

hz7dQ1e.gif

Image comes from Chapter 1 — Creating an App.

I use Corona Simulator v. 2017.3184 on Win7 64-bit.

ldurniat

Hi.  Thanks for the code, but it is 100 times better to be able to click a link and download a working project.

I coded up a demo based on your pasted code:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/physicsissue.zip

The problems are: 

  1. Your math is wrong: _L - _R … should be _R - _L.

  2. You assumed it was correct and did not check it.

This is easy to miss, but you should always confirm you calculations. :slight_smile:

Try making this change, then look at the console:

 print( \_CX, \_B- 120, \_L - \_R, 20 ) local bottomWall = display.newRect( \_CX, \_B- 120, \_L - \_R, 20 )
  1. Download my code and run original(), then run alt()

For comparison, here is the alt() code:

local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = cx - fullw/2 local right = cx + fullw/2 local top = cy - fullh/2 local bottom = cy + fullh/2 local platform = display.newImageRect( "fillW.png", 300, 50 ) platform.x = cx platform.y = bottom - 25 local rect = display.newRect( 200, 100, 100, 300 ) print( cx, bottom - 120, fullw, 20 ) local bottomWall = display.newRect( cx, bottom - 120, fullw, 20 ) physics.addBody( platform, "static" ) physics.addBody( rect, "dynamic", { bounce=0.3 } ) physics.addBody( bottomWall, "static" )

I don’t believe it was so simple :) 

Thanks Ed:)

Ignoring my laziness (not wanting to make a project from your pasted code), this was a great post. Thanks for the details and nice clean organization

Hi.  Thanks for the code, but it is 100 times better to be able to click a link and download a working project.

I coded up a demo based on your pasted code:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/physicsissue.zip

The problems are: 

  1. Your math is wrong: _L - _R … should be _R - _L.

  2. You assumed it was correct and did not check it.

This is easy to miss, but you should always confirm you calculations. :slight_smile:

Try making this change, then look at the console:

 print( \_CX, \_B- 120, \_L - \_R, 20 ) local bottomWall = display.newRect( \_CX, \_B- 120, \_L - \_R, 20 )
  1. Download my code and run original(), then run alt()

For comparison, here is the alt() code:

local cx = display.contentCenterX local cy = display.contentCenterY local fullw = display.actualContentWidth local fullh = display.actualContentHeight local left = cx - fullw/2 local right = cx + fullw/2 local top = cy - fullh/2 local bottom = cy + fullh/2 local platform = display.newImageRect( "fillW.png", 300, 50 ) platform.x = cx platform.y = bottom - 25 local rect = display.newRect( 200, 100, 100, 300 ) print( cx, bottom - 120, fullw, 20 ) local bottomWall = display.newRect( cx, bottom - 120, fullw, 20 ) physics.addBody( platform, "static" ) physics.addBody( rect, "dynamic", { bounce=0.3 } ) physics.addBody( bottomWall, "static" )

I don’t believe it was so simple :) 

Thanks Ed:)

Ignoring my laziness (not wanting to make a project from your pasted code), this was a great post. Thanks for the details and nice clean organization