Cocos2d-x 3.x integrates a physics world with scenes and nodes. A PhysicsBody belongs to a node, while contact events are delivered through EventListenerPhysicsContact.
Create a physics scene
1 | local scene = cc.Scene:createWithPhysics() |
Disable debug drawing in production.
Add a boundary body to a node:
1 | local edge = cc.PhysicsBody:createEdgeBox( |
Create a dynamic sprite:
1 | local ball = cc.Sprite:create("ball.png") |
Listen for contacts
1 | local listener = cc.EventListenerPhysicsContact:create() |
The category mask identifies what a body is. The collision mask determines which categories produce physical collision response, and the contact-test mask determines which interactions produce callbacks. A pair is considered according to both bodies’ masks, so define named bit constants instead of unexplained hexadecimal values.
Contact callbacks run during the physics step. Avoid immediately removing bodies or restructuring the scene in ways the engine version cannot safely handle; queue destructive game actions for the next update when necessary.