Hello,
I just started new project. I created it in Cocos Studio 2. I don’t want to use Chipmunk. I need only Box2D.
When I downloaded Box2D from GitHub - erincatto/box2d: Box2D is a 2D physics engine for games and dropped it into my Xcode project I got error:
/Box2D/Collision/Shapes/b2ChainShape.cpp:29:20: Out-of-line definition of 'Clear' does not match any declaration in 'b2ChainShape'
Well, I just removed that method:
//Clear()
After that I got many errors:
/Box2D/Dynamics/b2World.cpp:40:2: Use of undeclared identifier ‘g_debugDraw’; did you mean ‘m_debugDraw’?
Can someone please help me? Is there any tutorial, how to integrate Box2D with latest cocos2d-x ?
Also, when I created project in Cocos Studio 2 and now I have compiled libs in my project. So how can I edit config.h file ? For example change value of SPRITE_DEBUG_DRAW?
Don’t drop anything in to anywhere, get rid of whatever you added to xcode, it’s already part of the Cocos2D-x framework. Simply just include it:
#include "Box2D/Box2D.h"</blockquote
If you want to edit anything in the engine, including config.h then you need the source code version. Download that from here http://www.cocos2d-x.org/download click the “Download V3.9” button.
If you create a project with Cocos Studio you will get the pre-built libraries version. It’s faster to build but you have the disadvantage of not being able to edit the engine code or config (I assume).
Check this :
You need to tell the compiler to use Box2d using preprocessor flags
Thank you! It works.
Also, maybe you can help me with a simple project that includes standard tick method, debug draw and some sprites with 3.9? Like was in cocos2d-iphone default template. Because I can’t find any tutorial about that. I just created a new project in Cocos Studio 2 and it’s not clear what to do next to get Box2D work.
So, just a simple example really can help me.
Do I need to set it? My code just compiles ok now… I just did as @UKDeveloper99 said.
There’s no need for any preprocessor flags, it should all be setup to go.
You should try and think of cocos2d-x and box2d as 2 separate things. Box2D is the physics part only. Cocos2D-x provides the visualization of the physics. So when you create a physics body and you want a sprite to move as though it is attached to the body. Then every update call you need to set the sprite position to the body position and set the sprite rotation to the body rotation (angle) except it will be negative -angle.
Look at the HelloWorldScene class when you create a new project. Add a function in the header file for the update function. It’s virtual so look at how it’s define in the parent class (Layer/Node). Then you need to call this->scheduleUpdate(); in the init function of HelloWorldScene so that the update function is called every frame.
To set box2d up, look at some box2d tutorials on google, how to create a world and some basic bodies (e.g. boxes).
Then to simulate (run) the physics just call the step function in your update function you created earlier like so:
m_world->Step(1/60.0, 8, 3);
I see, thanks.
I have experience with Box2D, but with cocos2d-iphone. Now I want to move to cocos2d-x 3.9
Just I want to have a simple project with latest cocos2d, that includes: debug draw and a couple of sprites. That would be a good start for me. Can you please share such project with me?
In cocos2d-iphone, when I created a new project I see all this right after. But with current cocos2d-x there is no any pointers for that…
Sorry I think I’ve provided enough guidance here. For debug draw look for a GLESRender class on google, there is one packed in to the cocos engine somewhere but I can’t remember where for the life of me.
THanks anyway. But thats not so simple for me… I’m also new to C++, so just working project can help me a lot… Maybe someone else, can share simple project… I would really appreciate that.