Physics accuracy

Hi,

I started trying out Corona 2 hours ago so I’m kind of green and this is probably a dumb question, but here goes…

I’m not entirely satisfied with how the physics seems to work in the example below:

local physics = require("physics") physics.start() local ground = display.newRect(display.contentWidth / 2, display.contentHeight-20, display.contentWidth, 3) physics.addBody(ground, "static", { density=1.0, friction=0.5, bounce=0.0}) local function spawnCoins()     local coin = display.newImage("coin.png")     coin.x = math.random(display.contentWidth/8) + display.contentWidth/2      coin.y = -50     physics.addBody(coin, { density=2.0, friction=0.3, bounce=0.3}) end timer.performWithDelay(250, spawnCoins, 15)  

The result of this code is a pile of coins, but more often than not many of them are floating a bit above the coin below for some reason (see the physics.jpg image).

The coin.png image is 60x60 pixels and I’ve set the physics radius to 30. I would have thought that this would ensure some quite nice collisions.

Am I making a simple mistake or is the coin.png image wrong or is this just what we can expect from the physics engine?

Use the below flag in the beginning of your main.lua file to see the actual physics outlines from your objects:

physics.setDrawMode("hybrid")

Hi @runew,

These coins are round objects, but your actual internal physics bodies are rectangular (default). You need to tell Corona (Box2D) to use a radial-shaped body on these objects. This is done by adding a radius property to your body declaration (in the properties table):

[lua]

physics.addBody( coin, { radius=20, density=2.0, friction=0.3, bounce=0.3 } )

[/lua]

The value of this should match (or come close) to the visual size of the coins, which you can check by inspecting the hybrid mode as @Panc mentions.

Take care,

Brent

Thanks a lot - both of you  :slight_smile:

Use the below flag in the beginning of your main.lua file to see the actual physics outlines from your objects:

physics.setDrawMode("hybrid")

Hi @runew,

These coins are round objects, but your actual internal physics bodies are rectangular (default). You need to tell Corona (Box2D) to use a radial-shaped body on these objects. This is done by adding a radius property to your body declaration (in the properties table):

[lua]

physics.addBody( coin, { radius=20, density=2.0, friction=0.3, bounce=0.3 } )

[/lua]

The value of this should match (or come close) to the visual size of the coins, which you can check by inspecting the hybrid mode as @Panc mentions.

Take care,

Brent

Thanks a lot - both of you  :slight_smile: