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.