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

Preventing player from moving two boxes at once

Topics: User Forum
Apr 3, 2012 at 9:41 AM

Hey guys,

I have a top-down game where the player character can push boxes around.

I need the player to be only able to push a box if it is by itself and there is no other box behind it. i.e. one box at a time.

So if you push a box and it is also pushing on another box I need to stop the boxes from moving essentially.

I've tried tweaking the player pushing force, box masses and my friction force to achieve this but the player can still very very slowly move two or more boxes at once. Even if the friction force is tweaked to try and cancel out any very slow movement completely.

I've tried to lock down any boxes that are moving very slowly by calling ResetDynamics() on their body or setting their LinearVelocity to zero if they are moving at a really slow speed but they keep moving. The player character body can still push them slowly along.

I'm guessing that even by doing this the collision resolution with the playercharacter bumping the box still needs to move it a little bit?

I've also tried to find a way to set the box to Bodytype.Static and only switch it to Dynamic when being collided by something at high enough speed but this caused an exeption in the Timestep solver and seemed like a horrible hack.

Does anyone have any suggestions to prevent this slow, 2 boxes at once movement?






Apr 3, 2012 at 4:41 PM

If your boxes are all of the same size, lets say 5 units, then check what's at 10-15(i would use 12) units from the player in the moving direction. If there's a box just set it to static.


If the boxes have different sizes you'll need to use this size to calculate the distance at which the next box would be.

Apr 3, 2012 at 8:31 PM

The behaviour needed is unphysical. If a player can push a box of some size he can definitely push two: Newton's law of motion F=ma so if the mass is double then acceleration will be half. Special friction between boxes could help but it's hard to simulate.

In general to implement unphysical behaviour it's definitely not a hack to use special bodies like static or kinematic or a switching around according to the needs.  Actually it would be a hack to try to use physics (e.g. friction) :)

Apr 3, 2012 at 11:22 PM

It's not entirely unphysical though is it? In reality, if you can just manage to push one box and then attempted to push 5 at once you'd probably fail to move them.

I think its because in reality friction is not constant. The friction coefficient is higher when the body is at rest. So there is a minimum force required to set something in motion.

So I think you're right I need to switch the body type somehow so its Static unless a sufficient force has been applied.

What I'd like to do is at the start of every Update(), set all stationary boxes to Static. Then all the forces get applied from the player pushing, collisions etc. And just before calling World.Step(), I check each box to see if it has had a force applied to it sufficient to move it. If it has, I switch it to Dynamic and then call World.Step().

Don't know if Farseer works that way though and will allow that?

Is there a way to find out how much force a body has applied to it currently? I haven't see anything that will allow me to do that check...



Apr 3, 2012 at 11:29 PM

Hey victor, thanks! Great idea, but the problem is possibly that the player isn't the only thing that can move boxes. There might be explosions or other things going on that can also move them.

So I'd need to be able to check for all those other things as well and set the box back to dynamic if anything else is trying to move the box. Not sure the best way to do that. 

See my reply to Jerrysb,  it would be great if I could just check how much force has been applied to the box and decide whether it should be Static or Dynamic just prior to calling World.Step() but I don't know if that is possible.

Apr 4, 2012 at 12:40 AM

After thinking about this some more, I think I just need some way to simulate Static Friction in Farseer. That is, every Body will have some minimum force required to set it in motion. If the force applied to it is below that value, it won't move.

Then if you are trying to push two boxes at once, then problem will be solved because you'll need to achieve double the force to be able to set them both in motion.

I think... :)

Does anyone know how to simulate Static Friction in Farseer when in a top down perspective? as opposed to Kinetic friction....




Apr 4, 2012 at 6:05 PM
Edited Apr 4, 2012 at 6:06 PM

Well, I've thought about your idea of friction, and what you could do is check forces/impulses being applied to boxes, and if they're not big enough then apply a force/impulse of the same force/impulse in the opposite direction, making the balance 0.


My wonder is if this would cause strange behavior, or body shaking. You could make the static while the force/impulse is not big enough.

Apr 4, 2012 at 6:38 PM
Edited Apr 4, 2012 at 8:48 PM

I think we're on the right track here.

As a side-note Box2D and Farseer essentially implement Coulomb friction. Problem with simulating Coulomb friction numerically is that it can become unstable (it's even pointed out in the wikipedia article). This has to do with the integrator and how it approximates functions). This is actually quite visible when you need joint friction and try to do it yourself (good that we have joint motors)... See for example various discussions here and on the other forums about bodies sliding on a slope and so on.

So scratching friction and scratching static/kinematic bodes  how about a hybrid to what you guys are proposing: how about catching the PreSolve() event and if Body A is a box and Body b is another box that is moving against our stationary Box A then just disable the contact and kill the velocity (angular and linear) of Box B.

Somewhat similar problem is solved in this tutorial:

Apr 4, 2012 at 11:59 PM

Thanks so much guys!

I have found something that is working pretty good for me, although I'll try the approach you linked to jerrysb and see how that goes.

@victorspain sounds good but how do you find out the current force/impulse that is being applied to a body? I haven't found a way to find it yet...

So my current solution is this:

1. All boxes use a FrictionJoint to model its ground sliding friction. I can see now that this does model static friction but like you said jerrysb, its unstable and does allow some small movement when it shouldn't. If the player can push 1 box, they can always push 2 although very very slowly.

2. So I do a raycast from the player character out in front to a distance of 1.5 the size of box. If that raycast finds more than 1 box, I stop the player and apply a much weaker pushing force so that they'll fail to move more than one box at once. It's not perfect and doesn't account for all cases but might be good enough for now.