Cocos2d-x box2d simple object (1)

Source: Internet
Author: User

 

The physical world of box2d has a world to maintain. You can create simple objects, circles, rectangles, triangles, and polygon. Polygon supports only Convex Polygon and a maximum of 8 vertices.

 

There are three types of box2d objects: dynamic, static, and kinematic ).

Dynamic objects can be forced, collided, and moved.

Static objects cannot be moved, they cannot be moved, and they can be involved in a collision.

A kinematics object is similar to a static object, but it can be moved.

 

There are three steps to create an object,

1. The body of an object defines b2BodyDef. This struct describes the object type, position coordinate, angle, and speed.

2. Use b2BodyDef to create a body.

3. With the body, you still cannot see this object, and you also need to define the shape. This shape is added to an object through a fixtures.

 

    // body def    b2BodyDef bDef;    bDef.type = b2_dynamicBody;    bDef.position = b2Vec2(visibleSize.width / 2 / RATIO + 5, visibleSize.height / 2 / RATIO);    // body    b2Body * birdBody = world->CreateBody(&bDef);        // shape    b2PolygonShape birdShap;    CCSize size = bird->getContentSize();    birdShap.SetAsBox((65) / 2 / RATIO, (65) / 2 / RATIO, b2Vec2(45 / 2 / RATIO, 27 / 2 / RATIO), 0);    // fixtures    b2FixtureDef birdFixtureDef;    birdFixtureDef.shape = &birdShap;    // fix to body    birdBody->CreateFixture(&birdFixtureDef);

Bind the genie to an object:

 

An genie is bound to an object and is set in the Object body definition b2BodyDef,

 

        role = CCSprite::create(poop_1.png);        bodyDef.userData = role;

However, this is not enough. You will find that the genie and the object will be separated and will not move together. This also requires changing the coordinates of the genie Based on the object location. For details, refer to the update function of the previous code.

 

 

The official company has a B2Sprite class that binds the genie and the body together. I will add this class later to make it easier to use and don't need to define a lot of things.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.