COCOS2D-X Physical Engine

When your needs are very simple, do not use the physics engine, for example, you only need to determine whether two objects collide, and use the update function of the node object and the containsPoint of the Rect object in combination (), the intersectRect() method may be sufficient.

void update(float dt)
{
auto p = touch->getLocation();
auto rect = this->getBoundingBox();

if(rect.containsPoint(p))
{
// do something, intersection
}
}
// create a static PhysicsBody
auto physicsBody = PhysicsBody::createBox(Size(65.0f, 81.0f ), PhysicsMaterial(0.1f, 1.0f, 0.0f));
physicsBody->setDynamic(false);

// create a sprite
auto sprite = Sprite::create("whiteSprite.png");
sprite->setPosition(Vec2(400, 400));

// sprite will use physicsBody
sprite->addComponent(physicsBody);

//add contact event listener
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

The above example code flow:

(1) PhysicsBody object creation

(2) Sprite object creation

(3) PhysicsBody object is added to the Sprite object in the form of a component

(4) Create a listener to respond to the onContactBegin() event

Bodies

Rigid bodies describe the physical properties of abstract objects, including: mass, Position, rotation angle, speed and damping. Cocos2d-x uses PhysicsBody objects to represent rigid bodies. When the rigid body is associated with the shape, the rigid body has a geometric shape at the diagonal corners, and the shape is not associated. The rigid body is just a set of physical properties of an abstract object.

Material

Material describes the material properties of abstract objects:

(1) Density: density, used to calculate the mass of the object

(2) friction: friction, used to simulate contact sliding between objects.

(3) Restitution: Restitution coefficient, a coefficient that simulates the rebound of an object. It is generally set between 0 and 1. 0 means no rebound, and 1 means complete rebound.

Shape

Shape describes the geometric properties of abstract objects. Only when the shape is associated with a rigid body, the rigid body has a geometric shape. If you need a rigid body to have a complex shape, you can associate multiple shapes for it. Each shape object is related to a PhysicsMaterial and has the following attributes.

(1) type: describes the type of shape, such as circle Shape, rectangle, etc.

(2) area: used to calculate the mass of rigid body, density and area determine the mass of rigid body.

(3) mass: The mass of a rigid body, which affects the acceleration of the body under a given force.

(4) moment: the torque required by the rigid body to obtain a specific angular acceleration

(5) offset: the offset relative to the rigid body’s center of gravity in the current coordinates of the rigid body< /p>

(6) tag: A tag of the shape object

Connection/joint

The Contacts and joint objects describe how rigid bodies are related to each other .

Rigid bodies are connected to different rigid bodies. Rigid bodies can be static. Each joint class is a subclass of PhysicsJoint. You can set joint->setCollisionEnable(false) to avoid collisions between related rigid bodies. .

World

The world is a game simulation of the real physical world. It contains all the abstract objects that are added. You can add rigid bodies, shapes, and constraints to the physics In the world, then the whole world is updated as a whole.

PhysicsWorld

The physics world PhysicsWorld is deeply integrated with the scene (Scene). You only need to call the initWithPhysics() method of the Scene object to create a scene containing the physical world. Note that the return value of the function must be judged during initialization. It returns true if the initialization is successful, and false if it fails.

(1) gravity: global gravity, the default value is Vec2 (0.0f, -98.0f)

(2) speed: the speed of the physical world, the speed here refers to A rate at which this simulated world runs. The default value is 1.0

(3) updateRate: the refresh rate of the physical world, where the refresh rate refers to the ratio of the refresh time of the game engine to the refresh time of the physical world.

(4) substeps: The number of substeps that are refreshed each time in the physical world.

PhysicsBody

With position and velocity, you can apply forces, movements, damping, impulses to physical rigid bodies Wait.

Collision

Collision filtering

Collision filtering allows you to enable or prevent collisions between shapes. The engine supports the use of types, Group bit masks for collision screening.

Cocos2d-x has 32 supported collision types. For each shape, you can specify the type it belongs to. You can also specify which types can collide with this shape, all of which are done by masking.

auto sprite1 = addSpriteAtPosition(Vec2(s_centre.x-150,s_centre.y));
sprite1->getPhysicsBody()->setCategoryBitmask(0x02); // 0010
sprite1->getPhysicsBody()->setCollisionBitmask(0x01); // 0001

sprite1 = addSpriteAtPosition(Vec2(s_centre.x-150,s_centre.y + 100));
sprite1- >getPhysicsBody()->setCategoryBitmask(0x02); // 0010
sprite1->getPhysicsBody()->setCollisionBitmask(0x01); // 0001

auto sprite2 = addSpriteAtPosition(Vec2(s_centre .x + 150,s_centre.y),1);
sprite2->getPhysicsBody()->setCategoryBitmask(0x01); // 0001
sprite2->getPhysicsBody()->setCollisionBitmask(0x02); // 0010

auto sprite3 = addSpriteAtPosition(Vec2(s_centre.x + 150,s_centre.y + 100),2);
sprite3->getPhysicsBody()->setCategoryBitmask(0x03) ; // 0011
sprite3->getPhysicsBody()->setCollisionBitmask(0x03); // 0011

}

When your needs are very simple, don’t use the physics engine. For example, you only need to determine whether two objects collide, and use the update function of the node object and the containsPoint of the Rect object in combination. (), the intersectRect() method may be sufficient.

void update(float dt)
{
auto p = touch->getLocation();
auto rect = this->getBoundingBox();

if(rect.containsPoint(p))
{
// do something, intersection
}
}
// create a static PhysicsBody
auto physicsBody = PhysicsBody::createBox(Size(65.0f, 81.0f ), PhysicsMaterial(0.1f, 1.0f, 0.0f));
physicsBody->setDynamic(false);

// create a sprite
auto sprite = Sprite::create("whiteSprite.png");
sprite->setPosition(Vec2(400, 400));

// sprite will use physicsBody
sprite->addComponent(physicsBody);

//add contact event listener
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

The above example code flow:

(1) PhysicsBody object creation

(2) Sprite object creation

(3) PhysicsBody object is added to the Sprite object in the form of a component

< p>(4) Create a listener to respond to the onContactBegin() event

Bodies

Rigid bodies describe the physical properties of abstract objects, including: mass, position, rotation angle, Speed ​​and damping. Cocos2d-x uses PhysicsBody objects to represent rigid bodies. When the rigid body is associated with the shape, the rigid body has a geometric shape at the diagonal corners, and the shape is not associated. The rigid body is just a set of physical properties of an abstract object.

Material

Material describes the material properties of abstract objects:

(1) Density: density, used to calculate the mass of the object

(2) friction: friction, used to simulate contact sliding between objects.

(3) Restitution: Restitution coefficient, a coefficient that simulates the rebound of an object. It is generally set between 0 and 1. 0 means no rebound, and 1 means complete rebound.

Shape

Shape describes the geometric properties of abstract objects. Only when the shape is associated with a rigid body, the rigid body has a geometric shape. If you need a rigid body to have a complex shape, you can associate multiple shapes for it. Each shape object is related to a PhysicsMaterial and has the following attributes.

(1) type: describes the type of shape, such as circle Shape, rectangle, etc.

(2) area: used to calculate the mass of rigid body, density and area determine the mass of rigid body.

(3) mass: The mass of a rigid body, which affects the acceleration of the body under a given force.

(4) moment: the torque required by the rigid body to obtain a specific angular acceleration

(5) offset: the offset relative to the rigid body’s center of gravity in the current coordinates of the rigid body< /p>

(6) tag: A tag of the shape object

Connection/joint

The Contacts and joint objects describe how rigid bodies are related to each other .

Rigid bodies are connected to different rigid bodies. Rigid bodies can be static. Each joint class is a subclass of PhysicsJoint. You can set joint->setCollisionEnable(false) to avoid collisions between related rigid bodies. .

World

The world is a game simulation of the real physical world. It contains all the abstract objects that are added. You can add rigid bodies, shapes, and constraints to the physics In the world, then the whole world is updated as a whole.

PhysicsWorld

The physics world PhysicsWorld is deeply integrated with the scene (Scene). You only need to call the initWithPhysics() method of the Scene object to create a scene containing the physical world. Note that the return value of the function must be judged during initialization. It returns true if the initialization is successful, and false if it fails.

(1) gravity: global gravity, the default value is Vec2 (0.0f, -98.0f)

(2) speed: the speed of the physical world, the speed here refers to A rate at which the simulated world runs. The default value is 1.0

(3) updateRate: the refresh rate of the physical world, where the refresh rate refers to the ratio of the refresh time of the game engine to the refresh time of the physical world.

(4) substeps: The number of substeps that are refreshed each time in the physical world.

PhysicsBody

With position and speed, you can apply forces, movements, damping, impulses to physical rigid bodies Wait.

Collision

Collision filtering

Collision filtering allows you to enable or prevent collisions between shapes. The engine supports the use type, Group bit masks for collision screening.

Cocos2d-x has 32 supported collision types. For each shape, you can specify the type it belongs to. You can also specify which types can collide with this shape, all of which are done by masking.

auto sprite1 = addSpriteAtPosition(Vec2(s_centre.x-150,s_centre.y));
sprite1->getPhysicsBody()->setCategoryBitmask(0x02); // 0010
sprite1->getPhysicsBody()->setCollisionBitmask(0x01); // 0001

sprite1 = addSpriteAtPosition(Vec2(s_centre.x-150,s_centre.y + 100));
sprite1- >getPhysicsBody()->setCategoryBitmask(0x02); // 0010
sprite1->getPhysicsBody()->setCollisionBitmask(0x01); // 0001

auto sprite2 = addSpriteAtPosition(Vec2(s_centre .x + 150,s_centre.y),1);
sprite2->getPhysicsBody()->setCategoryBitmask(0x01); // 0001
sprite2->getPhysicsBody()->setCollisionBitmask(0x02); // 0010

auto sprite3 = addSpriteAtPosition(Vec2(s_centre.x + 150,s_centre.y + 100),2);
sprite3->getPhysicsBody()->setCategoryBitmask(0x03) ; // 0011
sprite3->getPhysicsBody()->setCollisionBitmask(0x03); // 0011

}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3665 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.