Why do perfectly aligned stacked up boxes tumble and fall?

I’ve been running into this problem over and over: perfectly stacked up, simple-rectangular shaped, non-rotated boxes etc. often start to move on the ground, stumble, and finally fall – even if it’s only say 5 boxes or so. How can I prevent this without “securing” the stacked tower in such a way that it won’t be fun anymore for the player to kick it over? In other words, increasing angularDamping etc. too much would get rid of the whole game play because the boxes *are* supposed to tumble when the player hits them. I’ve tried playing around with density, friction, physics.setScale, even a slight default rotation to the right to work against what seems like a world slightly skewed left-wards, but no approach works great so far. Sigh. Anyone have a solution?

Thanks!! [import]uid: 10284 topic_id: 14847 reply_id: 314847[/import]

A few possibilities:

object.isFixedRotation = true

or create temp weld joints between boxes (that can be broken upon kicking the blocks or whatever your doing [import]uid: 84637 topic_id: 14847 reply_id: 54866[/import]

Danny, thanks for the reply. Unfortunately I needs these boxes to be (potentially) rotating & lose. I just don’t understand why two perfectly aligned boxes would always slide to a certain direction. It helps a bit if I set global gravity to physics.setGravity(1.1, 9.8) (!), but it helps only a bit and I’m still looking for a real solution. I might give weld joints a try, thanks for this suggestion. [import]uid: 10284 topic_id: 14847 reply_id: 54889[/import]

as a potential work around, how about 2 x thin, static/kinematic, invisible physics objects. One either side of the stack which simply delete on collision?

That way it’d all remain upright until it needed to react.

Guess it depends on exactly how your app works; if the stack changes/grows/moves etc or if it’s the same each time. [import]uid: 67933 topic_id: 14847 reply_id: 54890[/import]

Sounds similar to a game we’re about to release, are the boxes on something that moves? [import]uid: 87611 topic_id: 14847 reply_id: 54892[/import]

Well, the boxes were just an example. I did have a game which had boxes before though which had the same issues. My current game actually has tables with vases, consisting of e.g. two table legs, a table plate, and vases stacked on top. I can’t stack as much as I want on to it due to these issues mentioned, and even if I scale down the concept, the tables sometimes slide along the floor and then tumble… and that’s even when I use quite a few workarounds like extra friction, skewed rotation, and fake gravity, all which help a bit but not completely… [import]uid: 10284 topic_id: 14847 reply_id: 54927[/import]

hmm, I know they are looking at improving collisions in a future release, but I realise that doesn’t help much now.

Do you have any drag and drop code to test?
[import]uid: 67933 topic_id: 14847 reply_id: 54946[/import]

I have had issues like this as well. Have you tried position iterations? The default is 8, but you can set them to something like 32, it’s a performance hit but really helps out with physics behaving more properly.
I was having issues with a rotating windmill, and a ball. The ball would fall on the windmill and “stick” or just go through it if the windmill was rotating to fast. A couple of helpful people on here (darkmod, technowand and another guy with weird spelling I can’t remember right now) suggested I do this:

physics.setPositionIterations(32)  

Also, someone suggested using the isBullet as well on the physics bodies which would be subjected to constant collision.

That really helped me out, so I don’t know if it will for you or not, but hey worth a shot?

ng

[import]uid: 61600 topic_id: 14847 reply_id: 54957[/import]

Hmm ok here we go,
You are saying that a stack of perfectly aligned boxes keep moving?

Here is my code I just spent the last 30 minutes putting together.

Some notes:

***View as Iphone 4*****
If you set the friction lower, the weight from the top pieces in the multiple row stack will slide out. Also keep in mind that box2d rounds math so things are never quite exact (just have to live with it).

Also, you will see *some* movement of some of the boxes briefly when you launch, but then they settle down. Not sure why, it could be due to rounding in Box2d or something.

I placed a big ball in there as well (crashobject) you can comment it out to see it crash and destroy things which is always fun!

If you comment out the position iterations as well, things will start moving and acting odd.

Also note, I am really kind of lame at coding, only started programming in June 2011, never touched anything before. I could put things in groups, and be more organized but this was just to see what you were saying and put it into action.

You could stack another 4 rows and I believe it would still be the same, as long as you line things up. Also, if you are using Images, remember things have to be PERFECTLY squared off, straight lines on bottom or things act weird!

Anyway, here is the code it’s plug in play. :slight_smile:

ng

[code]

display.setStatusBar( display.HiddenStatusBar )
local physics = require (“physics”)
local _W = display.contentWidth
local _H = display.contentHeight
–physics.start()
physics.start( true )
physics.setScale( 60 )
physics.setGravity( 0, 9.8 )
physics.setPositionIterations(32)
–comment out the crashobject to see just the stacks by themselves

local crashobject = display.newCircle( 450, 100,50,50)
physics.addBody( crashobject , { bounce = 0 , friction = 5 , density = 1, } )
crashobject:setFillColor(0, 255, 255)
–]]

local ground = display.newRect( 0, 850 , 640 , 50 )
physics.addBody( ground, “static” ,{ bounce = 0 , friction = 5 , density = 1 } )

–Bottom row of boxes, spread 50x50 size, spread out 75 pix apart, alternate colors/ red / blue
–Row 1 is numbered 11, 12, 13
–Row 2 is numbered 21, 22, 23 etc etc etc :slight_smile:

local box11 = display.newRect( 50, 800,50,50)
physics.addBody( box11 ,{ bounce = 0 , friction = 5 , density = 1 } )
box11:setFillColor(0, 0, 255)

local box12 = display.newRect( 125, 800,50,50)
physics.addBody( box12,{ bounce = 0 , friction = 5 , density = 1 } )
box12:setFillColor(255, 0, 0)

local box13 = display.newRect( 200, 800,50,50)
physics.addBody( box13,{ bounce = 0 , friction = 5 , density = 1 } )
box13:setFillColor(0, 0, 255)

local box14 = display.newRect( 275, 800,50,50)
physics.addBody( box14 ,{ bounce = 0 , friction = 5 , density = 1 } )
box14:setFillColor(255, 0, 0)

local box15 = display.newRect( 350, 800,50,50)
physics.addBody( box15,{ bounce = 0 , friction = 5 , density = 1 } )
box15:setFillColor(0, 0, 255)

local box16 = display.newRect( 425, 800,50,50)
physics.addBody( box16,{ bounce = 0 , friction = 5 , density = 1 } )
box16:setFillColor(255, 0, 0)

–2nd row of boxes, spread 50x50 size, spread out 75 pix apart, alternate colors green / yellow
local box21 = display.newRect( 85, 749,50,50)
physics.addBody( box21 ,{ bounce = 0 , friction = 5 , density = 1 } )
box21:setFillColor(0, 255, 0)

local box22 = display.newRect( 160, 749,50,50)
physics.addBody( box22 ,{ bounce = 0 , friction = 5 , density = 1 } )
box22:setFillColor(255,255,0)

local box23= display.newRect( 235, 749,50,50)
physics.addBody( box23 ,{ bounce = 0 , friction = 5 , density = 1 } )
box23:setFillColor(0, 255, 0)

local box24 = display.newRect( 310, 749,50,50)
physics.addBody( box24 ,{ bounce = 0 , friction = 5 , density = 1 } )
box24:setFillColor(255,255,0)

local box25 = display.newRect( 385, 750,50,50)
physics.addBody( box25 ,{ bounce = 0 , friction = 5 , density = 1 } )
box25:setFillColor(0, 255, 0)

–3rd row of boxes
–teal / white

local box31 = display.newRect( 62, 697,50,50)
physics.addBody( box31 ,{ bounce = 0 , friction = 5 , density = 1 } )
box31:setFillColor(0, 255, 255)

local box32 = display.newRect( 127, 697,50,50)
physics.addBody( box32 ,{ bounce = 0 , friction = 5 , density = 1 } )
box32:setFillColor(255, 255, 255)

local box33 = display.newRect( 197, 697,50,50)
physics.addBody( box33 ,{ bounce = 0 , friction = 5 , density = 1 } )
box33:setFillColor(0, 255, 255)

local box34 = display.newRect( 270, 697,50,50)
physics.addBody( box34 ,{ bounce = 0 , friction = 5 , density = 1 } )
box34:setFillColor(255, 255, 255)

local box35 = display.newRect( 340, 697,50,50)
physics.addBody( box35 ,{ bounce = 0 , friction = 5 , density = 1 } )
box35:setFillColor(0, 255, 255)

local box36 = display.newRect( 405, 697,50,50)
physics.addBody( box36 ,{ bounce = 0 , friction = 5 , density = 1 } )
box36:setFillColor(255, 255, 255)

–4th row, one for fun.

local box41 = display.newRect( 89, 645,50,50)
physics.addBody( box41 ,{ bounce = 0 , friction = 5 , density = 1 } )
box41:setFillColor(0, 255, 0)

–single stack of boxes

local boxr1 = display.newRect( 560, 800,50,50)
physics.addBody( boxr1 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr1:setFillColor(200, 100, 180)

local boxr2 = display.newRect( 560, 750,50,50)
physics.addBody( boxr2 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr2:setFillColor(200, 100, 180)

local boxr3 = display.newRect( 560, 700,50,50)
physics.addBody( boxr3 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr3:setFillColor(200, 100, 180)

local boxr4 = display.newRect( 560, 650,50,50)
physics.addBody( boxr4 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr4:setFillColor(200, 100, 180)

local boxr5 = display.newRect( 560, 600,50,50)
physics.addBody( boxr5 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr5:setFillColor(200, 100, 180)

local boxr6 = display.newRect( 560, 550,50,50)
physics.addBody( boxr6 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr6:setFillColor(200, 100, 180)

local boxr7 = display.newRect( 560, 500,50,50)
physics.addBody( boxr7 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr7:setFillColor(200, 100, 180)

local boxr7 = display.newRect( 560, 450,50,50)
physics.addBody( boxr7 ,{ bounce = 0 , friction = 5 , density = 1 } )
boxr7:setFillColor(255, 100, 180)

[/code] [import]uid: 61600 topic_id: 14847 reply_id: 54973[/import]

Was there ever a solution to this problem? [import]uid: 14935 topic_id: 14847 reply_id: 75192[/import]