Conveyor Belts

I started thinking about how one might model a conveyor belt. At first I figured I could just apply an impulse in the direction of the contact tangent, but that ended up being pretty naive.

Eventually realized that friction is the key, and that maybe the easiest way to do this would be to trick the ContactSolver into thinking there is relative motion between the two bodies, when in fact there is not. It might work...

Any better ideas?
Well...you could build an actual conveyor belt. Use two rotating circles, and wrap a "bridge" around it (from the TestBed). Or just use lots of little circles and forget the chain. What was wrong with applying an impulse in the direction of the contact tangent?
The impulse solution will eventually accelerate objects until they're going faster than the conveyor belt. It's just plain the wrong way to model it.

I just tested tweaking the "Relative velocity at contact" in the SolveVelocityConstraints() and this is exactly the effect I'm looking for. I hate hacking the internals though.
mayo: That sounds like a good way to do it. Let me know if you figure out how to make a nice API for it. That would make a nice contribution.
I thought that as the API stood you could set the velocity of static bodies and achieve exactly this effect.
Boris: this can actually be used on Dynamic bodies as well, to create, for example, a poor man's Tank treads.

Ok, here's the solution:

I wanted the conveyor belts to be shape based, not body based, so I added a property to b2Shape (I haven't bothered with the b2ShapeDef class yet):

Code: Select all

// in b2Shape
public var conveyorSpeed:Number = 0;


Next, SolveVelocityConstraints() needs to know about the shapes, so I had to add shape information to b2ContactConstraint.

Code: Select all

// in b2ContactConstraint
public var shape1:b2Shape;
public var shape2:b2Shape;


And then set those shapes when the constraints are built:

Code: Select all

// in b2ContactSolver constructor
// This code exists
cc.body1 = b1; //p
cc.body2 = b2; //p
// This is added      
cc.shape1 = shape1;
cc.shape2 = shape2;


Now tweak the relative velocity for one-point constraints:

Code: Select all

// b2ContactSolver -> SolveVelocityConstraints()
// This code exists
dvX = v2.x + (-w2 * ccp.r2.y) - v1.x - (-w1 * ccp.r1.y);
dvY = v2.y + (w2 * ccp.r2.x) - v1.y - (w1 * ccp.r1.x);
// This code added:
if(c.shape1.conveyorSpeed != 0) {
   dvX += tangentX * c.shape1.conveyorSpeed;
   dvY += tangentY * c.shape1.conveyorSpeed;
}
if(c.shape2.conveyorSpeed != 0) {
   dvX -= tangentX * c.shape2.conveyorSpeed;
   dvY -= tangentY * c.shape2.conveyorSpeed;
}


And also tweak for two-point constraints:

Code: Select all

// b2ContactSolver -> SolveVelocityConstraints()
// This code exists:
dvX = v2.x - w2 * ccp.r2.y - v1.x + w1 * ccp.r1.y;
dvY = v2.y + w2 * ccp.r2.x - v1.y - w1 * ccp.r1.x;
// This code added:               
if(c.shape1.conveyorSpeed != 0) {
   dvX += tangentX * c.shape1.conveyorSpeed;
   dvY += tangentY * c.shape1.conveyorSpeed;
}
if(c.shape2.conveyorSpeed != 0) {
   dvX -= tangentX * c.shape2.conveyorSpeed;
   dvY -= tangentY * c.shape2.conveyorSpeed;
}


I'll see if I can throw a test bed together for demo purposes.
Ok, test bed example:

http://sideroller.com/test/PhysTest.swf

And source, if anyone wants to try it:

http://sideroller.com/test/box2dflash.zip
BorisTheBrave wrote:I thought that as the API stood you could set the velocity of static bodies and achieve exactly this effect.


Set the velocity as in: call SetLinearVelocity() on a static body?
Nice one mayo, that works beautifully. I suggest you WakeUp the body when you change the conveyor speed, though, otherwise you can get it sleeping in a corner.

Android: Yes, though I've never tried it.
I want to create a conveyor belt with a real belt and wheel motion effect. Any help ??

Original thread: https://box2d.org/forum/viewtopic.php?t=2861