Bugs in Cocos2dx v3.x Physics?

I am using cocos2dx v3.4

My game needs Physics. So, I used the Integrated Physics available in cocos2dx. After adding physics to the game, there was lag in the game.

I then searched for solutions. I noticed many people telling that the Integrated Physics in cocos2dx v3.x is a basic version and it has a lot of bugs which slows down the game. Is this true?

Should I use box2d to implement Physics in my game?

@Heyalda: I have seen a post in which you have posted about the bug in Physics. Can you help? :slight_smile:

The built in physics engine is buggy and there are open bugs for most of the issues that I am aware of.

But, in my opinion, the built in physics engine is easier to use than using Chipmunk or Box2d directly, and being an integrated part of Cocos2d-x means the built in physics “wrapper” provides an interface that is aligned with the rest of Cocos2d-x, so if you have time you might want to consider using the built in solution and waiting for the fixes to come.

Here is some physics engine foundational information that I think is important to understand: Implementing physics with Chipmunk or Box2d both have their challenges; the biggest being that it is more than just using an API… the values you choose for gravity, mass, updates per loop, etc all need to create a system that will result in a stable numerical integration capable of running on a device of the least performance you plan to support. Basic tuning of values selected for objects and the number of updates per frame update are a good place to start with the necessary tuning of the physics engine.

With that said, there are many things that could possibly be causing the lag.

For example, maybe you have a lot of graphics scaling and rendering that pushes a device to the brink of lagging, and then adding the physics is the straw that breaks the camels back. Or maybe the physics is eating a lot of CPU and causing the rest of your game to suffer.

It might be helpful for me to see a video of what you are seeing as lag.

Does your lag look anything like this video? https://www.youtube.com/watch?v=MkgolnJDjlo&feature=youtu.be
If yes, you should check out this post: http://discuss.cocos2d-x.org/t/does-the-built-in-physics-engine-physicsworld-update-need-a-low-pass-filter-for-dt/21534/6

The only way to know for sure is to profile your game to see what is using the CPU the most.

I personally like to use Xcode and Instruments for profiling. Then I can do a time profile and see what processes are using the most CPU. You can even drill down to see which specific objects in your code are the most CPU hungry.

I’ve used low pass filter in the update method of Physics World but still there is lag!

The built in physics seems to be a wrapper around the chipmunk 7.0 physics. The chipmunk physics api is in there, it’s written in C and you can do all your physics work from the c API.

Here’s the documentations: http://chipmunk-physics.net/release/ChipmunkLatest-Docs/

I was looking into it, if you’re use to cocos2d-x it shouldn’t be a problem. Since this is my first cocos project I’m moving a bit slowly.

To get started in your AppDelegate.h under include “cocos2d.h” add import for #include “chipmunk.h”

then you can start using the Chipmunk C API

example:

cpSpace *space_ = cpSpaceNew();
cpSpaceSetGravity(space_, cpv(0.0f,-100.0f));

To be honest once you import the chipmunk.h following the documentation link I put above is quite straight forward.

its not 7.0, its 6.2 (at least cocos 2d-x 3.6)

nevertheless I haven’t had any issues with using the Chipmunk C API and it’s a lot more powerful than the built in one. I don’t know cocos well enough to write a wrapper class but C stuff isn’t too bad.