Conveyor Belts

In Google SVN for the C++ box2d, the two SolveVelocityConstraints additions become:

Code: Select all

// already there:
b2Vec2 dv = vB + b2Cross(wB, ccp->rB) - vA - b2Cross(wA, ccp->rA);

// new code:
if(c->shape1->conveyorSpeed != 0) {
   b2Vec2 t = tangent;
   t *= c->shape1->conveyorSpeed;
   dv += t;
}
if(c->shape2->conveyorSpeed != 0) {
   b2Vec2 t = tangent;
   t *= c->shape2->conveyorSpeed;
   dv -= t;
}

Posted for code review as much as to share. 🙂
mayobutter wrote:Ok, test bed example:

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

And source, if anyone wants to try it:

http://sideroller.com/test/box2dflash.zip


Nice, but i discovered that if you put a shape on the left egde it will move to the left instead of right sometimes.
conveyor-sideeffect.png
conveyor-sideeffect.png (4.22 KiB) Viewed 846 times
what about using a joint?
Does anyone know how to implement this in the current code base (2.1.2)? It looks quite different now.

tia
android wrote:
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?


I tried this on jBox2d with android and it did not work. I got the desired effect by making the body kinematic and applying the force to it. And then setting its position to the original place on every draw(sounds pretty dumb i know 😀). I have no idea if this is in any case proper way to do it.

Code: Select all

BodyDef groundBodyDef;
groundBodyDef = new BodyDef();

groundBodyDef.type = BodyType.KINEMATIC;
groundBodyDef.position.set(new Vec2(0, 0));
groundBodyDef.angle = 0;
       
Body part1 = world.createBody(groundBodyDef);
part1.setUserData("conveyor");

FixtureDef fixtureDef = new FixtureDef();
PolygonShape boxDef = new PolygonShape();
boxDef.setAsBox(10f, 0.1f);
fixtureDef.shape = boxDef;
fixtureDef.density = 1f;
fixtureDef.friction = 1f;
part1.createFixture(fixtureDef);
part1.setLinearVelocity(new Vec2 (3,0));

//And then in onDraw

if(playing)
      {
         Vec2 vec;
                   Body mBody = mWorld.getBodyList();

          
           do
           {
               Fixture fixture = mBody.getFixtureList();
               if (fixture != null)
               {
                   vec = mBody.getPosition();
                   float rot = mBody.getAngle() * 57f;  // convert radians to degrees
                   if (ShapeType.POLYGON == fixture.getType())
                   {
                      PolygonShape pol = (PolygonShape)fixture.getShape();
                         Vec2[] vertexes = pol.getVertices();
                      
                       if(mBody.getUserData() == (Object)"conveyor")
                       {
                          mBox.draw(gl, vec.x, vec.y, 0f, rot, vertexes[2].x, vertexes[2].y);
                          mBody.setTransform(new Vec2(0,0), 0);
                       }
                  
                   }
                  
               }
              
               mBody = mBody.getNext();
           }
           while (mBody != null);
           mWorld.update();
      }

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