Physics randomly frozen without error (Cocos2d-x project)

General discussion about the R.U.B.E editor
Post Reply
solidgoldrobot
Posts: 6
Joined: Wed Dec 11, 2013 12:20 am

Physics randomly frozen without error (Cocos2d-x project)

Post by solidgoldrobot »

Sometimes, when I load my physics scene, it is inexplicably frozen. I'll go back to another scene and retry the physics scene and it very randomly works and doesn't work.

I'm using RUBE layouts in a Cocos2d-x project with the loading / parsing code provided elsewhere on this site.

I did modify that code to parse multiple RUBE files into parallax layers (I'm using the editor to lay out everything, not just the physics), but my problem persists even when I remove this extra functionality.

I can trace out values and see that my bodies are awake and active, the world is not paused, update is getting called, etc, etc. But nothing is happening. Turning debug on / off has no effect. Can't drag bodies either. And there are no errors. Very puzzling.

Has anyone else experienced this? Theories? Suggestions?

Thanks for any help!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Physics randomly frozen without error (Cocos2d-x project

Post by iforce2d »

To clarify a bit more...
- what values are you tracing?
- are the bodies moving and just not being re-drawn, or are they not even moving?

Is the setImagePositionsFromPhysicsBodies function being called for the layer?
Are you calling the super-class function of all functions from RUBELayer that you have overridden? Eg.:

Code: Select all

void MyLayer::afterLoadProcessing(b2dJson *json)
{
    RUBELayer::afterLoadProcessing(json); // call superclass !
    // ... other stuff ...
}
Having said that though, randomly occurring problems are often caused by multiple threads doing things at different timing each run. Do you have multiple threads doing stuff?
solidgoldrobot
Posts: 6
Joined: Wed Dec 11, 2013 12:20 am

Re: Physics randomly frozen without error (Cocos2d-x project

Post by solidgoldrobot »

Thanks for your reply. I actually just edited the RUBELayer class to add the properties I needed for parallaxing layers, so it's not overridden, and everything is being called. I've logged each function as it's called to be sure, and everything works normally about 50% of the time.

I'm pretty certain this isn't a multiple thread issue either. I've run into those with Box2D before (they typically cause Box2D to throw errors, and I'm not getting any), so I'm pretty good about handling everything sequentially in the update method. I will continue to look for mistakes I've made along this line, though.

You asked if the bodies were moving, so I started logging the y velocity of a dynamic body in the update method. It always came up zero even though it was awake, active, and "in mid-air" (should be falling). So, I added an applyLinearImpulse call if it's velocity is zero. Now it traces the velocity it would have at the moment of impulse (never decaying as it should).

So it seems like it is making one successful time step, and then Box2D quits responding... without errors. Animations keep running, update keeps getting called, body properties keep returning values, etc... the bodies just stop moving.

I realize it's unlikely this has anything to do with RUBE itself... just looking for help brainstorming new things to try :)

Thanks!
solidgoldrobot
Posts: 6
Joined: Wed Dec 11, 2013 12:20 am

Re: Physics randomly frozen without error (Cocos2d-x project

Post by solidgoldrobot »

Ok, I stripped it down to nothing but a few bodies with a debug draw and the issue still occurred (and just one layer like the original RUBELayer class uses). However, when I commented out the initial clear() call in the loadWorld() function, the problem was gone!

So, I'm studying that to figure out what is going on. I'll compare with your unaltered RUBELayer class to make sure I didn't change something and forget about it ;)
solidgoldrobot
Posts: 6
Joined: Wed Dec 11, 2013 12:20 am

Re: Physics randomly frozen without error (Cocos2d-x project

Post by solidgoldrobot »

Ok, I figured it out, and it was entirely my fault (no big surprise). But I will post the solution in case anyone else is trying to do the same thing and they make the same mistake.

I modified RUBELayer to parse multiple RUBE layouts into separate parallaxing layers (only one of the layers contains physics bodies, the others are just foreground and background). It was creating empty b2worlds for all layers and sometimes m_world was ending up assigned to one of these instead of the world with my bodies in it. So no errors were being thrown and the world with bodies was never being time-stepped.

I just needed to add a condition within the j2b2World method so it will not try to create a b2world if there are no bodies in the parsed json, allowing me to use this approach for multiple layers.

I don't know if anyone else is tampering with the code in this way, but if you are, that's something to watch out for ;)
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Physics randomly frozen without error (Cocos2d-x project

Post by iforce2d »

Glad to hear you got it figured out :) Thanks for letting us know what the issue was.
Post Reply