About cocos2d-X 3.x use the engine's built-in physical engine Physics, cocos2d-xphysics
Today I was planning to use BOX2D physical engine, think of some time ago I heard that cocos2d-X in version 3.0 encapsulated a physical EnginePhysicsListening to the name is very domineering. It is actually much easier to use than BOX2D (after all, it is based on the BOX2D encapsulation). Well, now let's take a look at how to use the Physics physical engine,
Step 1: Create a project...
Part 2: Modify the scenario Creation Method
Replace Scene: create (); with Scene: createWithPhysics ();
Scene * HelloWorld: createScene () {// use the physical world to create a scenario. auto scene = Scene: createWithPhysics (); // display the physical world debugging status. A red box is displayed, convenient debugging of scene-> getPhysicsWorld ()-> setDebugDrawMask (PhysicsWorld: DEBUGDRAW_ALL); auto layer = HelloWorld: create (); scene-> addChild (layer); return scene ;}
Part 3: create a world box in onEnter ()
Void HelloWorld: onEnter () {Size visibleSize = Director: getInstance ()-> getVisibleSize (); // create a physical world. The Size is the same as the screen Size. Use the default material, the width of the debug box is three pixels: auto body = PhysicsBody: createEdgeBox (visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); // create a collision Image auto edgeShape = Node: create (); // bind the image to the created world with edgeShape-> setPhysicsBody (body); // set the position of the image in the middle of the screen edgeShape-> setPosition (visibleSize. width/2, visibleSize. height/2); // Add the layer addChild (edgeShape );}
Let's see, a physical world of the same size as our screen is successfully created.
Part 4: Add a physical genie
Let's write a function.
Void HelloWorld: addSprite (cocos2d: Vec2 pos) {auto body = Sprite: create ("CloseNormal.png "); // set a square to bind to the sprite body-> setPhysicsBody (PhysicsBody: createBox (body-> getContentSize (); body-> setPosition (pos ); addChild (body );}
We call this function in init () and set this genie in the middle of the screen.
addSprite( Vec2( visibleSize.width / 2, visibleSize.height / 2));
Check whether something is falling, and the speed is getting faster and faster.
Yes, this image looks like a circle. Let's create a round collision image.
body->setPhysicsBody( PhysicsBody::createBox( body->getContentSize()));
Change
body->setPhysicsBody( PhysicsBody::createCircle( body->getContentSize().width / 2));
See if the image we created is a circle.
Well, it looks boring. Let's listen to another event and click it to make it more fun.
In init (),
// Create a single Touch event listener auto listener = EventListenerTouchOneByOne: create (); // the listener-> onTouchBegan = [this] (Touch * t, event * e) {this-> addBall (t-> getLocation (). x, t-> getLocation (). y); return false ;}; // bind the listening object Director: getInstance ()-> getEventDispatcher ()-> addEventListenerWithSceneGraphPriority (listener, this );
Okay, you're done.
Physical detection collision callback in cocos2d-x
How to Use Xcode tools to add Box2D physical engine to the cocos2d-x framework?
Can't find file error? I have been entangled in this error for a long time. All the settings in the Search Path are set. m files are all changed. mm, that is, the error is always reported. If you have searched for N posts, you can use this setting, but I have not successfully built it. Thank you very much for your help.