3.0 new highlights: the simplest physical engine in history
Using a physical engine in a Cocos2d-x3.0 can be a pleasure, because a lot of tedious stuff is encapsulated for us.
So, I am about to start creating the game's level scenario, and everyone is keeping up.
We name the level scenario toll1_scene. the header file is as follows:
- # Ifndef toll1_scene_h
- # Define toll1_scene_h
-
- # Include cocos2d. h
- USING_NS_CC;
- Class toll1_scene: public Layer
- {
- Public:
- ~ Toll1_scene ();
- Static Scene * scene ();
- CREATE_FUNC (toll1_scene );
- Virtual bool init ();
-
- Virtual void onExit () override;
- };
-
- # Endif
The header file is nothing special, just like the header file in common scenarios.
Then, let's take a look at the cpp file:
- # Include toll1_scene. h
- # Include BackgroundLayer. h
-
- Toll1_scene ::~ Toll1_scene ()
- {
- }
-
- Scene * toll1_scene: scene ()
- {
- Auto scene = Scene: createWithPhysics ();
-
- /* The world of gravity */
- Vect gravity (0,-0.5f );
- Scene-> getPhysicsWorld ()-> setGravity (gravity );
-
- /* Enable the test mode */
- Scene-> getPhysicsWorld ()-> setDebugDrawMask (PhysicsWorld: DEBUGDRAW_ALL );
-
- // Create a boundary
- Size visibleSize = Director: getInstance ()-> getVisibleSize ();
-
- /*
- Create a hollow box rigid body that serves as the boundary of our game world (to prevent in-game objects from running out of the screen)
- The parameters are the rigid body size, material (in fact, Some preset configuration data), and edge thickness.
- */
- Auto body = PhysicsBody: createEdgeBox (Size (visibleSize. width, visibleSize. height), PHYSICSBODY_MATERIAL_DEFAULT, 3 );
-
- /* Create a node to hold the rigid body so that the rigid body can participate in the physical world of the game */
- Auto node = Node: create ();
- Node-> setPosition (Point (visibleSize. width/2, visibleSize. height/2 ));
- Node-> setPhysicsBody (body );
- Scene-> addChild (node );
-
- Auto layer = toll1_scene: create ();
- Scene-> addChild (layer, 10 );
-
- Return scene;
- }
-
-
- Bool toll1_scene: init ()
- {
- If (! Layer: init ())
- {
- Return false;
- }
- Return true;
- }
-
- Void toll1_scene: onExit ()
- {
- Layer: onExit ();
- }
It's too complicated. I want to explain it in several parts (Xiao RuO: But it's very easy at first !)
Integration with the physical world
It is very easy to create a physical world in 3.0. Previously we created a scenario and called Scene: create (). To create a physical scenario:
auto scene = Scene::createWithPhysics();
Therefore, this scenario is capable of the physical world, so we should not do anything else.
Set the gravity direction of the game
Next we will set a gravity direction for the physical world. This direction will be liked by everyone, and I will also set it at will. Different Directions will also affect the operation of the game, maybe it's even more interesting.
To operate on the physical world, you must obtain the physical world objects through the scenario and then perform the following operations:
/* The world of gravity */
Vect gravity (0,-0.5f );
Scene-> getPhysicsWorld ()-> setGravity (gravity );
/* Enable the test mode */
Scene-> getPhysicsWorld ()-> setDebugDrawMask (PhysicsWorld: DEBUGDRAW_ALL );
Vect is a vector that represents the x and y directions. In fact, it is a Point object.
The following code seems to be in disorder? Yes, that is to enable the debugging mode. This function is very good. When the debugging mode is enabled, all object shape nodes and items will be drawn for testing. Of course, there is a parameter that can set the parts you want to draw.
Create a rigid body in the simplest way
Generally, we need to create a rigid body, a shape, and various settings. In section 3.0, you do not need to use the following code:
/*
Create a hollow box rigid body that serves as the boundary of our game world (to prevent in-game objects from running out of the screen)
The parameters are the rigid body size, material (in fact, Some preset configuration data), and edge thickness.
*/
Auto body = PhysicsBody: createEdgeBox (Size (visibleSize. width, visibleSize. height), PHYSICSBODY_MATERIAL_DEFAULT, 3 );
The createEdgeBox function is used to create a hollow box rigid body. For more functions, see the header file of PhysicsBody.
In the createEdgeBox function, the shape has been created and added to the rigid body.
Add a rigid body to the physical world
/* Create a node to hold the rigid body so that the rigid body can participate in the physical world of the game */
Auto node = Node: create ();
Node-> setPosition (Point (visibleSize. width/2, visibleSize. height/2 ));
Node-> setPhysicsBody (body );
Scene-> addChild (node );
Don't forget, the physical world is our scenario, the scenario is the world, and the world is the scenario. According to this idea, to add a rigid body to the physical world, we need to add it to the scenario. How can we add a rigid body to the scenario? AddChild directly?
No ~! The scenario is used to add node objects. Therefore, we put the rigid body in the node and then add the node to the scenario. Do you understand? (Xiao RuO: Oh ~ It turns out this way (not fully understood ))
In fact, it is easy to understand that a rigid body is only a simulation of data. It is invisible, and the objects in the game need to be represented by various images and animations.
Therefore, the Node object is used for representation, and the rigid body object is used for physical simulation. The combination of the two is perfect.
After a rigid body is added to a node, it can be obtained through the getPhysicsBody function.
Running Effect
OK. Check the running effect. Before that, modify the AppDelegate. cpp file and make some configuration in the applicationDidFinishLaunching function:
- Bool AppDelegate: applicationDidFinishLaunching (){
- // Initialize director
- Auto director = Director: getInstance ();
- Auto glview = director-> getOpenGLView ();
- If (! Glview ){
- Glview = GLView: create (Don't Save Me !);
- Director-> setOpenGLView (glview );
- }
-
- /* Set the Win32 screen size to 480X800 ,*/
- Glview-> setFrameSize (480,800 );
-
- /* Simple screen adaptation, proportional stretch, black edges may exist */
- Glview-> setDesignResolutionSize (480,800, ResolutionPolicy: SHOW_ALL );
-
- Director-> setDisplayStats (true );
- Director-> setAnimationInterval (1.0/60 );
-
- /* The initial scenario is the level scenario */
- Auto scene = toll1_scene: scene ();
- Director-> runWithScene (scene );
-
- Return true;
- }
The screen size is set to 480X800 (this is free, only the size of windows runtime ).
The game design size is set to 480X800. This size determines the specifications of the game materials. I only use one set of resources for adaptation. Then, this is a game in the portrait direction.
I use the simplest SHOW_ALL mode for screen adaptation. Some mobile phones have black borders.
Then the initial running scenario is toll1_scene. Okay, run the game and the effect.
Pay attention to the red lines at the edge of the screen. This is the hollow box object we have created. The debugging mode is enabled, so it will be drawn.
With this hollow box, the objects in the box won't just run out of the screen.
Okay. Next, we will join the background of the game.