Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by sangel94403

Is there any work taking place to port the latest Box2D code over to Impact? Specifically I am looking for the setTangentSpeed functionality for contacts (http://code.google.com/p/box2d-html5/source/browse/trunk/Box2D/Testbed/Tests/ConveyorBelt.js).

Or has one of you geniuses come up with another way to create a conveyor belt (or surface) using box2d entities?

1 decade ago by sangel94403

I didn't get a response so here is how I created my conveyor belts. I added a contact listener to plugins/box2d/game.js that contains a PreSolve function. When the contact occurs, If one of the contact points belongs to a conveyor, I apply a force to the center of mass of the other object.

A couple points to note.
1) In plugins/box2d/game.js, add this.addContactListener(); to the loadLevel function. The addContactListener function is below.
2) For each entity, you will need a collision function
3) In the createBody function for each entity add these properties:
a) this.body.userData = 'yourEntityName';
b) this.body.entity = this;
4) When creating your conveyor in weltmeister, create a 'dir' key with value of either 'left' or 'right' depending on which direction you want your conveyor to roll.

Here is the addContactListener function to add to the plugins/box2d/game.js
addContactListener: function() {
       var listener = new Box2D.Dynamics.b2ContactListener();
       
       listener.BeginContact = function(contact){
            //console.log('Add Contact', point);
            // Instead of telling you which b2Body collided, the b2Contact tells you which fixture collided.
            var fixA = contact.GetFixtureA();
            // To know which b2Body was involved in the collision you can retrieve a reference to the fixture.
            var fixB = contact.GetFixtureB(); 
            var body1 = fixA.GetBody();
            var body2 = fixB.GetBody();

            if (body1.userData !== undefined && body1.entity !== undefined) {
                // console.log('body 1 Add Contact', point);
                body1.entity.collision((body2.entity !== undefined ? body2.entity : undefined));
            }
            if (body2.userData !== undefined && body2.entity != undefined) {
                // console.log('body 2 Add Contact', point);
                body2.entity.collision((body1.entity !== undefined ? body1.entity : undefined));
            }
      };
        
      listener.EndContact = function (contact) {
      };
    
      listener.PostSolve = function (contact, impulse) {
      };
    
      listener.PreSolve = function (contact, oldManifold) {
          var fixtureA = contact.GetFixtureA();
          var fixtureB = contact.GetFixtureB();
          //console.log('PreSolve');
             
          if (fixtureA.GetBody().userData == 'conveyor')
          {
              //console.log('PreSolve - Apply force to fixtureB ' + fixtureB.GetBody().userData);
              var speed = 0;
              if ( fixtureA.GetBody().entity.dir == 'left') {
                 speed = -200.0;
              } else {
                 speed = 200.0;
              }
              var vect = new Box2D.Common.Math.b2Vec2(speed, 0); 

              // Apply force to contact body 
              var cPos = fixtureB.GetBody().GetWorldCenter();
              fixtureB.GetBody().ApplyForce(vect,cPos);

              //contact.SetTangentSpeed(5.0);
          }

          if (fixtureB.GetBody().userData == 'conveyor')
          {
              //console.log('PreSolve - Apply force to fixtureA ' + fixtureA.GetBody().userData);              
              //contact.SetTangentSpeed(-5.0);
                var speed = 0 ;
              if ( fixtureB.GetBody().entity.dir == 'left' ) {
                                   speed = -200.0;
              } else {
                                  speed = 200.0;
              }
              var vect = new Box2D.Common.Math.b2Vec2(speed, 0);
               
              var cPos = fixtureA.GetBody().GetWorldCenter();
              fixtureA.GetBody().ApplyForce(vect,cPos);
          }
      };
        
      ig.world.SetContactListener(listener);        
},

And here is my conveyor.js
// JavaScript Document
ig.module(
    'game.entities.conveyor'
)
.requires(
    'impact.entity',
    'plugins.box2d.entity'
)
.defines(function(){
EntityConveyor = ig.Box2DEntity.extend({
    //_wmDrawBox: true,
    //_wmBoxColor: 'rgba(0, 255, 0, 0.7)',
    size: {x: 250, y:50},
    offset: {x: 0, y: 0},
    zindex: 16,
    name: 'conveyor',
    type: ig.Entity.TYPE.A,
    checkAgainst: ig.Entity.TYPE.NONE,
    collides: ig.Entity.COLLIDES.NEVER, // Collision is already handled by Box2D!
    animSheet: new ig.AnimationSheet( 'media/conveyor.png', 250, 50 ),
    dir: 'left',    // dir is set from weltmeister info 
    bodyDef: undefined,
        
    init: function( x, y, settings ) {
        // Add the animations
        this.addAnim( 'idle', 1, [0] );
        this.addAnim( 'left', 0.1, [0,1,2,3], false );
        this.addAnim( 'right', 0.1, [3,2,1,0], false );
        this.currentAnim=this.anims.idle;
        this.parent( x, y, settings );        
    },
    
    createBody: function() {
        this.bodyDef = new Box2D.Dynamics.b2BodyDef();
        this.bodyDef.position = new Box2D.Common.Math.b2Vec2(
            (this.pos.x + this.size.x / 2) * Box2D.SCALE,
            (this.pos.y + this.size.y / 2) * Box2D.SCALE
        ); 
        this.bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
        
        this.body = ig.world.CreateBody(this.bodyDef);
                
        this.body.userData= "conveyor";
        this.body.entity = this;
        var fixture = new Box2D.Dynamics.b2FixtureDef;
        fixture.shape = new Box2D.Collision.Shapes.b2PolygonShape();
        fixture.shape.SetAsBox(
            this.size.x / 2 * Box2D.SCALE,
            this.size.y / 2 * Box2D.SCALE
        );
        
        fixture.friction = 0.8;            
        this.body.CreateFixture(fixture);                
    },
    
    collision: function(colBody) {
        //console.log('Collision - ' + this.name + ' with ' + (colBody !== undefined ? colBody.name : 'undefined'));
    },

    update: function() {
        if (this.dir == 'left') {
           this.currentAnim=this.anims.left;
        } else {
           this.currentAnim=this.anims.right;
        }
        this.parent();
    }
});

});


1 decade ago by Joncom

Nice work figuring this out on your own! Thanks for sharing. :)
Page 1 of 1
« first « previous next › last »