Chipmunk and Javascript bindings - can't use getBody

Hi everyone,

I got a problem with Chipmunk when using the Javascript code I wrote for Cocos2d-html5 (which works fine on Chrome/Firefox) on iPhone.
It seems I can’t call getBody() on cc.PhysicsSprite, so I can’t apply any impulse or force.
How can I apply forces/impulses to bodies if I can’t get the body?

Furthermore, I needed to overwrite cp.Body.velocity_func (I followed the recommendation of the author of Chipmunk himself) to achieve what I wanted to do (basically, put a different velocity limit on X an Y axis).
Is there any way I can get that to work too?

Any help, current status of the implementation or work around would be welcome.
Thanks!

Nobody’s working on the JS Binding and its Chipmunk support ?
Please let me know so I can make decisions on what to use in my game.
Thank you for your help.

We are facing another issue where space.pointQueryFirst() always returns shape which is invalid. If you call any function on this returned value like getBody() or getBB() it crashes.

This issue happens only when running on the iOS device. Its working fine on the iOS simulator though.

Have you faced this issue?

Sorry for the late reply (but at least I reply, unlike the people “working” on the JS binding, if there are really people working on it).
I didn’t face the issue you had, sorry. The one I m facing now is that cc.PhysicsSprite.getBody() seems to not be implemented… so I can’t use physics at all. Could you call getBody() ?

AFAIK, physicsSprite.getBody should work, as long as you have previously setBody() on that physicsSprite.

the function is as simple as this

setBody:function (body) { this._body = body; }, getBody:function () { return this._body; },

I got a problem with Chipmunk when using the Javascript code I wrote for Cocos2d-html5 (which works fine on Chrome/Firefox) on iPhone.

are you saying it works on browser, but doesn’t work on JSB iphone?

One thing to check could be that objects gets deallocated when you try to access it.

because on native Cocos2d-iphone and -x, objects will be deallocated if you create them, but did not use them immediately

you could add to a layer or scene node, or manually call a function “.retain()”
this tells Cocos2d to not deallocate it, but you also need to call “.release()” when you are done with that object

are you saying it works on browser, but doesn’t work on JSB iphone?
Yes, that is exactly what I am saying. Here is the error I get : “cc.PhysicsSprite.getBody is undefined”
In the file “jsb_cocos2d.js”, if I add :
cc.PhysicsSprite.prototype.getBody = function() {
return this.getCPBody();
};
Then this error disappears, but it seems there are still plenty of things to implement to make it work. Are you sure it is already implemented? I didn’t find any sample using getBody()…

Edit: I just made a simple test, I wrote the 2 following lines
this.setBody(body); => works fine, at least it seems
this.getBody(); => generates the error “this.getBody is not a function”

It seems to be an issue of jsbinding. Issue http://www.cocos2d-x.org/issues/2113 was created.

Noel Meudec wrote:

> are you saying it works on browser, but doesn’t work on JSB iphone?
Yes, that is exactly what I am saying. Here is the error I get : “cc.PhysicsSprite.getBody is undefined”
In the file “jsb_cocos2d.js”, if I add :
cc.PhysicsSprite.prototype.getBody = function() {
return this.getCPBody();
};
Then this error disappears, but it seems there are still plenty of things to implement to make it work. Are you sure it is already implemented? I didn’t find any sample using getBody()…
>
Edit: I just made a simple test, I wrote the 2 following lines
this.setBody(body); => works fine, at least it seems
this.getBody(); => generates the error “this.getBody is not a function”

Thank you James!

Just a reminder : since getBody() doesn’t work with JSB, it is almost impossible to port games with physics from Cocos2d-HTML5 to Cocos2d-x with only the Javascript files.
It seems to be a low priority issue… sad, I see many games with physics (and I make some myself) which cannot be released on iPhone/Android.

Fixed at https://github.com/cocos2d/cocos2d-x/pull/3163 and https://github.com/cocos2d/cocos2d-x/pull/2869
Please have a try again.

I cannot compile when using the develop version … so I cannot test. Anything I should do?
I got errors like in CCGeometry.h :

inline Point compOp(std::function<float(float)> function) const => ERROR : no type “function” in namespace ‘std’
{
return Point(function(x), function(y)); => ERROR : used of undeclared identifier ‘function’
}

EDIT: I needed to compile using C++11, first time I had to do that so it took time to figure out how to do.
It is still not working though, it seems that some files are missing.

Noel Meudec wrote:

I cannot compile when using the develop version … so I cannot test. Anything I should do?
I got errors like in CCGeometry.h :
>
inline Point compOp(std::function<float(float)> function) const => ERROR : no type “function” in namespace ‘std’
{
return Point(function(x), function(y)); => ERROR : used of undeclared identifier ‘function’
}
>
EDIT: I needed to compile using C++11, first time I had to do that so it took time to figure out how to do.
It is still not working though, it seems that some files are missing (CCDeprecated.h, …).

Which platform are you using?

MAC OS X 10.8.4 (Mountain Lion)
Xcode 4.6.3
Target: iOS iPhone 6.1

How did you update the codes by git commands?

Noel Meudec wrote:

MAC OS X 10.8.4 (Mountain Lion)
Xcode 4.6.3
Target: iOS iPhone 6.1

I downloaded the last zip on github. It is not recommended ?

I took the last code on github just now, using git (https://github.com/cocos2d/cocos2d-x/commit/74002d48842cfa58d3e67c226cf398b97a7196d7).
I installed the templates, created a new project (_js), changed to C+11 and libc++.
When I compile : “CCDeprecated.h” file not found.

Yes, you should use git to clone the whole project.

git clone https://github.com/cocos2d/cocos2d-x.git
cd cocos2d-x
git submodule update --init

I’ve done it (see my previous message), same result. (I tested 2.1.4 to see if it could come from my side, but it works fine on my machine).

Sorry guys, I tried everything to compile, I updated to the latest version again, it simply won’t compile.
Steps (on MAC OS X 10.8.4 [Mountain Lion], Xcode 4.6.3, Target: iOS iPhone 6.1):
1/ Pull the latest version (6d958af23aa444afcf780e4c346da688146686a9)
2/ ./install-templates-xcode.sh uf
3/ Create a project (I used cocos2dx_js)
4/ Build => it doesnt’t work because std::function is not recognized (I would have created a separate branch of 3.0 preview… but it is just me)
5/ Edit build settings => use C11 and libc
6/ Build again => “CCDeprecated.h” file not found
7/ Comment “CCDeprecated.h” file include or copy/paste the file manually
8/ “etc/etc1.h” file not found
9/ I tried some more steps, but never could get it to compile. 2.1.4 works fine though.