0

How to create chipmunk debug layer with Cocos2d-JS v3? I could not find an example of how to do it.

Tolsi
  • 656
  • 9
  • 20

2 Answers2

3

Assuming you have added "chipmunk" to "modules"in your projects project.json, simply place the following within the ctor or init method of the Layer that has the Chipmunk space defined in it:

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

//Add the Debug Layer:
var debugNode = new cc.PhysicsDebugNode(space);
debugNode.visible = true;
this.addChild(debugNode);

You could also add the following to set up a "floor" and a sprite to bounce on it:

//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
var myBody = new cp.Body(Infinity, cp.momentForBox(Infinity, 10, 50));
myBody.p = cc.p(derecha - 10, arriba / 2);
space.addBody(myBody);

var myShape = new cp.BoxShape(myBody, 10, 50);
myShape.setElasticity(1);
myShape.setFriction(0);
space.addShape(myShape);
Sebastián Vansteenkiste
  • 2,234
  • 1
  • 19
  • 29
  • Thanks for the answer. And yet, how to display the collisions with cc.PhysicsDebugNode? – Tolsi Oct 15 '14 at 06:57
  • I don't understand what you are asking. Once you add the `PhysicsDebugNode` all physics objects added to the `space` will be rendered. – Sebastián Vansteenkiste Oct 16 '14 at 01:29
  • I have the static bodies (as hollow circle) and one dynamic body governed by an accelerometer. And when the dynamic body interacts with static bodies, then it stops, but there is no display of this collisions. On some screenshots I saw that the collision points are displayed as dots. – Tolsi Oct 16 '14 at 07:46
  • 1
    That's the first time I hear about that. It sounds interesting. Could you provide links to said screenshots and/or their sources? – Sebastián Vansteenkiste Oct 17 '14 at 16:12
  • probably I'm doing wrong something :[ this is my first experience with the physics engine. here is my project http://goo.gl/TtHIF4 – Tolsi Oct 17 '14 at 18:48
  • There are a few things that are a bit strange about your code, but nothing that has to do with this question/answer. As far as I know, there's no built-in function for what you are trying to do. An "easy" way to do it would be perhaps to add a callback for collisions and show a sprite when they happen, and then fade hide it after a few milliseconds. – Sebastián Vansteenkiste Oct 20 '14 at 02:24
  • I also had this idea. If not difficult for you, can you write about the strangeness of my code? It's compiled from CoffeeScrypt, but I think, you are talking not about that.. – Tolsi Oct 20 '14 at 06:25
  • Oh, so you are using CoffeeScript, that could be it. What's most strange to me is the way that you are wrapping all classes in parentheses. – Sebastián Vansteenkiste Oct 20 '14 at 20:41
0

To do this, i must add the module "physics" in project.json and then use cc.PhysicsDebugNode

Tolsi
  • 656
  • 9
  • 20