Lame to note 39-cocos2d-x-3.2 Box2d physical engine free-body implementation
Previous review
This famous saying goes:Troubles have greatly affected me ...... I have not received the monthly salary for more than a year, and I am struggling with poverty. I am very lonely in my troubles, and I have a lot of troubles, which is more powerful than art![Picasso]
I went to Beijing on a business trip some time ago. Fortunately, I came back before the Mid-Autumn Festival. Haha.
In the previous article, he learned the Ray-cast code explanation in the Box2d physical engine and continued to learn how to use other Box2d engines. This time, we use the Box2d physical engine to learn about physical drops.
Introduction
The theoretical knowledge is basically the same as that of RayCast. The following content must be added:
1. The Body defines variables.
The b2BodyDef struct is defined in the b2Body. h header file. The struct is defined as follows, and some of them contain default variables.
B2BodyDef ()
{
UserData = NULL;
Position. Set (0.0f, 0.0f );
Angle = 0.0f;
LinearVelocity. Set (0.0f, 0.0f );
AngularVelocity = 0.0f;
LinearDamping = 0.0f;
AngularDamping = 0.0f;
AllowSleep = true;
Awake = true;
FixedRotation = false;
Bullet = false;
Type = b2_staticBody;
Active = true;
GravityScale = 1.0f;
}
The role of this struct is that when we create a Body, we must have this struct as a parameter, and then create the desired Body based on this struct parameter. This time we will focus on the types below.
UserData is used to store custom data of applications.
Position. Set is used to Set the initial position for Body creation.
Angle is used to set the Body radians.
LinearVelocity. Set is used to Set the line speed of the Body. The default value is 0.
AngularVelocity is used to set the angular velocity.
LinearDamping linear damping is used to reduce the linear speed. This value can be set to greater than 1, but greater than 1 will cause the damping effect to be more sensitive to the time step.
AngularDamping Angular Velocity damping is used to reduce the angular velocity. This value cannot be too large, which leads to the damping effect being sensitive to the time step.
AllowSleep is TRUE by default, indicating whether the Body can sleep. If it is set to FALSE, the CPU usage will increase.
Awake = true; indicates whether the Body is sleep at the beginning.
FixedRotation = false; whether the Body can be rotated. The default value is FALSE. This is useful for roles.
Bullet = false; this is used to mark whether the Body is a fast moving Body. If it is a fast moving Body, it is marked as True to prevent penetrating other moving bodies. This can only be set for the moving Body. If it is not enough, it will increase the CPU processing time.
Type = b2_staticBody; the default value of type is 0, which is the static Body. The static BODY has no quality or speed and can only be moved manually. If the value is 1, it is the Body of the motion. There is no quality, but there is a non-zero speed (which can be set by the programmer), which can be moved through the parser. Value 2 indicates the dynamic Body, with a non-negative mass, non-zero speed (determined by external force), and motion through the parser. It is an enumeration type, as shown below.
Enum b2BodyType
{
B2_staticBody = 0,
B2_kinematicBody,
B2_dynamicBody
};
Active = true; Mark whether it is active.
GravityScale = 1.0f; the gravity amplification factor of the Body.
2. Fixture Definition
The Fixture definition is used to create a Fixture. The definition is as follows:
B2FixtureDef ()
{
Shape = NULL;
UserData = NULL;
Friction = 0.2f;
Restitution = 0.0f;
Density = 0.0f;
IsSensor = false;
B2Filter filter;
}
Shape must be set. This shape will be copied, so the previous shape variables can be used by others.
UserData is used to store specific data of an application.
Friction specifies the friction, usually 0 to 1
Restitution specifies the reply force. Usually 0 to 1
Density indicates Density
IsSensor indicates whether it is a sensor. If it is a sensor, it will collect information but will not respond to the collision.
Filter filters exposed DATA
Procedure
We will use the last learning template.
Http://download.csdn.net/detail/notbaron/7871237
Download this code.
1. Modify the RayCastNew constructor as follows:
RayCastNew ()
{
B2Vec2 gravity;
Gravity. Set (0.0f,-10.0f );
M_world = new b2World (gravity );
B2Body ** bodies = (b2Body **) b2Alloc (3 * sizeof (b2Body *));
{
B2BodyDef bd;
Bd. type = b2BodyType (0 );
Bd. position. Set (2.587699890136719e-02f, 5.515012262131709e + 00f );
Bd. angle = 0.20.00000000000e + 00f;
Bd. linearVelocity. Set (0.20.0000000e + 00f, 0.20.0000000e + 00f );
Bd. angularVelocity = 0.20.00000000000e + 00f;
Bd. linearDamping = 0.20.00000000000e + 00f;
Bd. angularDamping = 0.20.00000000000e + 00f;
Bd. allowSleep = bool (4 );
Bd. awake = bool (2 );
Bd. fixedRotation = bool (0 );
Bd. bullet = bool (0 );
Bd. active = bool (32 );
Bd. gravityScale = 1.20.0000000e + 00f;
Bodies [0] = m_world-> CreateBody (& bd );
{
B2FixtureDef fd;
Fd. friction = 2.000000029802322e-01f;
Fd. restitution = 0.20.0000000e + 00f;
Fd. density = 1.20.0000000e + 00f;
Fd. isSensor = bool (0 );
Fd. filter. categoryBits = uint16 (1 );
Fd. filter. maskBits = uint16 (65535 );
Fd. filter. groupIndex = int16 (0 );
B2PolygonShape shape;
B2Vec2 vs [8];
Vs [0]. Set (7.733039855957031e-01f,-1.4900000034084320e-01f );
Vs [1]. Set (-4.487270116806030e-01f, 1.138330027461052e-01f );
Vs [2]. Set (-1.880589962005615e + 00f,-1.365900039672852e-01f );
Vs [3]. Set (3.972740173339844e-01f,-3.897832870483398e + 00f );
Shape. Set (vs, 4 );
Fd. shape = & shape;
Bodies [0]-> CreateFixture (& fd );
}
}
{
B2BodyDef bd;
Bd. type = b2BodyType (2 );
Bd. position. Set (-3.123168977050781e-02f, 7.535382270812988e + 00f );
Bd. angle =-1.313644275069237e-02f;
Bd. linearVelocity. Set (8.230687316837036e-01f, 7.775862514942587e-02f );
Bd. angularVelocity = 3.705333173274994e-02f;
Bd. linearDamping = 0.20.00000000000e + 00f;
Bd. angularDamping = 0.20.00000000000e + 00f;
Bd. allowSleep = bool (4 );
Bd. awake = bool (2 );
Bd. fixedRotation = bool (0 );
Bd. bullet = bool (0 );
Bd. active = bool (32 );
Bd. gravityScale = 1.20.0000000e + 00f;
Bodies [1] = m_world-> CreateBody (& bd );
{
B2FixtureDef fd;
Fd. friction = 5.20.0000000e-01f;
Fd. restitution = 0.20.0000000e + 00f;
Fd. density = 5.20.0000000e + 00f;
Fd. isSensor = bool (0 );
Fd. filter. categoryBits = uint16 (1 );
Fd. filter. maskBits = uint16 (65535 );
Fd. filter. groupIndex = int16 (0 );
B2PolygonShape shape;
B2Vec2 vs [8];
Vs [0]. Set (3.473900079727173e + 00f,-2.009889930486679e-01f );
Vs [1]. Set (3.457079887390137e + 00f, 3.694039955735207e-02f );
Vs [2]. Set (-3.20.359949111938e + 00f, 2.348500074253440e-03f );
Vs [3]. Set (-3.109960079193115e + 00f,-3.581250011920929e-01f );
Vs [4]. Set (-2.590820074081421e + 00f,-5.472509860992432e-01f );
Vs [5]. Set (2.819370031356812e + 00f,-5.402340292930603e-01f );
Shape. Set (vs, 6 );
Fd. shape = & shape;
Bodies [1]-> CreateFixture (& fd );
}
}
{
B2BodyDef bd;
Bd. type = b2BodyType (2 );
Bd. position. Set (-7.438077926635742e-01f, 6.626811981201172e + 00f );
Bd. angle =-1.884713363647461e + 01f;
Bd. linearVelocity. Set (1.785794943571091e-01f, 3.799796104431152e-07f );
Bd. angularVelocity =-5.908820639888290e-06f;
Bd. linearDamping = 0.20.00000000000e + 00f;
Bd. angularDamping = 0.20.00000000000e + 00f;
Bd. allowSleep = bool (4 );
Bd. awake = bool (2 );
Bd. fixedRotation = bool (0 );
Bd. bullet = bool (0 );
Bd. active = bool (32 );
Bd. gravityScale = 1.20.0000000e + 00f;
Bodies [2] = m_world-> CreateBody (& bd );
{
B2FixtureDef fd;
Fd. friction = 9.499999880790710e-01f;
Fd. restitution = 0.20.0000000e + 00f;
Fd. density = 1.20.0000000e + 01f;
Fd. isSensor = bool (0 );
Fd. filter. categoryBits = uint16 (1 );
Fd. filter. maskBits = uint16 (65535 );
Fd. filter. groupIndex = int16 (-3 );
B2PolygonShape shape;
B2Vec2 vs [8];
Vs [0]. Set (1.639143696786366e-01f, 4.428444255173988e-02f );
Vs [1]. Set (-1.639146655797958e-01f, 4.428443685173988e-02f );
Vs [2]. Set (-1.639146655797958e-01f,-4.428443312644958e-02f );
Vs [3]. Set (1.639146357774734e-01f,-4.428444057703018e-02f );
Shape. Set (vs, 4 );
Fd. shape = & shape;
Bodies [2]-> CreateFixture (& fd );
}
}
GLESDebugDraw * _ debugDraw = newGLESDebugDraw (1.5); m_world-> SetDebugDraw (_ debugDraw );
Uint32 flags = 0;
Flags + = b2Draw: e_shapeBit;
_ DebugDraw-> SetFlags (flags );
M_world-> DrawDebugData ();
B2Free (bodies );
Bodies = NULL;
}
2. Modify the Step function as follows:
Void Step (Settings * settings)
{
Float32 timeStep = settings-> hz> 0.0f? 1.0f/settings-> hz: float32 (0.0f );
M_world-> Step (timeStep, settings-> velocityIterations, settings-> positionIterations );
M_world-> DrawDebugData ();
}
Compile and run the following code:
Code explanation
Here, I mainly explain two main functions, which are also different from Ray-Cast. One is RayCastNew ()
Constructor. One is a Step function. The code is not very complex in general, that is, three bodies are constructed in the RayCastNew () function, and the Step function is called every frame. Let's take a look at these two functions.
RayCastNew ()
{
B2Vec2 gravity; // defines a gravity Vector.
Gravity. Set (0.0f,-10.0f); // Set the gravity Vector to the negative direction of the Y axis.
M_world = new b2World (gravity); // creates a physical world through a gravity Vector
B2Body ** bodies = (b2Body **) b2Alloc (3 * sizeof (b2Body *); // defines the BODY array. The definition here is more skillful, you can simply copy and understand it.
{
B2BodyDef bd; // defines the definition variable of the first Body.
Bd. type = b2BodyType (0); // defines the Body type as a static Body.
Bd. position. Set (2.587699890136719e-02f, 5.515012264151709e + 00f); // define the position of the first Body
Bd. angle = 0.20.00000000000e + 00f; // you can specify the first Body angle.
Bd. linearVelocity. Set (0.20.0000000e + 00f, 0.20.0000000e + 00f); // you can specify the first line speed.
Bd. angularVelocity = 0.20.0000000e + 00f; // you can specify the angle of the first Body line.
Bd. linearDamping = 0.20.00000000000e + 00f; // you can specify the linear damping of the first Body.
Bd. angularDamping = 0.20.0000000e + 00f; // you can specify the first Body angular speed damping.
Bd. allowSleep = bool (4); // set the first Body to allow sleep
Bd. awake = bool (2); // set the first Body to activate the status.
Bd. fixedRotation = bool (0); // you can specify that the first Body cannot be rotated.
Bd. bullet = bool (0); // set the first Body not to move the Body quickly
Bd. active = bool (32); // set the first Body to start and activate
Bd. gravityScale = 1.20.0000000e + 00f; // set the gravity amplification factor of the first Body.
Bodies [0] = m_world-> CreateBody (& bd); // use this Body definition to create the first Body
{
B2FixtureDef fd; // defines the Fixture variable of the first Body.
Fd. friction = 2.000000029802322e-01f; // defines the friction of the first Body.
Fd. restitution = 0.20.0000000e + 00f; // defines the reply power of the first Body.
Fd. density = 1.20.0000000e + 00f; // defines the density of the first Body.
Fd. isSensor = bool (0); // you can specify that the first Body is not a sensor.
Fd. filter. categoryBits = uint16 (1); // collision directory bit, usually set one
Fd. filter. maskBits = uint16 (65535 );//
Fd. filter. groupIndex = int16 (0); // The three filters seem useless.
B2PolygonShape shape; // defines the variable shape.
B2Vec2 vs [8]; // defines the coordinate Array
Vs [0]. Set (7.733039855957031e-01f,-1.4900000034084320e-01f); // The first Vertex
Vs [1]. Set (-4.487270116806030e-01f, 1.138330027461052e-01f); // second Vertex
Vs [2]. Set (-1.880589962005615e + 00f,-1.365900039672852e-01f); // The third vertex
Vs [3]. Set (3.972740173339844e-01f,-3.897832870483398e + 00f); // The fourth point
Shape. Set (vs, 4); // you can Set the shape according to four points.
Fd. shape = & shape; // copy the shape to fixture.
Bodies [0]-> CreateFixture (& fd); // bind Fixture to Body [0].
}
}
{
B2BodyDef bd; // defines the definition variable of the second Body
Bd. type = b2BodyType (2); // defines the Body type as dynamic Body.
Bd. position. Set (-3.123168977050781e-02f, 7.535382270812988e + 00f); // Set the position of the second Body
Bd. angle =-1.313644275069237e-02f; // you can specify the second Body angle.
Bd. linearVelocity. Set (8.230687316837036e-01f, 7.775862514942587e-02f); // Set the second Body line angle
Bd. angularVelocity = 3.705333173274994e-02f; // set the second Body line angle
Bd. linearDamping = 0.20.00000000000e + 00f; // you can specify the linear damping of the second Body.
Bd. angularDamping = 0.20.0000000e + 00f; // you can specify the second Body angular speed damping.
Bd. allowSleep = bool (4); // sets the second Body to allow sleep
Bd. awake = bool (2); // set the second Body to activate the status.
Bd. fixedRotation = bool (0); // you can specify that the second Body cannot be rotated.
Bd. bullet = bool (0); // set the second Body not to move the Body quickly
Bd. active = bool (32); // sets the second Body to start activation.
Bd. gravityScale = 1.20.0000000e + 00f; // set the gravity amplification factor of the second Body.
Bodies [1] = m_world-> CreateBody (& bd); // create a second Body using this Body Definition
{
B2FixtureDef fd; // defines the Fixture variable of the second Body.
Fd. friction = 5.20.0000000e-01f; // defines the friction of the second Body.
Fd. restitution = 0.20.0000000e + 00f; // defines the responsiveness of the second Body.
Fd. density = 5.20.0000000e + 00f; // defines the density of the second Body.
Fd. isSensor = bool (0); // you can specify that the second Body is not a sensor.
Fd. filter. categoryBits = uint16 (1); // collision directory bit, usually set one
Fd. filter. maskBits = uint16 (65535 );//
Fd. filter. groupIndex = int16 (0 );//
B2PolygonShape shape; // defines the variable shape.
B2Vec2 vs [8]; // defines the coordinate Array
Vs [0]. Set (3.473900079727173e + 00f,-2.009889930486679e-01f );//
Vs [1]. Set (3.457079887390137e + 00f, 3.694039955735207e-02f );//
Vs [2]. Set (-3.20.359949111938e + 00f, 2.348500074253440e-03f );//
Vs [3]. Set (-3.109960079193115e + 00f,-3.581250011920929e-01f );//
Vs [4]. Set (-2.590820074081421e + 00f,-5.472509860992432e-01f );//
Vs [5]. Set (2.819370031356812e + 00f,-5.402340292930603e-01f );//
Shape. Set (vs, 6); // Set the shape based on 6 points.
Fd. shape = & shape; // copy the shape to fixture.
Bodies [1]-> CreateFixture (& fd); // bind Fixture to Body [1].
}
}
{
B2BodyDef bd; // defines the definition variable of the third Body.
Bd. type = b2BodyType (2); // defines the Body type as dynamic Body.
Bd. position. Set (-7.438077926635742e-01f, 6.626811981201172e + 00f); // Set the position of the third Body
Bd. angle =-1.884713363647461e + 01f; // you can specify the third Body angle.
Bd. linearVelocity. Set (1.785794943571091e-01f, 3.799796104431152e-07f); // Set the third Body line angle
Bd. angularVelocity =-5.908820639888290e-06f; // you can specify the third Body line angle.
Bd. linearDamping = 0.20.00000000000e + 00f; // sets the linear damping of the third Body.
Bd. angularDamping = 0.20.0000000e + 00f; // you can specify the third Body angular speed damping.
Bd. allowSleep = bool (4); // sets the third Body to allow sleep
Bd. awake = bool (2); // set the third Body to activate the status.
Bd. fixedRotation = bool (0); // you can specify that the third Body cannot be rotated.
Bd. bullet = bool (0); // set the third Body not to move the Body quickly.
Bd. active = bool (32); // set the third Body to start and activate
Bd. gravityScale = 1.20.0000000e + 00f; // set the gravity amplification factor of the third Body.
Bodies [2] = m_world-> CreateBody (& bd); // use this Body definition to create the third Body
{
B2FixtureDef fd; // defines the Fixture variable of the third Body.
Fd. friction = 9.499999880790710e-01f; // defines the friction of the third Body.
Fd. restitution = 0.20.0000000e + 00f; // defines the reply power of the third Body.
Fd. density = 1.20.0000000e + 01f; // defines the density of the third Body.
Fd. isSensor = bool (0); // you can specify that the third Body is not a sensor.
Fd. filter. categoryBits = uint16 (1); // collision directory bit, usually set one
Fd. filter. maskBits = uint16 (65535); // collision mask bit. The default value is 0 xFFFF (hexadecimal)
Fd. filter. groupIndex = int16 (-3); // set the collision group. If it is a negative number, the collision will not occur. If it is a positive number, the collision will occur. If the value is 0, it indicates that the Group will not be collided. The priority of a non-zero group is higher than that of the collision mask.
B2PolygonShape shape; // defines the variable shape.
B2Vec2 vs [8]; // defines the coordinate Array
Vs [0]. Set (1.639143696786366e-01f, 4.428444255173988e-02f );//
Vs [1]. Set (-1.639146655797958e-01f, 4.428443685173988e-02f );//
Vs [2]. Set (-1.639146655797958e-01f,-4.428443312644958e-02f );//
Vs [3]. Set (1.639146357774734e-01f,-4.428444057703018e-02f );//
Shape. Set (vs, 4); // you can Set the shape according to four points.
Fd. shape = & shape; // copy the shape to fixture.
Bodies [2]-> CreateFixture (& fd); // bind Fixture to Body [2].
}
}
GLESDebugDraw * _ debugDraw = newGLESDebugDraw (1.5); // defines a GLESDebugDraw
M_world-> SetDebugDraw (_ debugDraw); // register the GLESDebugDraw class for painting
Uint32 flags = 0; // defines the variable flags.
Flags + = b2Draw: e_shapeBit; // set the variable flasg and draw a shape.
_ DebugDraw-> SetFlags (flags); // set the flags to be drawn.
M_world-> DrawDebugData (); // draw the three bodies
B2Free (bodies); // release the variable
Bodies = NULL; // NULL
}
Void Step (Settings * settings)
{
Float32 timeStep = settings-> hz> 0.0f? 1.0f/settings-> hz: float32 (0.0f); // defines a frame frequency variable
M_world-> Step (timeStep, settings-> velocityIterations, settings-> positionIterations); // call the Step function in the Box2d source code.
M_world-> DrawDebugData (); // call the b2Draw class to draw the physical world
}