add FractureDemo to AllBulletDemos

improvements in CCD handling
some cleanup of CcdPhysicsDemo and BasicDemo
This commit is contained in:
erwin.coumans
2011-04-09 01:14:21 +00:00
parent 2291a6a9d3
commit cdddf9d25a
29 changed files with 550 additions and 867 deletions

View File

@@ -4,8 +4,8 @@ Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
@@ -13,280 +13,118 @@ subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
*/
//enable just one, DO_BENCHMARK_PYRAMIDS or DO_WALL
//#define DO_BENCHMARK_PYRAMIDS 1
#define DO_WALL 1
//Note: some of those settings need 'DO_WALL' demo
//#define USE_KINEMATIC_GROUND 1
//#define PRINT_CONTACT_STATISTICS 1
//#define USER_DEFINED_FRICTION_MODEL 1
//#define USE_CUSTOM_NEAR_CALLBACK 1
//#define CENTER_OF_MASS_SHIFT 1
//#define VERBOSE_TIMESTEPPING_CONSOLEOUTPUT 1
//#define USE_PARALLEL_DISPATCHER 1
//#define USE_PARALLEL_SOLVER 1 //experimental parallel solver
//from Bullet 2.68 onwards ODE Quickstep constraint solver is optional part of Bullet, re-distributed under the ZLib license with permission of Russell L. Smith
//#define COMPARE_WITH_QUICKSTEP 1
#include "btBulletDynamicsCommon.h"
#include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h"
#ifdef USE_PARALLEL_DISPATCHER
#include "BulletMultiThreaded/SpuGatheringCollisionDispatcher.h"
#ifdef _WIN32
#include "BulletMultiThreaded/Win32ThreadSupport.h"
#include "BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
#endif //_WIN32
#ifdef USE_LIBSPE2
#include "../../Extras/BulletMultiThreaded/SpuLibspe2Support.h"
#endif //USE_LIBSPE2
#ifdef USE_PARALLEL_SOLVER
#include "BulletMultiThreaded/SpuParallelSolver.h"
#include "BulletMultiThreaded/SpuSolverTask/SpuParallellSolverTask.h"
#endif //USE_PARALLEL_SOLVER
#endif//USE_PARALLEL_DISPATCHER
#include "LinearMath/btQuickprof.h"
#include "LinearMath/btIDebugDraw.h"
#include "GLDebugFont.h"
#include <stdio.h> //printf debugging
static float gCollisionMargin = 0.05f;
#include "CcdPhysicsDemo.h"
#include "GL_ShapeDrawer.h"
#include "GlutStuff.h"
btTransform comOffset;
btVector3 comOffsetVec(0,2,0);
extern float eye[3];
extern int glutScreenWidth;
extern int glutScreenHeight;
const int maxProxies = 32766;
const int maxOverlap = 65535;
bool createConstraint = true;//false;
#ifdef CENTER_OF_MASS_SHIFT
bool useCompound = true;
#else
bool useCompound = false;
#endif
#ifdef _DEBUG
const int gNumObjects = 120;
#else
const int gNumObjects = 120;//try this in release mode: 3000. never go above 16384, unless you increate maxNumObjects value in DemoApplication.cp
#endif
const int maxNumObjects = 32760;
int shapeIndex[maxNumObjects];
#define CUBE_HALF_EXTENTS 0.5
#define EXTRA_HEIGHT 1.f
#define EXTRA_HEIGHT -10.f
//GL_LineSegmentShape shapeE(btVector3(-50,0,0),
// btVector3(50,0,0));
#include "CcdPhysicsDemo.h"
#include "GlutStuff.h"
#include "GLDebugFont.h"
///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
#include "btBulletDynamicsCommon.h"
#include <stdio.h> //printf debugging
#include "GLDebugDrawer.h"
#if 0
extern btAlignedObjectArray<btVector3> debugContacts;
extern btAlignedObjectArray<btVector3> debugNormals;
#endif
static GLDebugDrawer sDebugDrawer;
void CcdPhysicsDemo::createStack( btCollisionShape* boxShape, float halfCubeSize, int size, float zPos )
CcdPhysicsDemo::CcdPhysicsDemo()
:m_ccdMode(USE_CCD)
{
btTransform trans;
trans.setIdentity();
for(int i=0; i<size; i++)
{
// This constructs a row, from left to right
int rowSize = size - i;
for(int j=0; j< rowSize; j++)
{
btVector3 pos;
pos.setValue(
-rowSize * halfCubeSize + halfCubeSize + j * 2.0f * halfCubeSize,
halfCubeSize + i * halfCubeSize * 2.0f,
zPos);
trans.setOrigin(pos);
btScalar mass = 1.f;
btRigidBody* body = 0;
body = localCreateRigidBody(mass,trans,boxShape);
#ifdef USER_DEFINED_FRICTION_MODEL
///Advanced use: override the friction solver
body->m_frictionSolverType = USER_CONTACT_SOLVER_TYPE1;
#endif //USER_DEFINED_FRICTION_MODEL
}
}
setDebugMode(btIDebugDraw::DBG_DrawText+btIDebugDraw::DBG_NoHelpText);
setCameraDistance(btScalar(20.));
}
////////////////////////////////////
//by default, Bullet will use its own nearcallback, but you can override it using dispatcher->setNearCallback()
void customNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo)
{
btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
if (dispatcher.needsCollision(colObj0,colObj1))
{
//dispatcher will keep algorithms persistent in the collision pair
if (!collisionPair.m_algorithm)
{
collisionPair.m_algorithm = dispatcher.findAlgorithm(colObj0,colObj1);
}
if (collisionPair.m_algorithm)
{
btManifoldResult contactPointResult(colObj0,colObj1);
if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE)
{
//discrete collision detection query
collisionPair.m_algorithm->processCollision(colObj0,colObj1,dispatchInfo,&contactPointResult);
} else
{
//continuous collision detection query, time of impact (toi)
float toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);
if (dispatchInfo.m_timeOfImpact > toi)
dispatchInfo.m_timeOfImpact = toi;
}
}
}
}
//experimental jitter damping (1 = no damping, 0 = total damping once motion below threshold)
extern btScalar gJitterVelocityDampingFactor;
extern int gNumManifold;
extern int gOverlappingPairs;
void CcdPhysicsDemo::clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#ifdef USE_KINEMATIC_GROUND
//btQuaternion kinRotation(btVector3(0,0,1),0.);
btVector3 kinTranslation(-0.01,0,0);
//kinematic object
btCollisionObject* colObj = m_dynamicsWorld->getCollisionObjectArray()[0];
//is this a rigidbody with a motionstate? then use the motionstate to update positions!
if (btRigidBody::upcast(colObj) && btRigidBody::upcast(colObj)->getMotionState())
{
btTransform newTrans;
btRigidBody::upcast(colObj)->getMotionState()->getWorldTransform(newTrans);
newTrans.getOrigin()+=kinTranslation;
btRigidBody::upcast(colObj)->getMotionState()->setWorldTransform(newTrans);
} else
{
m_dynamicsWorld->getCollisionObjectArray()[0]->getWorldTransform().getOrigin() += kinTranslation;
}
#endif //USE_KINEMATIC_GROUND
float dt = getDeltaTimeMicroseconds() * 0.000001f;
// printf("dt = %f: ",dt);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//simple dynamics world doesn't handle fixed-time-stepping
float ms = getDeltaTimeMicroseconds();
///step the simulation
if (m_dynamicsWorld)
{
//#define FIXED_STEP 1
#ifdef FIXED_STEP
m_dynamicsWorld->stepSimulation(1.0f/60.f,0);
#else
//during idle mode, just run 1 simulation step maximum
int maxSimSubSteps = m_idle ? 1 : 1;
if (m_idle)
dt = 1.0/420.f;
int numSimSteps = 0;
numSimSteps = m_dynamicsWorld->stepSimulation(dt,maxSimSubSteps);
m_dynamicsWorld->stepSimulation(1./60.);//ms / 1000000.f);
//optional but useful: debug drawing
m_dynamicsWorld->debugDrawWorld();
#ifdef VERBOSE_TIMESTEPPING_CONSOLEOUTPUT
if (!numSimSteps)
printf("Interpolated transforms\n");
else
{
if (numSimSteps > maxSimSubSteps)
{
//detect dropping frames
printf("Dropped (%i) simulation steps out of %i\n",numSimSteps - maxSimSubSteps,numSimSteps);
} else
{
printf("Simulated (%i) steps\n",numSimSteps);
}
}
#endif //VERBOSE_TIMESTEPPING_CONSOLEOUTPUT
#endif
}
renderme();
#ifdef USE_QUICKPROF
btProfiler::beginBlock("render");
#endif //USE_QUICKPROF
renderme();
//render the graphics objects, with center of mass shift
updateCamera();
#ifdef USE_QUICKPROF
btProfiler::endBlock("render");
displayText();
#if 0
for (int i=0;i<debugContacts.size();i++)
{
getDynamicsWorld()->getDebugDrawer()->drawContactPoint(debugContacts[i],debugNormals[i],0,0,btVector3(1,0,0));
}
#endif
glFlush();
//some additional debugging info
#ifdef PRINT_CONTACT_STATISTICS
printf("num manifolds: %i\n",gNumManifold);
printf("num gOverlappingPairs: %i\n",gOverlappingPairs);
#endif //PRINT_CONTACT_STATISTICS
glutSwapBuffers();
glFlush();
swapBuffers();
}
void CcdPhysicsDemo::displayText()
{
int lineWidth=440;
int xStart = m_glutScreenWidth - lineWidth;
int yStart = 20;
if((getDebugMode() & btIDebugDraw::DBG_DrawText)!=0)
{
setOrthographicProjection();
glDisable(GL_LIGHTING);
glColor3f(0, 0, 0);
char buf[124];
glRasterPos3f(xStart, yStart, 0);
switch (m_ccdMode)
{
case USE_CCD:
{
sprintf(buf,"Predictive contacts and motion clamping");
break;
}
case USE_NO_CCD:
{
sprintf(buf,"CCD handling disabled");
break;
}
default:
{
sprintf(buf,"unknown CCD setting");
};
};
GLDebugDrawString(xStart,20,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"Press 'p' to change CCD mode");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"Press '.' or right mouse to shoot bullets");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
glRasterPos3f(xStart, yStart, 0);
sprintf(buf,"space to restart, h(elp), t(ext), w(ire)");
yStart+=20;
GLDebugDrawString(xStart,yStart,buf);
resetPerspectiveProjection();
glEnable(GL_LIGHTING);
}
}
@@ -294,233 +132,136 @@ void CcdPhysicsDemo::clientMoveAndDisplay()
void CcdPhysicsDemo::displayCallback(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderme();
//optional but useful: debug drawing
displayText();
//optional but useful: debug drawing to detect problems
if (m_dynamicsWorld)
{
m_dynamicsWorld->debugDrawWorld();
}
#if 0
for (int i=0;i<debugContacts.size();i++)
{
getDynamicsWorld()->getDebugDrawer()->drawContactPoint(debugContacts[i],debugNormals[i],0,0,btVector3(1,0,0));
}
#endif
glFlush();
glutSwapBuffers();
swapBuffers();
}
///User-defined friction model, the most simple friction model available: no friction
float myFrictionModel( btRigidBody& body1, btRigidBody& body2, btManifoldPoint& contactPoint, const btContactSolverInfo& solverInfo )
{
//don't do any friction
return 0.f;
}
void CcdPhysicsDemo::initPhysics()
{
setTexturing(true);
setShadows(false);
#ifdef USE_PARALLEL_DISPATCHER
#ifdef _WIN32
m_threadSupportSolver = 0;
m_threadSupportCollision = 0;
#endif //
#endif
//#define USE_GROUND_PLANE 1
#ifdef USE_GROUND_PLANE
m_collisionShapes.push_back(new btStaticPlaneShape(btVector3(0,1,0),0.5));
#else
///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate.
///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346
m_collisionShapes.push_back(new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200)));
#endif
#ifdef DO_BENCHMARK_PYRAMIDS
m_collisionShapes.push_back(new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
#else
// m_collisionShapes.push_back(new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
#endif
setShadows(true);
m_ShootBoxInitialSpeed = 4000.f;
#ifdef DO_BENCHMARK_PYRAMIDS
setCameraDistance(32.5f);
#endif
#ifdef DO_BENCHMARK_PYRAMIDS
m_azi = 90.f;
#endif //DO_BENCHMARK_PYRAMIDS
m_dispatcher=0;
///collision configuration contains default setup for memory, collision setup
m_collisionConfiguration = new btDefaultCollisionConfiguration();
// m_collisionConfiguration->setConvexConvexMultipointIterations();
#ifdef USE_PARALLEL_DISPATCHER
int maxNumOutstandingTasks = 4;
#ifdef USE_WIN32_THREADING
m_threadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
"collision",
processCollisionTask,
createCollisionLocalStoreMemory,
maxNumOutstandingTasks));
#else
#ifdef USE_LIBSPE2
spe_program_handle_t * program_handle;
#ifndef USE_CESOF
program_handle = spe_image_open ("./spuCollision.elf");
if (program_handle == NULL)
{
perror( "SPU OPEN IMAGE ERROR\n");
}
else
{
printf( "IMAGE OPENED\n");
}
#else
extern spe_program_handle_t spu_program;
program_handle = &spu_program;
#endif
SpuLibspe2Support* threadSupportCollision = new SpuLibspe2Support( program_handle, maxNumOutstandingTasks);
#endif //USE_LIBSPE2
///Playstation 3 SPU (SPURS) version is available through PS3 Devnet
/// For Unix/Mac someone could implement a pthreads version of btThreadSupportInterface?
///you can hook it up to your custom task scheduler by deriving from btThreadSupportInterface
#endif
m_dispatcher = new SpuGatheringCollisionDispatcher(m_threadSupportCollision,maxNumOutstandingTasks,m_collisionConfiguration);
// m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
#else
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
#endif //USE_PARALLEL_DISPATCHER
#ifdef USE_CUSTOM_NEAR_CALLBACK
//this is optional
m_dispatcher->setNearCallback(customNearCallback);
#endif
m_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_collisionConfiguration->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,CONVEX_SHAPE_PROXYTYPE));
m_broadphase = new btDbvtBroadphase();
#ifdef COMPARE_WITH_QUICKSTEP
m_solver = new btOdeQuickstepConstraintSolver();
#else
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
btSequentialImpulseConstraintSolver* sol = new btSequentialImpulseConstraintSolver;
m_solver = sol;
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld ->setDebugDrawer(&sDebugDrawer);
m_dynamicsWorld->getSolverInfo().m_splitImpulse=true;
m_dynamicsWorld->getSolverInfo().m_numIterations = 20;
#ifdef USE_PARALLEL_SOLVER
m_threadSupportSolver = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
"solver",
processSolverTask,
createSolverLocalStoreMemory,
maxNumOutstandingTasks));
m_solver = new btParallelSequentialImpulseSolver(m_threadSupportSolver,maxNumOutstandingTasks);
#else
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
m_solver = solver;//new btOdeQuickstepConstraintSolver();
#endif //USE_PARALLEL_SOLVER
#endif
btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(m_dispatcher,m_broadphase,m_solver,m_collisionConfiguration);
m_dynamicsWorld = world;
///SOLVER_RANDMIZE_ORDER makes cylinder stacking a bit more stable
world->getSolverInfo().m_solverMode |= SOLVER_RANDMIZE_ORDER;
#ifdef USER_DEFINED_FRICTION_MODEL
//user defined friction model is not supported in 'cache friendly' solver yet, so switch to old solver
world->getSolverInfo().m_solverMode = SOLVER_RANDMIZE_ORDER;
#endif //USER_DEFINED_FRICTION_MODEL
#ifdef DO_BENCHMARK_PYRAMIDS
world->getSolverInfo().m_numIterations = 4;
#endif //DO_BENCHMARK_PYRAMIDS
m_dynamicsWorld->getDispatchInfo().m_enableSPU = true;
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
#ifdef USER_DEFINED_FRICTION_MODEL
if (m_ccdMode==USE_CCD)
{
//m_solver->setContactSolverFunc(ContactSolverFunc func,USER_CONTACT_SOLVER_TYPE1,DEFAULT_CONTACT_SOLVER_TYPE);
solver->SetFrictionSolverFunc(myFrictionModel,USER_CONTACT_SOLVER_TYPE1,DEFAULT_CONTACT_SOLVER_TYPE);
solver->SetFrictionSolverFunc(myFrictionModel,DEFAULT_CONTACT_SOLVER_TYPE,USER_CONTACT_SOLVER_TYPE1);
solver->SetFrictionSolverFunc(myFrictionModel,USER_CONTACT_SOLVER_TYPE1,USER_CONTACT_SOLVER_TYPE1);
//m_physicsEnvironmentPtr->setNumIterations(2);
m_dynamicsWorld->getDispatchInfo().m_useContinuous=true;
} else
{
m_dynamicsWorld->getDispatchInfo().m_useContinuous=false;
}
#endif //USER_DEFINED_FRICTION_MODEL
m_dynamicsWorld->setGravity(btVector3(0,-10,0));
///create a few basic rigid bodies
btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
box->initializePolyhedralFeatures();
btCollisionShape* groundShape = box;
int i;
// btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
m_collisionShapes.push_back(groundShape);
m_collisionShapes.push_back(new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)));
btTransform tr;
tr.setIdentity();
btTransform groundTransform;
groundTransform.setIdentity();
//groundTransform.setOrigin(btVector3(5,5,5));
for (i=0;i<gNumObjects;i++)
//We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
{
if (i>0)
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);
}
{
//create a few dynamic rigidbodies
// Re-using the same collision is better for memory usage and performance
btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
m_collisionShapes.push_back(colShape);
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//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)
colShape->calculateLocalInertia(mass,localInertia);
int gNumObjects = 120;
int i;
for (i=0;i<gNumObjects;i++)
{
shapeIndex[i] = 1;//sphere
}
else
shapeIndex[i] = 0;
}
btCollisionShape* shape = m_collisionShapes[1];
btTransform trans;
trans.setIdentity();
if (useCompound)
{
btCompoundShape* compoundShape = new btCompoundShape();
btCollisionShape* oldShape = m_collisionShapes[1];
m_collisionShapes[1] = compoundShape;
btVector3 sphereOffset(0,0,2);
comOffset.setIdentity();
#ifdef CENTER_OF_MASS_SHIFT
comOffset.setOrigin(comOffsetVec);
compoundShape->addChildShape(comOffset,oldShape);
#else
compoundShape->addChildShape(tr,oldShape);
tr.setOrigin(sphereOffset);
compoundShape->addChildShape(tr,new btSphereShape(0.9));
#endif
}
#ifdef DO_WALL
for (i=0;i<gNumObjects;i++)
{
btCollisionShape* shape = m_collisionShapes[shapeIndex[i]];
shape->setMargin(gCollisionMargin);
bool isDyna = i>0;
btTransform trans;
trans.setIdentity();
if (i>0)
{
//stack them
int colsize = 10;
int row = (i*CUBE_HALF_EXTENTS*2)/(colsize*2*CUBE_HALF_EXTENTS);
@@ -538,83 +279,91 @@ int maxNumOutstandingTasks = 4;
row*2*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,0);
trans.setOrigin(pos);
} else
{
trans.setOrigin(btVector3(0,EXTRA_HEIGHT-CUBE_HALF_EXTENTS,0));
float mass = 1.f;
btRigidBody* body = localCreateRigidBody(mass,trans,shape);
///when using m_ccdMode
if (m_ccdMode==USE_CCD)
{
body->setCcdMotionThreshold(1e-7);
body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
}
}
float mass = 1.f;
if (!isDyna)
mass = 0.f;
btRigidBody* body = localCreateRigidBody(mass,trans,shape);
#ifdef USE_KINEMATIC_GROUND
if (mass == 0.f)
{
body->setCollisionFlags( body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
body->setActivationState(DISABLE_DEACTIVATION);
}
#endif //USE_KINEMATIC_GROUND
// Only do CCD if motion in one timestep (1.f/60.f) exceeds CUBE_HALF_EXTENTS
body->setCcdMotionThreshold( CUBE_HALF_EXTENTS );
//Experimental: better estimation of CCD Time of Impact:
body->setCcdSweptSphereRadius( 0.2*CUBE_HALF_EXTENTS );
#ifdef USER_DEFINED_FRICTION_MODEL
///Advanced use: override the friction solver
body->m_frictionSolverType = USER_CONTACT_SOLVER_TYPE1;
#endif //USER_DEFINED_FRICTION_MODEL
}
#endif
#ifdef DO_BENCHMARK_PYRAMIDS
btTransform trans;
trans.setIdentity();
btScalar halfExtents = CUBE_HALF_EXTENTS;
trans.setOrigin(btVector3(0,-halfExtents,0));
localCreateRigidBody(0.f,trans,m_collisionShapes[shapeIndex[0]]);
int numWalls = 15;
int wallHeight = 15;
float wallDistance = 3;
for (int i=0;i<numWalls;i++)
{
float zPos = (i-numWalls/2) * wallDistance;
createStack(m_collisionShapes[shapeIndex[1]],halfExtents,wallHeight,zPos);
}
// createStack(m_collisionShapes[shapeIndex[1]],halfExtends,20,10);
// createStack(m_collisionShapes[shapeIndex[1]],halfExtends,20,20);
#define DESTROYER_BALL 1
#ifdef DESTROYER_BALL
btTransform sphereTrans;
sphereTrans.setIdentity();
sphereTrans.setOrigin(btVector3(0,2,40));
btSphereShape* ball = new btSphereShape(2.f);
m_collisionShapes.push_back(ball);
btRigidBody* ballBody = localCreateRigidBody(10000.f,sphereTrans,ball);
ballBody->setLinearVelocity(btVector3(0,0,-10));
#endif
#endif //DO_BENCHMARK_PYRAMIDS
// clientResetScene();
}
void CcdPhysicsDemo::clientResetScene()
{
exitPhysics();
initPhysics();
}
void CcdPhysicsDemo::keyboardCallback(unsigned char key, int x, int y)
{
if (key=='p')
{
switch (m_ccdMode)
{
case USE_CCD:
{
m_ccdMode = USE_NO_CCD;
break;
}
case USE_NO_CCD:
default:
{
m_ccdMode = USE_CCD;
}
};
clientResetScene();
} else
{
DemoApplication::keyboardCallback(key,x,y);
}
}
void CcdPhysicsDemo::shootBox(const btVector3& destination)
{
if (m_dynamicsWorld)
{
float mass = 1.f;
btTransform startTransform;
startTransform.setIdentity();
btVector3 camPos = getCameraPosition();
startTransform.setOrigin(camPos);
setShootBoxShape ();
btRigidBody* body = this->localCreateRigidBody(mass, startTransform,m_shootBoxShape);
body->setLinearFactor(btVector3(1,1,1));
//body->setRestitution(1);
btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]);
linVel.normalize();
linVel*=m_ShootBoxInitialSpeed;
body->getWorldTransform().setOrigin(camPos);
body->getWorldTransform().setRotation(btQuaternion(0,0,0,1));
body->setLinearVelocity(linVel);
body->setAngularVelocity(btVector3(0,0,0));
body->setContactProcessingThreshold(1e30);
///when using m_ccdMode, disable regular CCD
if (m_ccdMode==USE_CCD)
{
body->setCcdMotionThreshold(0.0001);
body->setCcdSweptSphereRadius(0.4f);
}
}
}
@@ -622,7 +371,6 @@ int maxNumOutstandingTasks = 4;
void CcdPhysicsDemo::exitPhysics()
{
//cleanup in the reverse order of creation/initialization
//remove the rigidbodies from the dynamics world and delete them
@@ -643,42 +391,23 @@ void CcdPhysicsDemo::exitPhysics()
for (int j=0;j<m_collisionShapes.size();j++)
{
btCollisionShape* shape = m_collisionShapes[j];
m_collisionShapes[j] = 0;
delete shape;
}
m_collisionShapes.clear();
//delete dynamics world
delete m_dynamicsWorld;
//delete solver
delete m_solver;
#ifdef USE_PARALLEL_DISPATCHER
#ifdef _WIN32
if (m_threadSupportSolver)
{
delete m_threadSupportSolver;
}
#endif
#endif
//delete broadphase
delete m_broadphase;
//delete dispatcher
delete m_dispatcher;
#ifdef USE_PARALLEL_DISPATCHER
#ifdef _WIN32
if (m_threadSupportCollision)
{
delete m_threadSupportCollision;
}
#endif
#endif
delete m_collisionConfiguration;
}