Bullet (COCOS2DX) analysis of rigid body creation and destruction (primitives)

Source: Internet
Author: User

RELATED links:http://bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Shapes

BulletIntroduction to Basic graphics

basic graphics provided by Bullet include spheres, rectangles, cylinders, capsules, cones, multi-spheres

And of course there's a plane, an infinite plane

1. the sphere is a very simple shape :

Btsphereshape (btscalar radius) provides the radius of a sphere

2. cuboid (Box)

Btboxshape (const BTVECTOR3 &boxhalfextents) provides half of the box size (half of the length and width of the height)

3. Cylinder (Similar to box shape)

Btcylindershape (const BTVECTOR3 &halfextents) offers half size (radius , height , unknown)

The default axis is y - axis,Btcylindershapex (x - axis ), Btcylindershapez (z - axis )

4. Capsule body

Btcapsuleshape (btscalar radius, btscalar height) provides a hemispherical radius , a cylinder height , and an actual height of 2*radius + Height

The default axis is y - axis,Btcapsuleshapex (x - axis ), Btcapsuleshapez (z -axis )


5. Cone

Btconeshape (btscalar radius, btscalar height) provides a base radius , height

The default axis is y- axis,Btconeshapex (x - axis ), Btconeshapez (z -axis )

6. Multi-sphere (multiple sphere combinations)

Btmultisphereshape (const BTVECTOR3 *positions, const btscalar *radi, int numspheres)

Positions the position of each sphere , Radi the radius of each sphere, the number of spheres


Create a rigid body

1. first get the rigid body shape you want to create, take box as an example

btcollisionshape* colshape = new btboxshape(size * 0.5f); //Halfsize

Because Btboxshape inherits from Btcollisionshape.

2. Set other basic information for a rigid body

(1). Transformation matrices for rigid bodies

(2). Inertia of rigid bodies (dynamic objects only)

(3). motionstate provides interpolation, synchronization of active objects, etc.

(4). Material information for rigid bodies

(5). Creating a rigid body

Bttransform Starttransform;    1starttransform.setidentity (); Starttransform.setorigin (position); Rigidbody is dynamic if and only if mass is non zero, otherwise staticbool isdynamic = (material.mass! = 0.F); BtVector3 Localinertia (0,0,0); if (isdynamic) Colshape->calculatelocalinertia (Material.mass, Localinertia); 2//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes ' active ' OBJECTSBTD efaultmotionstate* mymotionstate = new Btdefaultmotionstate (starttransform); 3btrigidbody::btrigidbodyconstructioninfo Rbinfo (Material.mass, Mymotionstate,colshape,localinertia); 4rbinfo.m_restitution = Material.restitution;rbinfo.m_friction = Material.friction;rbinfo.m_rollingfriction = material.rollingfriction;btrigidbody* BODY = new Btrigidbody (rbinfo); 5

3. Add a rigid body to the world

_world->addrigidbody (body);

Destroying rigid bodies

1. get _world->getcollisionobjectarray () from World because the rigid body inherits from Btcollisionobject

2. If obj is a rigid body, convert to rigid body btrigidbody* BODY = btrigidbody:: upcast (obj);

Because a rigid body may contain moitonstate,

3. Delete the motionstate of the rigid body, and collisionshape, (Collisionshape is not obj?) View the values of the two are different)

4. Removing obj from the world should not remove the rigid body? Look at the source notes

///removecollisionobject would first check if it is a rigid body, if so call removerigidbody otherwise call btcollision World::removecollisionobject

Virtual void removecollisionobject (btcollisionobject* collisionobject);

5. Release obj, because it is generated with new, new is actually the memory allocation method of the bullet, overloading the new

6. Release complete

Remove the bodies from the dynamics World and delete themfor (i = _world->getnumcollisionobjects ()-1; I >= 0; I- -) {btcollisionobject* obj = _world->getcollisionobjectarray () [i];btrigidbody* BODY = btrigidbody::upcast (obj); if ( Body && body->getmotionstate ()) {delete body->getmotionstate ();d elete Body->getcollisionshape ();} _world->removecollisionobject (obj);  Delete obj;

Bullet (COCOS2DX) analysis of rigid body creation and destruction (primitives)

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.