demo cleanup part 4 (fixed leaks in vehicle demo)

This commit is contained in:
ejcoumans
2007-12-07 02:12:00 +00:00
parent 8a28c7940a
commit c1a2debd3b
3 changed files with 87 additions and 21 deletions

View File

@@ -39,10 +39,6 @@ class BasicDemo : public DemoApplication
btConstraintSolver* m_solver; btConstraintSolver* m_solver;
btCollisionAlgorithmCreateFunc* m_sphereSphereCF;
btCollisionAlgorithmCreateFunc* m_sphereBoxCF;
btCollisionAlgorithmCreateFunc* m_boxSphereCF;
btDefaultCollisionConfiguration* m_collisionConfiguration; btDefaultCollisionConfiguration* m_collisionConfiguration;
public: public:

View File

@@ -89,12 +89,62 @@ VehicleDemo::VehicleDemo()
m_carChassis(0), m_carChassis(0),
m_cameraHeight(4.f), m_cameraHeight(4.f),
m_minCameraDistance(3.f), m_minCameraDistance(3.f),
m_maxCameraDistance(10.f) m_maxCameraDistance(10.f),
m_indexVertexArrays(0),
m_vertices(0)
{ {
m_vehicle = 0; m_vehicle = 0;
m_cameraPosition = btVector3(30,30,30); m_cameraPosition = btVector3(30,30,30);
} }
VehicleDemo::~VehicleDemo()
{
//cleanup in the reverse order of creation/initialization
//remove the rigidbodies from the dynamics world and delete them
int i;
for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
delete body->getMotionState();
}
m_dynamicsWorld->removeCollisionObject( obj );
delete obj;
}
//delete collision shapes
for (int j=0;j<m_collisionShapes.size();j++)
{
btCollisionShape* shape = m_collisionShapes[j];
delete shape;
}
delete m_indexVertexArrays;
delete m_vertices;
//delete dynamics world
delete m_dynamicsWorld;
delete m_vehicleRayCaster;
delete m_vehicle;
//delete solver
delete m_constraintSolver;
//delete broadphase
delete m_overlappingPairCache;
//delete dispatcher
delete m_dispatcher;
delete m_collisionConfiguration;
}
void VehicleDemo::initPhysics() void VehicleDemo::initPhysics()
{ {
@@ -107,13 +157,14 @@ void VehicleDemo::initPhysics()
#endif #endif
btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50)); btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); m_collisionShapes.push_back(groundShape);
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); m_collisionConfiguration = new btDefaultCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
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); m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);
btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver(); m_constraintSolver = new btSequentialImpulseConstraintSolver();
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration); m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
#ifdef FORCE_ZAXIS_UP #ifdef FORCE_ZAXIS_UP
m_dynamicsWorld->setGravity(btVector3(0,0,-10)); m_dynamicsWorld->setGravity(btVector3(0,0,-10));
#endif #endif
@@ -139,7 +190,7 @@ const float TRIANGLE_SIZE=20.f;
const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1); const int totalTriangles = 2*(NUM_VERTS_X-1)*(NUM_VERTS_Y-1);
btVector3* gVertices = new btVector3[totalVerts]; m_vertices = new btVector3[totalVerts];
int* gIndices = new int[totalTriangles*3]; int* gIndices = new int[totalTriangles*3];
@@ -152,14 +203,14 @@ const float TRIANGLE_SIZE=20.f;
//height set to zero, but can also use curved landscape, just uncomment out the code //height set to zero, but can also use curved landscape, just uncomment out the code
float height = 0.f;//20.f*sinf(float(i)*wl)*cosf(float(j)*wl); float height = 0.f;//20.f*sinf(float(i)*wl)*cosf(float(j)*wl);
#ifdef FORCE_ZAXIS_UP #ifdef FORCE_ZAXIS_UP
gVertices[i+j*NUM_VERTS_X].setValue( m_vertices[i+j*NUM_VERTS_X].setValue(
(i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE, (i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,
(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE, (j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE,
height height
); );
#else #else
gVertices[i+j*NUM_VERTS_X].setValue( m_vertices[i+j*NUM_VERTS_X].setValue(
(i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE, (i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,
height, height,
(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE); (j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
@@ -183,13 +234,13 @@ const float TRIANGLE_SIZE=20.f;
} }
} }
btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles, m_indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
gIndices, gIndices,
indexStride, indexStride,
totalVerts,(btScalar*) &gVertices[0].x(),vertStride); totalVerts,(btScalar*) &m_vertices[0].x(),vertStride);
bool useQuantizedAabbCompression = true; bool useQuantizedAabbCompression = true;
groundShape = new btBvhTriangleMeshShape(indexVertexArrays,useQuantizedAabbCompression); groundShape = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression);
tr.setOrigin(btVector3(0,-4.5f,0)); tr.setOrigin(btVector3(0,-4.5f,0));
@@ -231,6 +282,7 @@ const float TRIANGLE_SIZE=20.f;
btHeightfieldTerrainShape* heightFieldShape = new btHeightfieldTerrainShape(width,length,heightfieldData,maxHeight,upIndex,useFloatDatam,flipQuadEdges);; btHeightfieldTerrainShape* heightFieldShape = new btHeightfieldTerrainShape(width,length,heightfieldData,maxHeight,upIndex,useFloatDatam,flipQuadEdges);;
groundShape = heightFieldShape; groundShape = heightFieldShape;
heightFieldShape->setUseDiamondSubdivision(true); heightFieldShape->setUseDiamondSubdivision(true);
btVector3 localScaling(20,20,20); btVector3 localScaling(20,20,20);
@@ -241,11 +293,7 @@ const float TRIANGLE_SIZE=20.f;
#endif // #endif //
m_collisionShapes.push_back(groundShape);
//create ground object //create ground object
localCreateRigidBody(0,tr,groundShape); localCreateRigidBody(0,tr,groundShape);
@@ -262,7 +310,10 @@ const float TRIANGLE_SIZE=20.f;
localTrans.setOrigin(btVector3(0,0,1)); localTrans.setOrigin(btVector3(0,0,1));
#else #else
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f)); btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));
m_collisionShapes.push_back(chassisShape);
btCompoundShape* compound = new btCompoundShape(); btCompoundShape* compound = new btCompoundShape();
m_collisionShapes.push_back(compound);
btTransform localTrans; btTransform localTrans;
localTrans.setIdentity(); localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis //localTrans effectively shifts the center of mass with respect to the chassis

View File

@@ -17,6 +17,8 @@ subject to the following restrictions:
class btVehicleTuning; class btVehicleTuning;
struct btVehicleRaycaster; struct btVehicleRaycaster;
class btCollisionShape;
#include "BulletDynamics/Vehicle/btRaycastVehicle.h" #include "BulletDynamics/Vehicle/btRaycastVehicle.h"
#include "DemoApplication.h" #include "DemoApplication.h"
@@ -28,6 +30,21 @@ class VehicleDemo : public DemoApplication
btRigidBody* m_carChassis; btRigidBody* m_carChassis;
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;
class btBroadphaseInterface* m_overlappingPairCache;
class btCollisionDispatcher* m_dispatcher;
class btConstraintSolver* m_constraintSolver;
class btDefaultCollisionConfiguration* m_collisionConfiguration;
class btTriangleIndexVertexArray* m_indexVertexArrays;
btVector3* m_vertices;
btRaycastVehicle::btVehicleTuning m_tuning; btRaycastVehicle::btVehicleTuning m_tuning;
btVehicleRaycaster* m_vehicleRayCaster; btVehicleRaycaster* m_vehicleRayCaster;
btRaycastVehicle* m_vehicle; btRaycastVehicle* m_vehicle;
@@ -40,6 +57,8 @@ class VehicleDemo : public DemoApplication
VehicleDemo(); VehicleDemo();
virtual ~VehicleDemo();
virtual void clientMoveAndDisplay(); virtual void clientMoveAndDisplay();
virtual void clientResetScene(); virtual void clientResetScene();