Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with physics.addBody
Started by ldurniat Jun 03 2018 10:46 AM

4 replies to this topic
addbody physics

Best Answer roaminggamer , 03 June 2018 - 11:35 AM

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. :)

 

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 )

3. Download my code and run original(), then run alt()

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 352 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,917 posts
  • Corona SDK

  Best Answer

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. :)

 

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 )

3. Download my code and run original(), then run alt()



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,917 posts
  • Corona SDK

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" )


[TOPIC: post.html]
#4

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 352 posts
  • Corona SDK

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

I don't believe it was so simple :) 

 

Thanks Ed:)



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,917 posts
  • Corona SDK

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


  • ldurniat likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: addbody, physics