Add COLLADA physics support for GImpact (allowing dynamic moving rigid bodies using concave collision shapes) in ColladaDemo, and ColladaConverter (import and export)
Note: ColladaConverter will export a btGImpactConvexDecompositionShape the same as a btCompoundShape, so importing an exported btGImpactConvexDecompositionShape (roundtrip) creates a btCompoundShape
This commit is contained in:
@@ -18,7 +18,7 @@ subject to the following restrictions:
|
|||||||
#include "LinearMath/btIDebugDraw.h"
|
#include "LinearMath/btIDebugDraw.h"
|
||||||
#include "GLDebugDrawer.h"
|
#include "GLDebugDrawer.h"
|
||||||
|
|
||||||
|
#include "BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h"
|
||||||
|
|
||||||
//COLLADA_DOM and LibXML source code are included in Extras/ folder.
|
//COLLADA_DOM and LibXML source code are included in Extras/ folder.
|
||||||
//COLLADA_DOM should compile under all platforms, and is enabled by default.
|
//COLLADA_DOM should compile under all platforms, and is enabled by default.
|
||||||
@@ -76,7 +76,7 @@ int main(int argc,char** argv)
|
|||||||
|
|
||||||
/// Import Collada 1.4 Physics objects
|
/// Import Collada 1.4 Physics objects
|
||||||
/// also can pass filename in as argument
|
/// also can pass filename in as argument
|
||||||
const char* filename = "jenga.dae";
|
const char* filename = "boxc4d.dae";
|
||||||
printf("argc=%i\n",argc);
|
printf("argc=%i\n",argc);
|
||||||
{
|
{
|
||||||
for (int i=0;i<argc;i++)
|
for (int i=0;i<argc;i++)
|
||||||
@@ -109,6 +109,8 @@ void ColladaDemo::initPhysics(const char* filename)
|
|||||||
|
|
||||||
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
|
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
|
||||||
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
|
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
|
||||||
|
btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher);
|
||||||
|
|
||||||
btVector3 worldMin(-1000,-1000,-1000);
|
btVector3 worldMin(-1000,-1000,-1000);
|
||||||
btVector3 worldMax(1000,1000,1000);
|
btVector3 worldMax(1000,1000,1000);
|
||||||
btBroadphaseInterface* pairCache = new btAxisSweep3(worldMin,worldMax);
|
btBroadphaseInterface* pairCache = new btAxisSweep3(worldMin,worldMax);
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ subject to the following restrictions:
|
|||||||
//#include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
|
//#include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
|
||||||
#include "BulletCollision/CollisionShapes/btCompoundShape.h"
|
#include "BulletCollision/CollisionShapes/btCompoundShape.h"
|
||||||
#include "LinearMath/btDefaultMotionState.h"
|
#include "LinearMath/btDefaultMotionState.h"
|
||||||
|
#include "BulletCollision/Gimpact/btGImpactShape.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#define snprintf _snprintf
|
#define snprintf _snprintf
|
||||||
@@ -358,13 +360,14 @@ bool ColladaConverter::convert()
|
|||||||
// Find out how many groups we need to allocate space for
|
// Find out how many groups we need to allocate space for
|
||||||
int numTriangleGroups = (int)meshElement->getTriangles_array().getCount();
|
int numTriangleGroups = (int)meshElement->getTriangles_array().getCount();
|
||||||
int numPolygonGroups = (int)meshElement->getPolygons_array().getCount();
|
int numPolygonGroups = (int)meshElement->getPolygons_array().getCount();
|
||||||
int totalGroups = numTriangleGroups + numPolygonGroups;
|
int numPolyList = (int)meshElement->getPolylist_array().getCount();
|
||||||
|
int totalGroups = numTriangleGroups + numPolygonGroups + numPolyList;
|
||||||
if (totalGroups == 0)
|
if (totalGroups == 0)
|
||||||
{
|
{
|
||||||
printf("No Triangles or Polygons found int Geometry %s \n", lib->getId() );
|
printf("No Triangles or Polygons found int Geometry %s \n", lib->getId() );
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
printf("Found mesh geometry (%s): numTriangleGroups:%i numPolygonGroups:%i\n",lib->getId(),numTriangleGroups,numPolygonGroups);
|
printf("Found mesh geometry (%s): numTriangleGroups:%i numPolygonGroups :%i numPolyList=%i\n",lib->getId(),numTriangleGroups,numPolygonGroups,numPolyList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1506,18 +1509,40 @@ void ColladaConverter::addConvexHull (btCollisionShape* shape, const char* conve
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void ColladaConverter::addConvexMesh (btCollisionShape* shape, const char* nodeName)
|
||||||
ColladaConverter::addConvexMesh (btCollisionShape* shape, const char* nodeName)
|
|
||||||
{
|
{
|
||||||
printf("convex Triangle Mesh Shape\n");
|
printf("convex Triangle Mesh Shape\n");
|
||||||
printf("ERROR: Unsupported.\n");
|
printf("ERROR: Unsupported.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void ColladaConverter::addConcaveMesh(btCollisionShape* shape, const char* nodeName)
|
||||||
ColladaConverter::addConcaveMesh(btCollisionShape* shape, const char* nodeName)
|
|
||||||
{
|
{
|
||||||
btTriangleMeshShape* meshShape = (btTriangleMeshShape*)shape;
|
btTriangleMeshShape* meshShape = (btTriangleMeshShape*)shape;
|
||||||
btStridingMeshInterface* meshInterface = meshShape->getMeshInterface ();
|
btStridingMeshInterface* meshInterface = meshShape->getMeshInterface ();
|
||||||
|
addConcaveMeshInternal(meshInterface,nodeName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColladaConverter::addGimpactMesh(btCollisionShape* shape, const char* shapeName)
|
||||||
|
{
|
||||||
|
btGImpactShapeInterface* meshShapeInterface = (btGImpactShapeInterface*) shape;
|
||||||
|
switch (meshShapeInterface->getGImpactShapeType())
|
||||||
|
{
|
||||||
|
case CONST_GIMPACT_TRIMESH_SHAPE:
|
||||||
|
{
|
||||||
|
btGImpactMeshShape* meshShape = (btGImpactMeshShape*) shape;
|
||||||
|
btStridingMeshInterface* meshInterface = meshShape->getMeshInterface ();
|
||||||
|
addConcaveMeshInternal(meshInterface,shapeName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColladaConverter::addConcaveMeshInternal(btStridingMeshInterface* meshInterface , const char* nodeName)
|
||||||
|
{
|
||||||
|
|
||||||
domLibrary_geometries* geomLib = getDefaultGeomLib ();
|
domLibrary_geometries* geomLib = getDefaultGeomLib ();
|
||||||
domGeometry* geo = findGeometry (nodeName);
|
domGeometry* geo = findGeometry (nodeName);
|
||||||
@@ -1758,6 +1783,43 @@ void ColladaConverter::buildShapeNew (btCollisionShape* shape, void* domTechniqu
|
|||||||
gi->setUrl (shapeURL);
|
gi->setUrl (shapeURL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case GIMPACT_SHAPE_PROXYTYPE:
|
||||||
|
{
|
||||||
|
btGImpactShapeInterface* meshShapeInterface = (btGImpactShapeInterface*) shape;
|
||||||
|
switch (meshShapeInterface->getGImpactShapeType())
|
||||||
|
{
|
||||||
|
case CONST_GIMPACT_TRIMESH_SHAPE:
|
||||||
|
{
|
||||||
|
addGimpactMesh (shape, shapeName);
|
||||||
|
char shapeURL[512];
|
||||||
|
snprintf(&shapeURL[0], 512, "#%s", shapeName);
|
||||||
|
domInstance_geometry* gi = (domInstance_geometry*)colladaShape->createAndPlace (COLLADA_ELEMENT_INSTANCE_GEOMETRY);
|
||||||
|
gi->setUrl (shapeURL);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case CONST_GIMPACT_COMPOUND_SHAPE:
|
||||||
|
{
|
||||||
|
btGImpactCompoundShape* gimpactCompoundShape = (btGImpactCompoundShape*)shape;
|
||||||
|
|
||||||
|
for (int i = 0; i < gimpactCompoundShape->getNumChildShapes (); i++)
|
||||||
|
{
|
||||||
|
btCollisionShape* child_shape = gimpactCompoundShape->getChildShape(i);
|
||||||
|
char childShapeName[512];
|
||||||
|
sprintf(childShapeName,"%s-Child%d",shapeName,i);
|
||||||
|
buildShapeNew (child_shape, domTechniqueCommon, childShapeName,true, gimpactCompoundShape->getChildTransform(i));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
|
case TRIANGLE_MESH_SHAPE_PROXYTYPE:
|
||||||
{
|
{
|
||||||
addConcaveMesh (shape, shapeName);
|
addConcaveMesh (shape, shapeName);
|
||||||
@@ -2086,8 +2148,7 @@ void ColladaConverter::addMaterial (btRigidBody* rb, const char* nodeName)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void ColladaConverter::updateRigidBodyPosition (btRigidBody* body, domNode* node)
|
||||||
ColladaConverter::updateRigidBodyPosition (btRigidBody* body, domNode* node)
|
|
||||||
{
|
{
|
||||||
// remove all translations
|
// remove all translations
|
||||||
while (node->getTranslate_array().getCount())
|
while (node->getTranslate_array().getCount())
|
||||||
@@ -2137,8 +2198,7 @@ ColladaConverter::updateRigidBodyPosition (btRigidBody* body, domNode* node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void ColladaConverter::updateRigidBodyVelocity (btRigidBody* body)
|
||||||
ColladaConverter::updateRigidBodyVelocity (btRigidBody* body)
|
|
||||||
{
|
{
|
||||||
domInstance_rigid_bodyRef rigidBodyInstance = findRigid_body_instance (body);
|
domInstance_rigid_bodyRef rigidBodyInstance = findRigid_body_instance (body);
|
||||||
if (!rigidBodyInstance)
|
if (!rigidBodyInstance)
|
||||||
@@ -2177,8 +2237,7 @@ ColladaConverter::updateRigidBodyVelocity (btRigidBody* body)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void ColladaConverter::updateConstraint (btTypedConstraint* constraint, domRigid_constraint* rigidConstraint)
|
||||||
ColladaConverter::updateConstraint (btTypedConstraint* constraint, domRigid_constraint* rigidConstraint)
|
|
||||||
{
|
{
|
||||||
if (constraint->getConstraintType() != D6_CONSTRAINT_TYPE)
|
if (constraint->getConstraintType() != D6_CONSTRAINT_TYPE)
|
||||||
return;
|
return;
|
||||||
@@ -2365,6 +2424,9 @@ void ColladaConverter::syncOrAddRigidBody (btRigidBody* body)
|
|||||||
|
|
||||||
if (shape->getShapeType () == TRIANGLE_MESH_SHAPE_PROXYTYPE) {
|
if (shape->getShapeType () == TRIANGLE_MESH_SHAPE_PROXYTYPE) {
|
||||||
addConcaveMesh (shape, shapeName);
|
addConcaveMesh (shape, shapeName);
|
||||||
|
} else if (shape->getShapeType () == GIMPACT_SHAPE_PROXYTYPE)
|
||||||
|
{
|
||||||
|
addGimpactMesh (shape, shapeName);
|
||||||
} else if (!shape->isConvex () && !shape->isCompound() && (shape->getShapeType()!=STATIC_PLANE_PROXYTYPE)) {
|
} else if (!shape->isConvex () && !shape->isCompound() && (shape->getShapeType()!=STATIC_PLANE_PROXYTYPE)) {
|
||||||
printf("Unknown shape type. %d Skipping rigidbody.\n", shape->getShapeType());
|
printf("Unknown shape type. %d Skipping rigidbody.\n", shape->getShapeType());
|
||||||
return;
|
return;
|
||||||
@@ -2699,6 +2761,15 @@ btCollisionShape* ColladaConverter::createBvhTriangleMeshShape(btTriangleMesh* t
|
|||||||
return shape;
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btCollisionShape* ColladaConverter::createGimpactShape(btTriangleMesh* trimesh)
|
||||||
|
{
|
||||||
|
btGImpactMeshShape* shape = new btGImpactMeshShape(trimesh);
|
||||||
|
shape->setMargin(0.f);
|
||||||
|
shape->updateBound();
|
||||||
|
m_allocatedCollisionShapes.push_back(shape);
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
|
||||||
btCollisionShape* ColladaConverter::createConvexTriangleMeshShape(btTriangleMesh* trimesh)
|
btCollisionShape* ColladaConverter::createConvexTriangleMeshShape(btTriangleMesh* trimesh)
|
||||||
{
|
{
|
||||||
btCollisionShape* shape = new btConvexTriangleMeshShape(trimesh);
|
btCollisionShape* shape = new btConvexTriangleMeshShape(trimesh);
|
||||||
@@ -2970,8 +3041,9 @@ void ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBod
|
|||||||
|
|
||||||
if (rbOutput.m_isDynamics)
|
if (rbOutput.m_isDynamics)
|
||||||
{
|
{
|
||||||
printf("moving concave <mesh> not supported, transformed into convex\n");
|
printf("moving concave <mesh> added\n");
|
||||||
rbOutput.m_colShape = createConvexTriangleMeshShape(trimesh);
|
//rbOutput.m_colShape = createConvexTriangleMeshShape(trimesh);
|
||||||
|
rbOutput.m_colShape = createGimpactShape(trimesh);
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
printf("static concave triangle <mesh> added\n");
|
printf("static concave triangle <mesh> added\n");
|
||||||
|
|||||||
@@ -141,6 +141,10 @@ protected:
|
|||||||
void addConvexHull (btCollisionShape* shape, const char* nodeName);
|
void addConvexHull (btCollisionShape* shape, const char* nodeName);
|
||||||
void addConvexMesh (btCollisionShape* shape, const char* nodeName);
|
void addConvexMesh (btCollisionShape* shape, const char* nodeName);
|
||||||
void addConcaveMesh(btCollisionShape* shape, const char* nodeName);
|
void addConcaveMesh(btCollisionShape* shape, const char* nodeName);
|
||||||
|
void addGimpactMesh(btCollisionShape* shape, const char* nodeName);
|
||||||
|
|
||||||
|
void addConcaveMeshInternal(class btStridingMeshInterface* meshInterface , const char* nodeName);
|
||||||
|
|
||||||
class domNode* addNode (btRigidBody* body, const char* nodeName, const char* shapeName);
|
class domNode* addNode (btRigidBody* body, const char* nodeName, const char* shapeName);
|
||||||
class domRigid_constraint* addConstraint (btTypedConstraint* constraint, const char* constraintName);
|
class domRigid_constraint* addConstraint (btTypedConstraint* constraint, const char* constraintName);
|
||||||
class domInstance_rigid_constraint* addConstraintInstance (btTypedConstraint* constraint, const char* constraintName);
|
class domInstance_rigid_constraint* addConstraintInstance (btTypedConstraint* constraint, const char* constraintName);
|
||||||
@@ -227,6 +231,7 @@ public:
|
|||||||
virtual class btTriangleMesh* createTriangleMeshContainer();
|
virtual class btTriangleMesh* createTriangleMeshContainer();
|
||||||
virtual btCollisionShape* createBvhTriangleMeshShape(btTriangleMesh* trimesh);
|
virtual btCollisionShape* createBvhTriangleMeshShape(btTriangleMesh* trimesh);
|
||||||
virtual btCollisionShape* createConvexTriangleMeshShape(btTriangleMesh* trimesh);
|
virtual btCollisionShape* createConvexTriangleMeshShape(btTriangleMesh* trimesh);
|
||||||
|
virtual btCollisionShape* createGimpactShape(btTriangleMesh* trimesh);
|
||||||
virtual class btConvexHullShape* createConvexHullShape();
|
virtual class btConvexHullShape* createConvexHullShape();
|
||||||
virtual class btCompoundShape* createCompoundShape();
|
virtual class btCompoundShape* createCompoundShape();
|
||||||
|
|
||||||
|
|||||||
@@ -881,6 +881,8 @@ Set of btGImpactMeshShapePart parts
|
|||||||
*/
|
*/
|
||||||
class btGImpactMeshShape : public btGImpactShapeInterface
|
class btGImpactMeshShape : public btGImpactShapeInterface
|
||||||
{
|
{
|
||||||
|
btStridingMeshInterface* m_meshInterface;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
btAlignedObjectArray<btGImpactMeshShapePart*> m_mesh_parts;
|
btAlignedObjectArray<btGImpactMeshShapePart*> m_mesh_parts;
|
||||||
void buildMeshParts(btStridingMeshInterface * meshInterface)
|
void buildMeshParts(btStridingMeshInterface * meshInterface)
|
||||||
@@ -907,6 +909,7 @@ protected:
|
|||||||
public:
|
public:
|
||||||
btGImpactMeshShape(btStridingMeshInterface * meshInterface)
|
btGImpactMeshShape(btStridingMeshInterface * meshInterface)
|
||||||
{
|
{
|
||||||
|
m_meshInterface = meshInterface;
|
||||||
buildMeshParts(meshInterface);
|
buildMeshParts(meshInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,6 +925,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
btStridingMeshInterface* getMeshInterface()
|
||||||
|
{
|
||||||
|
return m_meshInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
const btStridingMeshInterface* getMeshInterface() const
|
||||||
|
{
|
||||||
|
return m_meshInterface;
|
||||||
|
}
|
||||||
|
|
||||||
int getMeshPartCount() const
|
int getMeshPartCount() const
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user