the c++ Box2d on android cause the values NaN

General discussion about Box2D tutorials
Post Reply
marvelyyc
Posts: 5
Joined: Sun Aug 31, 2014 6:05 am

the c++ Box2d on android cause the values NaN

Post by marvelyyc » Sun Aug 31, 2014 6:10 am

hi guys I have problem about the Box2D c++ on android.
I use the coco2dx to build my game.It run well in the win32.
But when i put it on the android.It will be crash.
I found the Box2d world-step() cause the values become NaN

I don't know how to debug the C++ on eclipse.So i use the CCLOG.(anyone can debug the cocos2dx on eclipse?)

this is my LOG,the body's position angle worldcenter become the NaN after world->step

Code: Select all

08-31 09:55:58.862: D/cocos2d-x debug info(4829): before world->step
08-31 09:55:58.862: D/cocos2d-x debug info(4829): angle is:0.000000
08-31 09:55:58.862: D/cocos2d-x debug info(4829): pos is:-0.576004, 3.446330
08-31 09:55:58.862: D/cocos2d-x debug info(4829): worldCenter is:-0.576004, 3.446330
08-31 09:55:58.862: D/cocos2d-x debug info(4829): lovalCenter is:0.000000, -0.000000
08-31 09:55:59.177: D/cocos2d-x debug info(4829): after world->step
08-31 09:55:59.177: D/cocos2d-x debug info(4829): angle is:nan
08-31 09:55:59.177: D/cocos2d-x debug info(4829): pos is:nan, nan
08-31 09:55:59.177: D/cocos2d-x debug info(4829): worldCenter is:nan, nan
08-31 09:55:59.177: D/cocos2d-x debug info(4829): lovalCenter is:0.000000, -0.000000
So anyone can help me with this?I will mad....

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: the c++ Box2d on android cause the values NaN

Post by iforce2d » Sun Aug 31, 2014 6:23 am

Most likely you are originally giving Box2D that NaN value yourself, for example in user-controlled parameters for ApplyForce or something, and then it spreads like a virus to all other values it is calculated with. Check all the values you give as input to Box2D functions.

This might help:
http://stackoverflow.com/questions/2529 ... own-reason

marvelyyc
Posts: 5
Joined: Sun Aug 31, 2014 6:05 am

Re: the c++ Box2d on android cause the values NaN

Post by marvelyyc » Sun Aug 31, 2014 9:06 am

Thank your reply.I found the reason.

Because i add too many body in the World.If add small numbers bodies it will be run well!!
If i add too many bodies it will be NaN!!
But why? why the Box2d C++ on android have this problem?How did you guys fix this problem?

And when i use the RUBE to build Box2D file.ANd export the .json file.I found that all the files can be loaded on WIN32. But some file can't be loaded on the Android with b2dJson.Is there any rules about the .json?

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: the c++ Box2d on android cause the values NaN

Post by iforce2d » Sun Aug 31, 2014 5:31 pm

I don't think having a large number of bodies would cause NaNs to start appearing. It would just make things run very slowly. It would also be a very bad bug in Box2D if that was true. Just imagine how many hundreds of people would have this same issue if it was simply caused by having a lot of bodies. Almost every person who tried Box2D programming would probably have it some time or another :)

I don't think it's an issue with Box2D C++ on Android per se... I mean, consider that the Box2D source code is the same as on Windows right? I think it's more to do with input values you are giving, which could be related to the different control schemes between these two platforms (I am assuming that when you say Win32 you mean regular Windows, using mouse and keyboard input).

Does the problem occur if you don't do any input? That is, if you just set it up to run without using any platform-specific code to get touches etc?

Did you try using the b2Asserts as in the Stack Overflow thread I linked to?

What do you mean by "can't be loaded"?

marvelyyc
Posts: 5
Joined: Sun Aug 31, 2014 6:05 am

Re: the c++ Box2d on android cause the values NaN

Post by marvelyyc » Mon Sep 01, 2014 12:00 am

Thank your for reply.
All the input values is set in the RUBE. I don't do anything after the .json file loaded.

"can't be loaded" means:sometimes when l load the .json file on the android.The app maybe crash on the function "world = json.readFromString(content, errMag);".
I think is my fault.Because some .json files can be loaded.some not.Maybe the RUBE setting that I did cause this.

By the way iforce2d your source code "rube-cocos2d-x-V3-sample" will be breakdown on the Android too.
UIControlsRUBELayer is OK, BasicRUBELayer and RUBELayer is not OK.
I think maybe the version of cocos2dx cause this.!I use the cocos2dx 3.2.
I will try the other version of cocos2dx and your method.

Thank you very much.

marvelyyc
Posts: 5
Joined: Sun Aug 31, 2014 6:05 am

Re: the c++ Box2d on android cause the values NaN

Post by marvelyyc » Mon Sep 01, 2014 6:30 am

The problem has been fixed!Although i don't the reason.
I change my cocos2dx version from 3.3 to 3.1.So it works well.
If someone have this problem can try this method.

Thank you iforce2d. Thank you for your patience and help all the time.

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: the c++ Box2d on android cause the values NaN

Post by iforce2d » Mon Sep 01, 2014 1:22 pm

Yes, the cocos2d-x v3 sample was written for 3.0rc I think. The cocos2d-x framework seems to change so fast and so drastically that there is not much incentive to continuously keep it up to date with every change. They also seem to have very little regard for backward compatibility. I am still using v2.1.5 which works just fine, but is completely different to 3.0, to the point where I spent an entire day trying to convert an app to 3.0 and eventually gave up and reverted, since it didn't seem worth the effort.

Post Reply