Physics design for v3.0

Hello everyone, I’m working on designing and implementing physics in cocos2d-x v3.0. This is the design for now:
classes:
PhysicsWorld: The world simulates all the physics effects. It will create automatically if you use Scene::createWithPhysics(), and you can get it from scene->getPhysicsWorld().
PhysicsBody: The body works with PhysicsWorld, if you create a node, set a PhysicsBody to it (use node->setPhysicsBody()), and add to the scene (or layer in the scene), it will take effects with the PhysicsWorld.
PhysicsShape: The shape of the body, a body can have many shapes to combine it to a complex shape (like a tank). each shape can have different material(see blow).
PhysicsMaterial: It has 3 properties: density, elasticity, and friction.
PhysicsJoint: It’s a joint connects two physics bodies together.
PhysicsContact: It will create automatically when two body contact with each other, you can create a PhysicsContactListener and set it to PhysicsWorld to receive the contact callback and deal with it.

Here is something I want to discuss with you.

  1. About mass of a body. as we know in reality, a body has big mass is hard to move. so in our engine, how do we deal with the relationship between body and shape? Does a body have mass? How about shape? In my design, they both have mass, and I add a density property in shape(see PhysicsMaterial), so if you create a shape with density != 0, it will have a mass (will calculate automatically with the area of the shape, or you can set it with shape->setMass()), if you add it to a body, the body’s mass will be increased (and remove it will be decreased), you can also set the number of mass to the body, but the mass also will be increased/decreased when you add/remove a shape.
  2. The relationship between Physics and our engine. Now is how the physics works: you create a scene with Scene::createWithPhysics(), set the PhysicsBody to the node, and add the node to scene use addChild(). But there is some limits: The physics only works when you add the node to the scene or layer under the scene. Because our engine is a tree structure but but Physics is a container structure (all the bodies are under the world), I think it’s easy to understand and easy to maintain if physics only works with the node in the scene or in the layer which under the scene. What’s your opinion?
  3. Because I have very less experience with physics engine, so I want to know what’s your opinion with my design of the physics APIs? Is there a problem in some circumstances? or is there a simple idea of this? Tell me if you have a better idea and I will be very appreciated, thank you!

You can get the latest code of cocos2d-x with physics from my git repository: https://github.com/boyu0/cocos2d-x.git branch: iss2771_physical

There is some test in TestCpp, under the PhyscisTest, you can see the effects and test it in there. I’m coding in mac, it works well with mac and ios simulator, I don’t know it’s ok or not in other platforms, you can run this test in mac.

Hello boyu0,

How to create a Physicsbody that is unbreachable? (No other physicsbody can pass through it)

Thank you,
Trix