Here is the system I am using to generate trimeshes:
Code: Select all
void TrimeshAddTriangles(btTriangleMesh *& trimesh, StaticMesh * source)
{//recursively copy all triangles from source into trimesh
int nk = source->num_kids;
int ns = source->num_surfaces;
static g_vector globalPos(0,0,0);
g_vector localPos(source->position.x,source->position.y,source->position.z);
globalPos += localPos;
//we only currently deal with translation, not rotation, so adding and subtracting
//can be used in place of push/popping the matrix.
for(int s = 0; s < ns; s++)
{
btVector3 V0,V1,V2;
float gx,gy,gz;
globalPos.get_vector(gx,gy,gz);
V0.setValue(
source->vertices[source->surfaces[s].vertrefs[0]].x + gx,
source->vertices[source->surfaces[s].vertrefs[0]].y + gy,
source->vertices[source->surfaces[s].vertrefs[0]].z + gz
);
V1.setValue(
source->vertices[source->surfaces[s].vertrefs[1]].x + gx,
source->vertices[source->surfaces[s].vertrefs[1]].y + gy,
source->vertices[source->surfaces[s].vertrefs[1]].z + gz
);
V2.setValue(
source->vertices[source->surfaces[s].vertrefs[2]].x + gx,
source->vertices[source->surfaces[s].vertrefs[2]].y + gy,
source->vertices[source->surfaces[s].vertrefs[2]].z + gz
);
trimesh->addTriangle(V0,V1,V2);
}
for (int k=0; k < nk; k++)
{
TrimeshAddTriangles(trimesh,source->kids[k]);
}
globalPos = globalPos - localPos;
}
void TrimeshFromStaticMesh (btCollisionShape *& trimeshShape, StaticMesh * source)
{
btTriangleMesh* trimesh = new btTriangleMesh();
TrimeshAddTriangles(trimesh, source);
bool useQuantizedBvhTree = true;
trimeshShape = new btBvhTriangleMeshShape(trimesh,useQuantizedBvhTree);
}
Code: Select all
void ConvexHullAddPoints(btConvexHullShape * dest, StaticMesh * source)
{
int nk = source->num_kids;
int nv = source->num_vertices;
static g_vector globalPos(0,0,0);
g_vector localPos(source->position.x,source->position.y,source->position.z);
globalPos += localPos;
for(int v=0; v<nv; v++)
{
g_vector vec = globalPos + g_vector(source->vertices[v].x,source->vertices[v].y,source->vertices[v].z);
dest->addPoint(btPoint3(vec.x, vec.y, vec.z));
}
for(int k=0; k<nk; k++)
{
ConvexHullAddPoints(dest,source->kids[k]);
}
globalPos = globalPos - localPos;
}
void ConvexHullFromStaticMesh (btCollisionShape *& convexHullShape, StaticMesh * source)
{
btConvexHullShape * hull = new btConvexHullShape();
ConvexHullAddPoints(hull,source);
convexHullShape = hull;
}
I am initialising the physics engine like so:
Code: Select all
// collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
// use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
// the maximum size of the collision world. Make sure objects stay within these boundaries
// Don't make the world AABB size too large, it will harm simulation quality and performance
btVector3 worldAabbMin(-10000,-1000,-10000);
btVector3 worldAabbMax(10000,1000,10000);
int maxProxies = 1024;
btAxisSweep3* overlappingPairCache = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
// the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,-10,0));