This project has moved and is read-only. For the latest updates, please go here.

Stacking Objects

Topics: Developer Forum, User Forum
Jun 18, 2008 at 5:08 AM
I'm attempting to allow stackable blocks in a game I'm creating. These blocks are created by the user. However, when I try to just stack blocks horizontally 4 or 5 blocks tall I run into an issue where several of the upper blocks begin to slide. It starts out slow and then they speed up, sliding way out of position. Oftentimes this leads to the tower just falling apart. The only way to stop this that I've found is to crank allowed penetration WAY up. This, however, leads to the side effect where the blocks really get stuck together in weird positions. So if a tower should really topple it won't because the corners of the blocks all stick together. Is there something I'm missing that could help with this issue? I've been playing around with different variables for hours and am about ready to pull my hair out.

Jun 18, 2008 at 9:21 AM
The example with the staciking block in the Farseer demo's does this too.. I guess its a common problem in some physics engines..

Have you played around with friction? Maybe it can help.. you a thought.
Jun 18, 2008 at 3:29 PM
It seems to happen the same regardless of friction. I'm not sure if there are any other variables besides allowed penetration I can play around with that might give some results but I haven't found any yet.
Jun 19, 2008 at 2:12 AM
I'm getting around the issue for now by just using two geometries per block. One for one half, one for the other. It seems to have eliminated the problem even at pretty low Allowed Penetration levels. However, I'm still hoping there is a less performance impacting solution out there.
Jun 19, 2008 at 3:36 AM
If I'm not mistaken, you will need support for resting bodies.

This means that the physic objects freeze when they are at rest. You example is a situation where this is needed. Bodies resting on each other.

I've seen several physic engines with this feature, also ones based on Box2D (Farseer is also based on Box2D), you might be able to port the feature into Farseer.
Jun 19, 2008 at 3:41 AM
Edited Jun 19, 2008 at 3:46 AM
I actually found this one today:

It's a physics engine for Flash 9, it's based on Box2D and supports body resting (sleeping). As you see in the examples, the bodies changes color when active/sleeping.

EDIT: Oh, nearly forgot this one:
It's a port of Box2D to C#, it also supports sleeping.
Apr 4, 2009 at 5:12 PM
Can anyone explain the solution of having 2 geometries per block?
I'm having the same stacking problems.
Apr 4, 2009 at 8:42 PM
Thanks for bumping this.  I have also been having a problem with resting bodies, but I must have missed this thread.  I will check out genbox's links and get more information.

For my game, I don't think a workaround will work.  I actually need resting bodies.  Maybe RogueCommanderIX can explain how these are different from his patch.
Apr 5, 2009 at 4:51 PM
Resting bodies is supported by Farseer Physics. It's called InactivityController.
The InactivityController disables bodies that are out of range of other bodies. When a body is approaching a resting body, it will wake up the resting body. It requires a lot of fiddling to get it to work properly. It can improve stacking, but it can also make it more unstable with the wrong properties.
Farseer Physics has some good stacking abilities already. Try without the resting bodies first.

All physics engines have stacking "problems". While it can be minimized by using algorithms, we are still limited in precision by the computer. The calculations needs to be done quickly so that we can have many geometries at the same time. Some engines have implemented error correction algorithms to make more stable stacking, but they are still not 100% accurate and they have the overhead of errorchecking.

You might want to try out Box2DX (A C# port of Box2D) and see if it has more stable stacking. Box2D implemented error correction some time ago if I remember correctly.
Apr 6, 2009 at 6:06 AM
Thanks genbox.  I actually found the InactivityController last night and have been playing with it since then.  I was going to recommend that to Bishoymly as well.  No need to look at the "2 geoms per block" solution, when the InactivityController probably does what you want. 

For me, it doesn't appear that it will do what I need, the way I am currently doing things.  I need blocks to stack, and stay stacked, even while they are being collided with.  I just need to modify the way I have been doing things.  I'll do some more digging for my specific scenario and maybe start a new thread when I have solved my issues.  They are probably fairly unique, because they have to do with my specific game.