Re-stacking of blocks have some unintended random behaviour

I have a stack of blocks that in certain cases is restacked to their default position. But when I do this, their x position seems to have a certain randomness that I do not want. This randomness seems to be dependent on the display resolution. If I run the code below on the  Galaxy III simulator the block number two from the bottom is acting up when the blocks are re-stacked.

local w = display.contentWidth local h = display.contentHeight local physics = require("physics") physics.start() physics.setScale(w/5) physics.setDrawMode("debug") -- Ground local ground = display.newRect(w/2, h-h/15, w/1.1, h/15) physics.addBody(ground, "static", { density=5.0, friction=0.3, bounce=0.0}) -- Add blocks local blockArr = {} local function placeBlocks()     for i = 1, 5 do         blockArr[i] = display.newRect(w - w/5, h-h/15 - 10 - i\*(55), 50, 50)         print(string.format("Block[%d](%d, %d)", i, blockArr[i].x, blockArr[i].y))         physics.addBody(blockArr[i], "dynamic", { density=2.0 - i/20, friction=0.9, bounce=0.0})     end end -- Clear blocks local function clearBlocks()     for i = #blockArr, 1, -1 do         physics.removeBody(blockArr[i])         blockArr[i]:removeSelf()         blockArr[i] = nil     end end local function clearAndReplaceBlocks()     clearBlocks()     placeBlocks() end timer.performWithDelay( 1500, clearAndReplaceBlocks, 20 )

I’m completely new to lua and I’m not sure if the clearBocks() and placeBlocks() is written well. It could easily be that I’m doing something stupid and have myself to thank.

I guess inaccuracies in the block placement has something to do with it, but what I really do not understand is why the first placement is always “perfect” and the later placements has this random nature.

Hi @runewinse,

For testing, try allowing a short pause between the clearing and re-creating of the blocks. For example, remove the “placeBlocks()” call from the “clearAndReplaceBlocks()” function. Then, after the for loop in the “clearBlocks()” function, add something like:

[lua]

timer.performWithDelay( 100, placeBlocks )

[/lua]

This may help the physics engine clear up “memory” of the blocks before it places a new set down.

Brent

Thanks! I find it a little odd that I have to do this, but it seems to work.

Hi @runewinse,

There are certain things in the physics engine which require one additional “frame” of the Runtime to operate. As you work more with physics, you’ll probably encounter a few of these. It’s during this frame that Box2D works out its internal processing and math, before it proceeds to the next frame. That being said, these instances are usually handled by triggering the event on a timer as short as 10 milliseconds, so you can probably change the 100 to 10 in your timer code.

Best regards,

Brent

Hi @runewinse,

For testing, try allowing a short pause between the clearing and re-creating of the blocks. For example, remove the “placeBlocks()” call from the “clearAndReplaceBlocks()” function. Then, after the for loop in the “clearBlocks()” function, add something like:

[lua]

timer.performWithDelay( 100, placeBlocks )

[/lua]

This may help the physics engine clear up “memory” of the blocks before it places a new set down.

Brent

Thanks! I find it a little odd that I have to do this, but it seems to work.

Hi @runewinse,

There are certain things in the physics engine which require one additional “frame” of the Runtime to operate. As you work more with physics, you’ll probably encounter a few of these. It’s during this frame that Box2D works out its internal processing and math, before it proceeds to the next frame. That being said, these instances are usually handled by triggering the event on a timer as short as 10 milliseconds, so you can probably change the 100 to 10 in your timer code.

Best regards,

Brent