Dynamic Body Stacking and Margins

I am trying to stack some basic dynamic boxes (squares). 40px x 40px. When I make the bodies “dynamic” there seems to be a 1px margin around each body, making the shape 42px x 42px. This also makes the body appear to float off the floor object by 1px. Is there any way to fix this?

Also, when stacking these dynamic bodies, the left body slowly topples off the stack and the right body slowly slides to the left. I do not have the accelerometer activated and there is no X coord gravity applied, only Y coord. (0, 9.8).

Any ideas on what I am doing wrong with these dynamic bodies?

Thanks,
Ian [import]uid: 12877 topic_id: 5297 reply_id: 305297[/import]

The margin I don’t know how to fix; I have noticed that too, but I haven’t messed with physics enough to know how to fix it.

As for the other problem, the first thing I’m wondering is if the boxes need to rotate or are always upright. In the latter case, the toppling will probably be fixed by using the isFixedRotation property.

http://developer.anscamobile.com/reference/index/bodyisfixedrotation [import]uid: 12108 topic_id: 5297 reply_id: 17566[/import]

For your first problem, I would solve it by specifying the shape property for your boxes (as opposed to letting box2d make the body automatically following the image size) and set it at 39x39 pixels, so that when the physical body is constructed, it would end up as 40x40. Or maybe when setting the shape property, the sizes are exact? This needs some confirmation. Do that and tell us. :slight_smile:

Regarding your second problem, you could set a larger value for friction… As an example, a while ago I tried this: 2 balls, identical in size, one on top of the other (same x coordinate), falling one over the other. When they reached the ground, seeing as there is no X influence (like in your case), they should stack one on top of the other, right? Well they don’t. But then again… they wouldn’t do that in real life, would they? :slight_smile: [import]uid: 13720 topic_id: 5297 reply_id: 17644[/import]

@jhocking Thanks. .isFixedRotation actually does help the toppling over problem. Although it’s not the ideal result, it could work in this scenario.

@florinsivlad Thanks. I will try specifying the shape property. I set a high friction value, which helped, but did not prevent the boxes from toppling over. [import]uid: 12877 topic_id: 5297 reply_id: 17673[/import]