Adding bodies to the space
We already have the addBody
function with all required arguments, so it's time to define it:
addBody: function(posX,posY,width,height,isDynamic,spriteImage,type){ if(isDynamic){ var body = new cp.Body(1,cp.momentForBox(1,width,height)); } else{ var body = new cp.Body(Infinity,Infinity); } body.setPos(cp.v(posX,posY)); if(isDynamic){ world.addBody(body); } var shape = new cp.BoxShape(body, width, height); shape.setFriction(1); shape.setElasticity(0); shape.name=type; world.addShape(shape); }
This is where big differences between Box2D and Chipmunk2D start to show. Thus, we will explain the addBody
function line-by-line:
if(isDynamic) { var body = new cp.Body(1,cp.momentForBox(1,width,height)); } else{ var body = new cp.Body(Infinity,Infinity); }
We have two ways to create a body, irrespective of whether it's static or dynamic. Both use the cp.Body
method, whose arguments are the mass and the moment of inertia. The moment of inertia...