Cocos2dx - Chipmunk c++

Hello! Please can someone send a sample code with debug line, etc using the Chipmunk c++? I can’t find this in anywhere. I think the tutorial from cocos is a bit old and does not work :frowning: Thanks!!

http://cocos2d-x.org/docs/programmers-guide/physics/index.html

I am having the same issue. I don’t understand why this following code snippet works, but it does and it will allow you to use physics in your scene:

Scene* HelloWorld::createScene()
{
	// create the scene with physics enabled
	auto scene = Scene::createWithPhysics();

	// set gravity
	scene->getPhysicsWorld()->setGravity(Vec2(0, -900));

	// optional: set debug draw
	scene->getPhysicsWorld()->setDebugDrawMask(0xffff);

	auto layer = HelloWorld::create();
	scene->addChild(layer);

	return scene;
}

What confuses me is the second part, where a HelloWorld (inherits from scene) object is created, and wherein the physicsworld scene adds the HelloWorld object as its child.

I am beyond baffled. Cannot post on forums yet so if anyone has reasons etc please let me know.
Hopefully this was of help to you Pliskeviski.

Hi, you can also do something like this:

 Scene* HelloWorld::createScene()
    {
    	HelloWorld* scene = new (std::nothrow) HelloWorld();
    	if (scene && scene->initWithPhysics() && scene->init())
    	{
    		scene->autorelease();
    		// custom code
    		scene->getPhysicsWorld()->setGravity({ 0.0f,-900.0f });
    		scene->getPhysicsWorld()->setSubsteps(3);
    
   		    // return the scene
    		return scene;
    	}
    	CC_SAFE_DELETE(scene);
    	return nullptr;
    }

Edit: Thank you VERY much for posting your code.

I am very, very aggravated. I have spent over a day trying to understand this, and looking for code to add physics to an already existing scene. WHY IS THE FUNCTION INITWITHPHYSICS() NOT INCLUDED IN THE SCENE CLASS REFERENCE http://www.cocos2d-x.org/docs/api-ref/cplusplus/v3x/d4/d5f/classcocos2d_1_1_scene.html

I know this is an open source project and I respect that, but this is just redic. How can I add these important functions to the documentation?

I think the class reference gets generated automatically so I don’t know how to change it. That also explains why there are quite some functions undocumented, that’s why I prefer to read the source code instead of the reference.

As a side note: The snipet you were using before is from a time when you needed a Layer object to be able to receive touch events, so the idea was that instead of making you’re “scene class” inherit from Scene you made it inherit from Layer and then attached that layer to a normal Scene, so in that case the HelloWorld class would be a Layer and not an Scene. Right now all nodes can receive touch input so it is not necessary anymore.

1 Like

Good advice zero. From now on I will also just read the source code. They really aught to just delete the documentation, though.

Thank you for all of your help with explanations and code.

If it is precise physics, it is recommended to use box2d, because the chipmunk has a lot of small problems enough to let you give up

1 Like

Could you describe what type of problems?
Thanks

Such as collision rebound angle deviation
The rebound is unstable
You can not set the gravitational scaling of a single object
Bullets penetrate
Sphere rolling probability (5%) suddenly bounced
And so on, there are many small problems,
But I used the box2d after, these problems will not happen

1 Like

Didn’t know it had all these problems.
Can’t they be solved by just increasing the number of sub-steps?

Yes, I spent about two weeks to google search and debug these small problems, but unfortunately, ultimately did not really solve, that time really helpless

The beginning also worried that box2d will have the same problem, but fortunately, when the project development is completed, box2d is still very good.

Have you tried to ask on the chipmunk forum and show this problem?
I just wonder, but I never used it, I like Box2D.

Mainly chipmunk little problem too much, if each question to ask questions and solve, will seriously affect the progress of the project

I see… but maybe it’s like a couple of bugs? I also decide not to use it for lack of CCD(Continuous Collision Detection).