Bullet (COCOS2DX) Convex multipatch shape and combined shape

Source: Internet
Author: User

High-level topographic and triangular meshes are also created for complex shapes.

Let's take a look at the effect


The left side creates a multipatch from the provided vertex, and a combined shape of the sphere and the box on the right

First to understand convex polyhedron

Creates a convex multipatch shape from the provided vertex , regardless of the given vertex order, creating a convex multipatch composed of these vertices.

Let's take a look at the explanation of Btconvexhullshape and the constructor function

This constructor optionally takes in a pointer to points. Each point was assumed to be 3 consecutive btscalar (x, Y, z), the striding defines the number of bytes between each point, I N memory.///it is easier to not pass any points in the constructor, and just add one point at a time, using ADDPOINT.///BT Convexhullshape make an internal copy of the Points.btconvexhullshape (const btscalar* points=0,int numpoints=0, int stride =sizeof (BtVector3));

The first parameter is the vertex data, the second one is the number of vertices, and the third is the byte per vertex.

At the same time, it is not necessary to give all the vertices at once, can be added later:

void Addpoint (const btvector3& point, bool Recalculatelocalaabb = TRUE);

Added vertex information, whether to recalculate aabb bounding box

for the provision of vertex data, users do not need to always save, see the source Btconvexhullshape will use btalignedobjectarray<btvector3> m_unscaledpoints;

To save the vertex data.

Here's an example.

See, why does one appear to be a triangular pyramid, and one is not? , in fact, two shapes are the same, please see me slowly.

The previous explanation Btconvexhullshape will build a multipatch based on the data provided, and we have to keep this in mind.

By looking at the inheritance graph, we know that Btconvexhullshape inherits from Btpolyhedralconvexshape,

and Btpolyhedralconvexshape, offers a

Optional method mainly used to generate multiple contact points by clipping polyhedral features (faces/edges)///experim Ental/work-in-progressvirtual boolinitializepolyhedralfeatures (int shiftverticesbymargin=0);

It can be understood that the provided point is formatted as a multipatch, meaning that a vertex can be called initializepolyhedralfeatures

Formatted as a multipatch.

Below is the result generated in the order of the vertices we provide, above the formatted result

below to see how the author joins the PHYSICSWORLD3D

btrigidbody* physicsworld3d::addconvexhull (const float* floatdata, int numpoints, const btvector3& position, BOOL Bpoly, const physicsmaterial3d& material) {btconvexhullshape* Colshape = new Btconvexhullshape (Floatdata, numPoints , sizeof (BTVECTOR3)), if (Bpoly) {colshape->initializepolyhedralfeatures ();} Auto BODY = GetBody (colshape, position, material); _world->addrigidbody (body); return body;} btrigidbody* Physicsworld3d::addconvexhull (std::vector<btvector3>& points, const btvector3& position, BOOL Bpoly, const physicsmaterial3d& material) {Auto BODY = Addconvexhull (Points[0].m_floats, Points.size (), Position, bpoly, material); return body;}

Provides two overloaded functions, one that directly takes a floating-point data pointer containing vertex data as an argument, and the other as an array of vertices.

The second overload is provided to generate a multipatch from directly passing in the raw file's data, remember the raw file? Bullet (COCOS2DX) Creation of terrain

Look at an example.

Std::vector<float> points; Physicshelper3d::loadraw ("Monkey.raw", points);//blender Monkey _convexbody = _world->addconvexhull (&points[0], Points.size ()/3, BtVector3 ( -2, 0, 5), False, Physicsmaterial3d (5.F, 0.5f, 0.5f, 0.f)); _convexbody = _world->addconvex Hull (&points[0], points.size ()/3, BtVector3 (2, 0, 5), True, Physicsmaterial3d (5.F, 0.5f, 0.5f, 0.f));

Right not formatted, left for formatting

Let's look at the combo shape.

In short, combine multiple shapes into one shape, and the top image to the right is a combination of shapes

Can be made up of several different primitive Shape

The constructor is simple

Btcompoundshape (bool Enabledynamicaabbtree = TRUE);

The dynamic tree is turned on by default, optimizing

The key is to increase the collision shape

Voidaddchildshape (const bttransform& localtransform,btcollisionshape* shape);

Localtransform shape relative Offset, shape collision shapes.

relative offset: Join the final Btcompoundshape in the (0,0,0), relative offset is relative to (0,0,0)

That you want to get the combo shape and create it yourself . Btcollisionshape , and then add to Btcompoundshape

The author made a simple package

struct physicsshapeinfo3d{btcollisionshape* colshape;bttransform transform; Physicsshapeinfo3d () {}; Physicsshapeinfo3d (btcollisionshape* shape, bttransform& trans): Colshape (Shape), transform (trans) {}};

Provides a simple structure to pass the collision Shapes property

btrigidbody* Physicsworld3d::addcompound (std::vector<physicsshapeinfo3d>& shapeList, const BTVECTOR3 & Position, const physicsmaterial3d& material) {btcompoundshape* shape = new Btcompoundshape;for (auto& Shapeinfo:shapelist) {Shape->addchildshape (shapeinfo.transform, shapeinfo.colshape);} Auto BODY = getbody (shape, position, material); _world->addrigidbody (body); return body;}

std:: Vector < physicsshapeinfo3d >& shapelist Shape Properties list to get the rigid bodies of the grouped shapes

Here's an example.

1

shape = New btboxshape(btVector3(1.f, 1.F, 1.f));

Shapeinfo.colshape = shape;

Trans.setorigin (btVector3(0, 1, 0));

Shapeinfo.transform = trans;

_shapes.push_back (Shapeinfo);

2

shape = New btsphereshape(1.f);

Shapeinfo.colshape = shape;

Trans.setorigin (btVector3(0, 3, 0));

Shapeinfo.transform = trans;

_shapes.push_back (Shapeinfo);

_world->addcompound (_shapes, btVector3(0, 0, 5.f));

The previous example creates a composite shape on a sphere, down as a box

Of course, you can create a bowling ball through a sphere and a cylinder, and you can write a bowling game.

Source Code


Bullet (COCOS2DX) Convex multipatch shape and combined shape

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.