wrapped up first version of the FractureDemo

move 'split impulse' / position solver before velocity solver, so that applied impulse is available for fracture
add btSliderConstraint::getAngularPos see http://code.google.com/p/bullet/issues/detail?id=489
This commit is contained in:
erwin.coumans
2011-03-18 00:20:52 +00:00
parent 74a65a6207
commit f17fa297d5
6 changed files with 172 additions and 149 deletions

View File

@@ -73,45 +73,37 @@ void FractureDemo::initPhysics()
//m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld = new btFractureDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
btFractureDynamicsWorld* fractureWorld = new btFractureDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld = fractureWorld;
m_dynamicsWorld->getDispatchInfo().m_convexMaxDistanceUseCPT = true;
m_ShootBoxInitialSpeed=100;
//m_splitImpulse removes the penetration resolution from the applied impulse, otherwise objects might fracture due to deep penetrations.
m_dynamicsWorld->getSolverInfo().m_splitImpulse = true;
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
{
///create a few basic rigid bodies
btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
btCollisionShape* groundShape = new btBoxShape(btVector3(50,1,50));
/// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),0);
m_collisionShapes.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-50,0));
//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
{
btScalar mass(0.);
//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)
groundShape->calculateLocalInertia(mass,localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//add the body to the dynamics world
m_dynamicsWorld->addRigidBody(body);
groundTransform.setOrigin(btVector3(0,0,0));
localCreateRigidBody(0.f,groundTransform,groundShape);
}
{
///create a few basic rigid bodies
btCollisionShape* shape = new btBoxShape(btVector3(1,1,1));
m_collisionShapes.push_back(shape);
btTransform tr;
tr.setIdentity();
tr.setOrigin(btVector3(5,2,0));
localCreateRigidBody(0.f,tr,shape);
}
{
//create a few dynamic rigidbodies
@@ -143,31 +135,38 @@ void FractureDemo::initPhysics()
btTransform trans;
trans.setIdentity();
btVector3 pos(i*2*CUBE_HALF_EXTENTS ,10,0);
btVector3 pos(i*2*CUBE_HALF_EXTENTS ,20,0);
trans.setOrigin(pos);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
btFractureBody* body = new btFractureBody(rbInfo, m_dynamicsWorld);
body->setActivationState(ISLAND_SLEEPING);
body->setLinearVelocity(btVector3(0,-10,0));
m_dynamicsWorld->addRigidBody(body);
body->setActivationState(ISLAND_SLEEPING);
}
}
clientResetScene();
fractureWorld->stepSimulation(1./60.,0);
fractureWorld->glueCallback();
}
void FractureDemo::clientResetScene()
{
exitPhysics();
initPhysics();
}
void FractureDemo::clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -204,11 +203,10 @@ void FractureDemo::showMessage()
glColor3f(0, 0, 0);
char buf[124];
int lineWidth=350;
int lineWidth=380;
int xStart = m_glutScreenWidth - lineWidth;
int yStart = 20;
glRasterPos3f(xStart, yStart, 0);
btFractureDynamicsWorld* world = (btFractureDynamicsWorld*)m_dynamicsWorld;
if (world->getFractureMode())
{
@@ -217,12 +215,14 @@ void FractureDemo::showMessage()
{
sprintf(buf,"Glue mode");
}
GLDebugDrawString(xStart,20,buf);
yStart+=20;
glRasterPos3f(xStart, yStart, 0);
GLDebugDrawString(xStart,yStart,buf);
sprintf(buf,"f to toggle fracture/glue mode");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
sprintf(buf,"space to restart, mouse to pick/shoot");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
resetPerspectiveProjection();
glEnable(GL_LIGHTING);
}
@@ -246,15 +246,6 @@ void FractureDemo::displayCallback(void) {
swapBuffers();
}
void FractureDemo::keyboardCallback(unsigned char key, int x, int y)
{
if (key=='f')
{
} else
{
PlatformDemoApplication::keyboardCallback(key,x,y);
}
}
void FractureDemo::keyboardUpCallback(unsigned char key, int x, int y)
{
@@ -269,17 +260,6 @@ void FractureDemo::keyboardUpCallback(unsigned char key, int x, int y)
}
void FractureDemo::shootBox(const btVector3& destination)
{
@@ -359,16 +339,22 @@ void FractureDemo::exitPhysics()
delete shape;
}
m_collisionShapes.clear();
delete m_dynamicsWorld;
m_dynamicsWorld=0;
delete m_solver;
m_solver=0;
delete m_broadphase;
m_broadphase=0;
delete m_dispatcher;
m_dispatcher=0;
delete m_collisionConfiguration;
m_collisionConfiguration=0;
}

View File

@@ -67,10 +67,10 @@ class FractureDemo : public PlatformDemoApplication
virtual void displayCallback();
virtual void keyboardCallback(unsigned char key, int x, int y);
virtual void keyboardUpCallback(unsigned char key, int x, int y);
virtual void clientResetScene();
static DemoApplication* Create()
{
FractureDemo* demo = new FractureDemo;

View File

@@ -7,17 +7,19 @@
btFractureDynamicsWorld::btFractureDynamicsWorld ( btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
:btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration),
m_fracturingMode(false)
m_fracturingMode(true)
{
}
void btFractureDynamicsWorld::glueCallback(btScalar timeStep)
void btFractureDynamicsWorld::glueCallback()
{
int numManifolds = getDispatcher()->getNumManifolds();
///first build the islands based on axis aligned bounding box overlap
btUnionFind unionFind;
int index = 0;
@@ -102,7 +104,7 @@ void btFractureDynamicsWorld::glueCallback(btScalar timeStep)
btAlignedObjectArray<btCollisionObject*> removedObjects;
//update the sleeping state for bodies, if all are sleeping
///iterate over all islands
for ( startIslandIndex=0;startIslandIndex<numElem;startIslandIndex = endIslandIndex)
{
int islandId = unionFind.getElement(startIslandIndex).m_id;
@@ -129,19 +131,25 @@ void btFractureDynamicsWorld::glueCallback(btScalar timeStep)
numObjects++;
}
///Then for each island that contains at least two objects and one fracture object
if (fractureObjectIndex>=0 && numObjects>1)
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
btFractureBody* fracObj = (btFractureBody*)getCollisionObjectArray()[fractureObjectIndex];
///glueing objects means creating a new compound and removing the old objects
///delay the removal of old objects to avoid array indexing problems
removedObjects.push_back(fracObj);
m_fractureBodies.remove(fracObj);
btAlignedObjectArray<btScalar> massArray;
btAlignedObjectArray<btVector3> oldImpulses;
btAlignedObjectArray<btVector3> oldCenterOfMassesWS;
oldImpulses.push_back(fracObj->getLinearVelocity()/1./fracObj->getInvMass());
oldCenterOfMassesWS.push_back(fracObj->getCenterOfMassPosition());
btScalar totalMass = 0.f;
@@ -178,9 +186,15 @@ void btFractureDynamicsWorld::glueCallback(btScalar timeStep)
btCollisionObject* otherCollider = getCollisionObjectArray()[i];
btRigidBody* otherObject = btRigidBody::upcast(otherCollider);
//don't glue/merge with static objects right now, otherwise everything gets stuck to the ground
///todo: expose this as a callback
if (!otherObject || !otherObject->getInvMass())
continue;
oldImpulses.push_back(otherObject->getLinearVelocity()*(1.f/otherObject->getInvMass()));
oldCenterOfMassesWS.push_back(otherObject->getCenterOfMassPosition());
removedObjects.push_back(otherObject);
m_fractureBodies.remove((btFractureBody*)otherObject);
@@ -221,6 +235,16 @@ void btFractureDynamicsWorld::glueCallback(btScalar timeStep)
btFractureBody* newBody = new btFractureBody(totalMass,0,newCompound,localInertia, &massArray[0], numChildren,this);
newBody->recomputeConnectivity(this);
newBody->setWorldTransform(fracObj->getWorldTransform()*shift);
//now the linear/angular velocity is still zero, apply the impulses
for (int i=0;i<oldImpulses.size();i++)
{
btVector3 rel_pos = oldCenterOfMassesWS[i]-newBody->getCenterOfMassPosition();
const btVector3& imp = oldImpulses[i];
newBody->applyImpulse(imp, rel_pos);
}
addRigidBody(newBody);
@@ -255,21 +279,19 @@ struct btFracturePair
void btFractureDynamicsWorld::solveConstraints(btContactSolverInfo& solverInfo)
{
//todo: add some logic
// save all velocities
// 1) that if a fracture object break
// 2) revert all velocties
// 3) apply impulses for the fracture bodies at the contact locations
// 4)and run the constaint solver again
// todo: after fracture we should run the solver again for better realism
// for example
// save all velocities and if one or more objects fracture:
// 1) revert all velocties
// 2) apply impulses for the fracture bodies at the contact locations
// 3)and run the constaint solver again
btDiscreteDynamicsWorld::solveConstraints(solverInfo);
fractureCallback(solverInfo.m_timeStep);
fractureCallback();
}
void btFractureDynamicsWorld::addNewBody(const btTransform& oldTransform,btScalar* masses, btCompoundShape* oldCompound)
btFractureBody* btFractureDynamicsWorld::addNewBody(const btTransform& oldTransform,btScalar* masses, btCompoundShape* oldCompound)
{
int i;
@@ -288,6 +310,7 @@ void btFractureDynamicsWorld::addNewBody(const btTransform& oldTransform,btScala
newBody->setCollisionFlags(newBody->getCollisionFlags()|btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
newBody->setWorldTransform(oldTransform*shift);
addRigidBody(newBody);
return newBody;
}
void btFractureDynamicsWorld::addRigidBody(btRigidBody* body)
@@ -398,7 +421,10 @@ void btFractureDynamicsWorld::breakDisconnectedParts( btFractureBody* fracObj)
}
if (numShapes)
{
addNewBody(fracObj->getWorldTransform(),&masses[0],newCompound);
btFractureBody* newBody = addNewBody(fracObj->getWorldTransform(),&masses[0],newCompound);
newBody->setLinearVelocity(fracObj->getLinearVelocity());
newBody->setAngularVelocity(fracObj->getAngularVelocity());
numIslands++;
}
}
@@ -412,16 +438,17 @@ void btFractureDynamicsWorld::breakDisconnectedParts( btFractureBody* fracObj)
}
#include <stdio.h>
void btFractureDynamicsWorld::fractureCallback( btScalar timeStep)
void btFractureDynamicsWorld::fractureCallback( )
{
btAlignedObjectArray<btFracturePair> sFracturePairs;
if (!m_fracturingMode)
{
glueCallback(timeStep);
glueCallback();
return;
}
@@ -442,12 +469,15 @@ void btFractureDynamicsWorld::fractureCallback( btScalar timeStep)
totalImpact += manifold->getContactPoint(p).m_appliedImpulse;
}
// printf("totalImpact=%f\n",totalImpact);
static float maxImpact = 0;
if (totalImpact>maxImpact)
maxImpact = totalImpact;
//some threshold otherwise resting contact would break objects after a while
if (totalImpact < 10)
if (totalImpact < 40.f)
continue;
// printf("strong impact\n");

View File

@@ -17,11 +17,7 @@ class btFractureDynamicsWorld : public btDiscreteDynamicsWorld
bool m_fracturingMode;
void glueCallback(btScalar timeStep);
void fractureCallback( btScalar timeStep);
void addNewBody(const btTransform& oldTransform,btScalar* masses, btCompoundShape* oldCompound);
btFractureBody* addNewBody(const btTransform& oldTransform,btScalar* masses, btCompoundShape* oldCompound);
void breakDisconnectedParts( btFractureBody* fracObj);
@@ -42,6 +38,13 @@ public:
}
bool getFractureMode() const { return m_fracturingMode;}
///normally those callbacks are called internally by the 'solveConstraints'
void glueCallback();
///normally those callbacks are called internally by the 'solveConstraints'
void fractureCallback();
};
#endif //_BT_FRACTURE_DYNAMICS_WORLD_H

View File

@@ -1115,12 +1115,13 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations(
//should traverse the contacts random order...
int iteration;
{
solveGroupCacheFriendlySplitImpulseIterations(bodies ,numBodies,manifoldPtr, numManifolds,constraints,numConstraints,infoGlobal,debugDrawer,stackAlloc);
for ( iteration = 0;iteration<infoGlobal.m_numIterations;iteration++)
{
solveSingleIteration(iteration, bodies ,numBodies,manifoldPtr, numManifolds,constraints,numConstraints,infoGlobal,debugDrawer,stackAlloc);
}
solveGroupCacheFriendlySplitImpulseIterations(bodies ,numBodies,manifoldPtr, numManifolds,constraints,numConstraints,infoGlobal,debugDrawer,stackAlloc);
}
return 0.f;
}

View File

@@ -236,7 +236,10 @@ public:
btScalar getTargetAngMotorVelocity() { return m_targetAngMotorVelocity; }
void setMaxAngMotorForce(btScalar maxAngMotorForce) { m_maxAngMotorForce = maxAngMotorForce; }
btScalar getMaxAngMotorForce() { return m_maxAngMotorForce; }
btScalar getLinearPos() { return m_linPos; }
btScalar getLinearPos() const { return m_linPos; }
btScalar getAngularPos() const { return m_angPos; }
// access for ODE solver