Cleaned up/simplified construction of a btRigidBody
Fixed memoryleak in btOptimizedBvh (delete []m_contiguousNodes;) Changed DemoApplication::localCreateRigidBody, so it adds the rigidbody to the btDynamicsWorld. Added check for duplicate objects in world when adding. Added assert to prevent setLinearVelocity on static rigidbodies Added btCollisionFilterGroups to btBroadphaseProxy removed duplicate 'btBroadphaseProxy* m_broadphaseProxy;' in btRigidBody
This commit is contained in:
@@ -72,9 +72,7 @@ public:
|
||||
//this create an internal copy of the vertices
|
||||
btCollisionShape* shape = new btConvexHullShape(&vertices[0],vertices.size());
|
||||
|
||||
btRigidBody* body = m_demoApp->localCreateRigidBody(isDynamic, mass, startTransform,shape);
|
||||
assert(body);
|
||||
m_demoApp->getDynamicsWorld()->addCollisionObject( body );
|
||||
btRigidBody* body = m_demoApp->localCreateRigidBody(mass, startTransform,shape);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -243,6 +243,7 @@ void CcdPhysicsDemo::initPhysics()
|
||||
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
|
||||
|
||||
m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver);
|
||||
//setGravity(btVector3(0,0,1));
|
||||
|
||||
m_dynamicsWorld->setDebugDrawer(&debugDrawer);
|
||||
|
||||
@@ -326,9 +327,8 @@ void CcdPhysicsDemo::initPhysics()
|
||||
if (!isDyna)
|
||||
mass = 0.f;
|
||||
|
||||
btRigidBody* body = localCreateRigidBody(isDyna,mass,trans,shape);
|
||||
btRigidBody* body = localCreateRigidBody(mass,trans,shape);
|
||||
|
||||
m_dynamicsWorld->addCollisionObject(body);
|
||||
|
||||
// Only do CCD if motion in one timestep (1.f/60.f) exceeds CUBE_HALF_EXTENTS
|
||||
body->m_ccdSquareMotionTreshold = CUBE_HALF_EXTENTS;
|
||||
|
||||
@@ -81,8 +81,19 @@ class MyColladaConverter : public ColladaConverter
|
||||
btCollisionShape* shape)
|
||||
{
|
||||
|
||||
btRigidBody* body = m_demoApp->localCreateRigidBody(isDynamic, mass, startTransform,shape);
|
||||
m_demoApp->getDynamicsWorld()->addCollisionObject(body);
|
||||
if (!isDynamic && (mass != 0.f))
|
||||
{
|
||||
printf("Warning: non-dynamic objects needs to have zero mass!\n");
|
||||
mass = 0.f;
|
||||
}
|
||||
|
||||
if (isDynamic && (mass == 0.f))
|
||||
{
|
||||
printf("Warning: dynamic rigidbodies needs nonzero mass!\n");
|
||||
mass = 1.f;
|
||||
}
|
||||
|
||||
btRigidBody* body = m_demoApp->localCreateRigidBody(mass, startTransform,shape);
|
||||
return body;
|
||||
}
|
||||
|
||||
|
||||
@@ -159,11 +159,10 @@ void ConcaveDemo::initPhysics()
|
||||
startTransform.setIdentity();
|
||||
startTransform.setOrigin(btVector3(0,0,0));
|
||||
|
||||
btRigidBody* staticBody = localCreateRigidBody(isDynamic, mass, startTransform,trimeshShape);
|
||||
btRigidBody* staticBody = localCreateRigidBody(mass, startTransform,trimeshShape);
|
||||
|
||||
staticBody->m_collisionFlags |=btCollisionObject::isStatic;
|
||||
|
||||
getDynamicsWorld()->addCollisionObject(staticBody);
|
||||
//enable custom material callback
|
||||
staticBody->m_collisionFlags |= btCollisionObject::customMaterialCallback;
|
||||
|
||||
@@ -172,7 +171,7 @@ void ConcaveDemo::initPhysics()
|
||||
{
|
||||
btCollisionShape* boxShape = new btBoxShape(btVector3(1,1,1));
|
||||
startTransform.setOrigin(btVector3(2*i,1,1));
|
||||
getDynamicsWorld()->addCollisionObject(localCreateRigidBody(true, 1, startTransform,boxShape));
|
||||
localCreateRigidBody(1, startTransform,boxShape);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,17 +62,14 @@ void ConstraintDemo::initPhysics()
|
||||
trans.setIdentity();
|
||||
trans.setOrigin(btVector3(0,20,0));
|
||||
|
||||
bool isDynamic = false;
|
||||
float mass = 1.f;
|
||||
|
||||
btRigidBody* body0 = localCreateRigidBody( isDynamic,mass,trans,shape);
|
||||
getDynamicsWorld()->addCollisionObject(body0);
|
||||
float mass = 0.f;
|
||||
btRigidBody* body0 = localCreateRigidBody( mass,trans,shape);
|
||||
trans.setOrigin(btVector3(2*CUBE_HALF_EXTENTS,20,0));
|
||||
isDynamic = true;
|
||||
btRigidBody* body1 = localCreateRigidBody( isDynamic,mass,trans,shape);
|
||||
|
||||
mass = 1.f;
|
||||
btRigidBody* body1 = localCreateRigidBody( mass,trans,shape);
|
||||
body1->setDamping(0.3,0.3);
|
||||
getDynamicsWorld()->addCollisionObject(body1);
|
||||
|
||||
|
||||
|
||||
clientResetScene();
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ void ConvexDecompositionDemo::initPhysics(const char* filename)
|
||||
startTransform.setIdentity();
|
||||
startTransform.setOrigin(btVector3(0,-4,0));
|
||||
|
||||
localCreateRigidBody(false,0,startTransform,new btBoxShape(btVector3(30,2,30)));
|
||||
localCreateRigidBody(0.f,startTransform,new btBoxShape(btVector3(30,2,30)));
|
||||
|
||||
class MyConvexDecomposition : public ConvexDecomposition::ConvexDecompInterface
|
||||
{
|
||||
@@ -192,8 +192,7 @@ void ConvexDecompositionDemo::initPhysics(const char* filename)
|
||||
btTransform trans;
|
||||
trans.setIdentity();
|
||||
trans.setOrigin(centroid);
|
||||
btRigidBody* body = m_convexDemo->localCreateRigidBody(isDynamic, mass, trans,convexShape);
|
||||
m_convexDemo->getDynamicsWorld()->addCollisionObject(body);
|
||||
btRigidBody* body = m_convexDemo->localCreateRigidBody( mass, trans,convexShape);
|
||||
mBaseCount+=result.mHullVcount; // advance the 'base index' counter.
|
||||
|
||||
|
||||
@@ -237,7 +236,7 @@ void ConvexDecompositionDemo::initPhysics(const char* filename)
|
||||
startTransform.setIdentity();
|
||||
startTransform.setOrigin(btVector3(20,2,0));
|
||||
|
||||
localCreateRigidBody(isDynamic, mass, startTransform,convexShape);
|
||||
localCreateRigidBody(mass, startTransform,convexShape);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -335,7 +335,7 @@ void DemoApplication::keyboardCallback(unsigned char key, int x, int y)
|
||||
break;
|
||||
}
|
||||
|
||||
if (getDynamicsWorld()->getDebugDrawer())
|
||||
if (getDynamicsWorld() && getDynamicsWorld()->getDebugDrawer())
|
||||
getDynamicsWorld()->getDebugDrawer()->setDebugMode(m_debugMode);
|
||||
|
||||
glutPostRedisplay();
|
||||
@@ -393,8 +393,7 @@ void DemoApplication::shootBox(const btVector3& destination)
|
||||
startTransform.setOrigin(camPos);
|
||||
btCollisionShape* boxShape = new btBoxShape(btVector3(1.f,1.f,1.f));
|
||||
|
||||
btRigidBody* body = this->localCreateRigidBody(isDynamic, mass, startTransform,boxShape);
|
||||
m_dynamicsWorld->addCollisionObject(body);
|
||||
btRigidBody* body = this->localCreateRigidBody(mass, startTransform,boxShape);
|
||||
|
||||
btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]);
|
||||
linVel.normalize();
|
||||
@@ -606,30 +605,36 @@ void DemoApplication::mouseMotionFunc(int x,int y)
|
||||
|
||||
|
||||
|
||||
btRigidBody* DemoApplication::localCreateRigidBody(bool isDynamic, float mass, const btTransform& startTransform,btCollisionShape* shape)
|
||||
btRigidBody* DemoApplication::localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape)
|
||||
{
|
||||
//rigidbody is dynamic if and only if mass is non zero, otherwise static
|
||||
bool isDynamic = (mass != 0.f);
|
||||
|
||||
btVector3 localInertia(0,0,0);
|
||||
if (isDynamic)
|
||||
shape->calculateLocalInertia(mass,localInertia);
|
||||
|
||||
btMassProps massProps(0.f,localInertia);
|
||||
|
||||
btRigidBody* body = new btRigidBody(massProps);
|
||||
body->m_collisionShape = shape;
|
||||
body->m_worldTransform = startTransform;
|
||||
btRigidBody* body = new btRigidBody(mass,startTransform,shape,localInertia);
|
||||
|
||||
if (!isDynamic)
|
||||
//filtering allows to excluded collision pairs, early in the collision pipeline (broadphase)
|
||||
bool useFiltering = true;
|
||||
if (useFiltering)
|
||||
{
|
||||
body->m_collisionFlags = btCollisionObject::isStatic;//??
|
||||
// body->getBroadphaseProxy()->m_collisionFilterGroup = 1;/btCcdConstructionInfo::StaticFilter;
|
||||
// body->getBroadphaseProxy()->m_collisionFilterMask = btCcdConstructionInfo::AllFilter ^ btCcdConstructionInfo::StaticFilter;
|
||||
body->setMassProps( 0.f, localInertia);
|
||||
short collisionFilterGroup = isDynamic?
|
||||
btBroadphaseProxy::DefaultFilter :
|
||||
btBroadphaseProxy::StaticFilter;
|
||||
short collisionFilterMask = isDynamic?
|
||||
btBroadphaseProxy::AllFilter :
|
||||
btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter;
|
||||
|
||||
m_dynamicsWorld->addCollisionObject(body,collisionFilterGroup,collisionFilterMask);
|
||||
} else
|
||||
{
|
||||
body->setMassProps( mass, localInertia);
|
||||
body->m_collisionFlags = 0;
|
||||
//no collision filtering, so always create an overlapping pair, even between static-static etc.
|
||||
m_dynamicsWorld->addCollisionObject(body);
|
||||
}
|
||||
|
||||
|
||||
//Bullet uses per-object gravity
|
||||
body->setGravity(m_gravity);
|
||||
|
||||
return body;
|
||||
|
||||
@@ -147,7 +147,7 @@ public:
|
||||
}
|
||||
btVector3 getRayTo(int x,int y);
|
||||
|
||||
btRigidBody* localCreateRigidBody(bool isDynamic, float mass, const btTransform& startTransform,btCollisionShape* shape);
|
||||
btRigidBody* localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape);
|
||||
|
||||
///callback methods by glut
|
||||
|
||||
|
||||
@@ -134,8 +134,7 @@ void UserCollisionAlgorithm::initPhysics()
|
||||
startTransform.setIdentity();
|
||||
startTransform.setOrigin(btVector3(0,-2,0));
|
||||
|
||||
btRigidBody* staticBody= localCreateRigidBody(isDynamic, mass, startTransform,trimeshShape);
|
||||
getDynamicsWorld()->addCollisionObject(staticBody);
|
||||
btRigidBody* staticBody= localCreateRigidBody(mass, startTransform,trimeshShape);
|
||||
//enable custom material callback
|
||||
staticBody->m_collisionFlags |= btCollisionObject::customMaterialCallback;
|
||||
|
||||
@@ -144,12 +143,12 @@ void UserCollisionAlgorithm::initPhysics()
|
||||
{
|
||||
btCollisionShape* sphereShape = new btSphereShape(1);
|
||||
startTransform.setOrigin(btVector3(1,2*i,1));
|
||||
btRigidBody* body = localCreateRigidBody(true, 1, startTransform,sphereShape);
|
||||
getDynamicsWorld()->addCollisionObject(body);
|
||||
btRigidBody* body = localCreateRigidBody(1, startTransform,sphereShape);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
m_dynamicsWorld->setDebugDrawer(&debugDrawer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user