1

I am fairly new to cocos 2d and chipmunk. So far i have managed to create a static object and add collision handler to it. But i want to create a dynamic.

this.space = space;
this.sprite = new cc.PhysicsSprite("#rock.png");
var body = new cp.StaticBody();
body.setPos(pos);
this.sprite.setBody(body);

this.shape = new cp.BoxShape(body,
    this.sprite.getContentSize().width,
    this.sprite.getContentSize().height);
this.shape.setCollisionType(SpriteTag.rock);

this.space.addStaticShape(this.shape);
spriteSheet.addChild(this.sprite);

But i want to create a dynamic body similarly which moves for example by:

cp.v(310, 0), cp.v(0, 0)

This is very basic I know but I would grateful if someone would help me with this. Also if you have good documentation in of cocos 2d and chipmunk in JS. do share it. Thanks

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
Sameer Hussain
  • 2,421
  • 8
  • 23
  • 41

1 Answers1

1

Here you go:

//Add the Chipmunk Physics space
var space = new cp.Space();
space.gravity = cp.v(0, -10);

//Optionally add the debug layer that shows the shapes in the space moving:
/*var debugNode = new cc.PhysicsDebugNode(space);
debugNode.visible = true;
this.addChild(debugNode);*/

//add a floor:
var floor = new cp.SegmentShape(this.space.staticBody, cp.v(-1000, 10), cp.v(1000, 0), 10);
//floor.setElasticity(1);
//floor.setFriction(0);
space.addStaticShape(floor);

//add a square to bounce
//the Sprite
var mySprite = cc.PhysicsSprite.create("res/something.png");
gameLayer.addChild(mySprite);

//the Body
var size = mySprite.getContentSize();
var innertialMomentum = 1; //Use Infinity if you want to avoid the body from rotating
var myBody = new cp.Body(innertialMomentum , cp.momentForBox(innertialMomentum , size.width, size.height));
mySprite.setBody(myBody);
space.addBody(myBody);
//myBody.p = cc.p(xxx, yyy); //To alter the position of the sprite you have to manipulate the body directly, otherwise it won't have the desired effect. You can also access it by mySprite.body

//the Shape
var myShape = new cp.BoxShape(myBody, size.width, size.height);
//myShape.setElasticity(1);
//myShape.setFriction(0);
space.addShape(myShape);

//Apply your desired impulse
//mySprite.body.applyImpulse(cp.v(ix,iy), cp.v(rx,ry)); // Where the first vector is for the impulse strength and direction and the second is for the offset between the center of the object and where you want the impulse to be applied.
Sebastián Vansteenkiste
  • 2,234
  • 1
  • 19
  • 29
  • If i don't want it to bounce. i should just put myShape.setElasticity(0) ? also do you have any link to good tutorial online for chipmunk and cocos 2d js ? Thanks for the help man! – Sameer Hussain Apr 27 '15 at 13:13
  • Yep, mess with the numbers as much as you like :) I don't have any good *updated* tutorials, but beyond these basics there's not a lot more to it than using standard Chipmunk (though you are showing your elements with Cocos2d-js), so you can just search for ChipmunkJS tutorials and you should have what you need, anything else, just ask. PS: I've updated my answer a bit, I didn't realize you were asking about `PhysicsSprite` ;) – Sebastián Vansteenkiste Apr 27 '15 at 13:24
  • Thanks! and yeah i managed to do it myself too with a little bit of experimenting. One last question suppose i want to change gravity of the space in the update function. Like this: update:function (dt) { this.space.gravity = cp.v(0, g_gravity); } it doesn't work instead a new force is applied on the body combined with the previous gravity and they have cumulative effect. How to update the gravity ? thanks in advance – Sameer Hussain Apr 27 '15 at 14:00
  • That's.. Strange. I thought `space.gravity = something` should "override" the previous gravity vector.. If it's working like you said, then I'd just keep a copy of the last applied vector and, whenever you want to change it, cancel it out by applying it's inverse before applying your new gravity. – Sebastián Vansteenkiste Apr 27 '15 at 17:54