Chipmunk physics problem

Hey everyone!! I am trying to use chipmunk physics with my game, and for some reason when i run my game in cocos creator, only the sprite shows and nothing else happens. It doesn’t fall or move. This is my code. any help would be appreciated. Thanks!!

code__________

var body;
cc.Class({
extends: cc.Component,

properties: {
    playerPrefab: {
        default: null,
        type: cc.Prefab
    },
},


PhysicsInit: function(){
    //create physics space
    var space = this.space = new cp.Space();
    space.gravity = cp.v(0.3, 0.5);
    space.iterations = 60;

    //here i setup my static walls

},

createPlayer: function(x, y){
    var space = this.space;
    var newPlayer = cc.instantiate(this.PlayerPrefab);
    var pos = newPlayer.position = cc.v2(x,y);
    var mass = 1;
    var radius = 40;
    var velocity = 10;
    //make body for PlayerPrefab
     body = space.addBody(new cp.Body(mass, cp.momentForCircle(mass, 0, radius, cp.v(0,0))));
    body.setPos(pos);
    body.setVel(cp.v(Math.sin(20) * velocity, Math.cos(20) * velocity));
    //make shape for body
    var playerShape = space.addShape(new cp.CircleShape(body, radius, cp.v(0,0)));
    playerShape.setElasticity(0.8);
    playerShape.setFriction(1);

    // put the newly added node under the Canvas node
    this.node.addChild(newPlayer, 13);
    cc.log("printed");

},

// use this for initialization
onLoad: function () {
    this.PhysicsInit();
    var x = ((cc.winSize.width/2) + 30);
    var y = ((cc.winSize.height/2) + 50);
    this.createPlayer(200,300);
},

// called every frame, uncomment this function to activate update callback
 update: function (dt) {
        if (this.space) {
            this.space.step(dt);
        }

   },
});

value of gravity is very low. try cp.v(0,400) instead

1 Like

I just tried that and still nothing happens. the player is displayed but not moving. I have no idea why??

i had fix something in your code. if you see any issues on console. show me

var body;
cc.Class({
extends: cc.Component,

properties: {
playerPrefab: {
default: null,
type: cc.Prefab
},
},

PhysicsInit: function(){
//create physics space
this.space = new cp.Space();
this.space.gravity = cp.v(0, -400);
this.space.iterations = 60;

//here i setup my static walls

},

createPlayer: function(x, y){
var newPlayer = cc.instantiate(this.PlayerPrefab);
var pos = newPlayer.position = cc.v2(x,y);
var mass = 1;
var radius = 40;
var velocity = 10;
//make body for PlayerPrefab
body = this.space.addBody(new cp.Body(mass, cp.momentForCircle(mass, 0, radius, cp.v(0,0))));
body.setPos(pos);
body.setVel(cp.v(Math.sin(20) * velocity, Math.cos(20) * velocity));
newPlayer.addBody(body);
//make shape for body
var playerShape = this.space.addShape(new cp.CircleShape(body, radius, cp.v(0,0)));
playerShape.setElasticity(0.8);
playerShape.setFriction(1);
this.node.addChild(newPlayer, 13);
cc.log(“printed”);

},

// use this for initialization
onLoad: function () {
this.PhysicsInit();
var x = ((cc.winSize.width/2) + 30);
var y = ((cc.winSize.height/2) + 50);
this.createPlayer(200,300);
},

// called every frame, uncomment this function to activate update callback
update: function (dt) {
if (this.space) {
this.space.step(dt);
}

},
});

ok i tried that and it gave this error:

oh! sorry. i forgot. that function is: newPlayer.setBody(body)

1 Like

I replaced newPlayer.addBody with newPlayer.setBody(body);

but it gives this error:

anyone? Still having problems with this! @ThanhLe69