Problem with chipmunk physics

Hi there. I have a problem getting my player to move with chipmunk physics. I get it to show, but it doesnt fall or anything. I think its to do with setting the physics body to the player sprite. But i get an error when i set it…

Please Help!!

Anyone? Im still stuck. Thanks! @pandamicro @slackmoehrle

I am not sure about JavaScript. Check the js-tests. There is a working example there.

yer I tried that but still no luck. For some reason it always gives me an error when i try to set the sprite a body…

anyone?? I have been stuck with this for over 2 weeks. I have had a look at all the tutorials for chipmunk physics in relation to cocos creator/cocos js but still no luck. for some reason, console says that setBody is not a function?? @nantas2 @pandamicro

Can you paste your code here please.

1 Like

@slackmoehrle

Ok here is my code:

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
var 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);
newPlayer.setBody(body);
// 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);
}
},
});

Is anyone able to help me? Please!? @pandamicro @slackmoehrle

What line is causing the error?

newPlayer.setBody(body) is causing the error. I put a picture up the top of the post which shows the error in console. I have no idea how to fix it… @slackmoehrle

Is anyone able to help? I have been patiently waiting for a while now. I really need to get this working. Please!! @slackmoehrle @ThanhLe69

Seriously! Can someone please just help me!? I think i have been quite patient. I really need to get this working… Thanks.
@slackmoehrle @pandamicro @ThanhLe69

Did you see a working example in js-tests?

Does that example work for you? I’ll try to get to this today. I got tied up on new docs yesterday.

Shoot me an email about this so it is in my face. Slackmoehrle@cocos2d-x.org

1 Like

Yes i tried the js tests and i have done everything it does to setup chipmunk… I will send you an email right now

Hi Drunk3n1, are you using Cocos Creator?

If so, it seems the physics engine isn’t fully supported by Cocos Creator just yet.

In th meantime though, it might be worth checking this…

It seems the physics component won’t be included for another 6 months or so (https://trello.com/c/PUL61UYe/25-physics-integrate-physics-engine-with-component10)

Yes i am @JapanDev

Please check the edit of my previous post.

1 Like

Yes i tried that but the only problem i have is that i cant set the physics body to my sprite. Which that tutorial can do. My code is shown above… Can you help?

NewPlayer.setBody(body) is the problem… @JapanDev

Hi Drunk3n1,

Sorry, I’ve not gone through this process myself so can’t be of much help here with just the snippet. From what you provided though, it seems like you would most likely need to subclass player and add a body property along with the setBody(body) function. It might even be worth copying the cannon code and stripping it down to what you need.

Just my 2 cents, but I wouldn’t personally want to use the physics component until it’s properly implemented and supported if I was planning on publishing an app requiring it. From the Cannon example, there’s just too much hacking for my liking though I’m sure that’s not what you want to hear.

1 Like