Basic Use of ApplyImpulse

Posted by nycynik on Game Development See other posts from Game Development or by nycynik
Published on 2012-11-03T15:28:55Z Indexed on 2012/11/03 23:19 UTC
Read the original article Hit count: 148

Filed under:
|

I am trying to apply a force to a bunch of b2_dynamicBodys, but it seems to only work for a random number of items and then stops with an error.

     //create some items to move
     bodyDef.type = b2Body.b2_dynamicBody;
     for(var i = 0; i < 5; ++i) {
        fixDef.shape = new b2PolygonShape;
        fixDef.shape.SetAsBox(1,1);
        fixDef.friction = 1;
        fixDef.restitution = .1;
        bodyDef.position.x = Math.random() * 10;
        bodyDef.position.y = Math.random() * 10;
        bodyDef.linearDamping=1;
        bodyDef.angularDamping=.8;
        itemsArray.push(world.CreateBody(bodyDef).CreateFixture(fixDef));  // store for later
     }

then i try to apply a force later with:

angle = 20;
for (var xIdx=0; xIdx<itemArray.length; xIdx++) {
    itemsArray[xIdx].GetBody().ApplyImpulse(new b2Vec2(50*Math.cos(angle*Math.PI/180),50*Math.sin(angle*Math.PI/180)););
}

the error I receive is

TypeError: 'undefined' is not an object (evaluating 'c.x')

Is there something wrong with saving the items for later use when I am creating them? Does anyone know what is causing this.

© Game Development or respective owner

Related posts about JavaScript

Related posts about box2d