fix in box-box contact generation: choose better contact point location (along contact normal) when objects are swapped.

move debugDrawWorld to btCollisionWorld.
improved CollisionInterfaceDemo, show how to perform a closest point query for objects that are not in the collision world.
removed a bit of garbage from the debug drawer
This commit is contained in:
erwin.coumans
2009-12-18 00:54:52 +00:00
parent 6425af9a6c
commit fcd2b93a22
7 changed files with 619 additions and 501 deletions

View File

@@ -17,6 +17,7 @@ subject to the following restrictions:
///
/// CollisionInterfaceDemo shows high level usage of the Collision Detection.
///
#define TEST_NOT_ADDING_OBJECTS_TO_WORLD
#include "GL_Simplex1to4.h"
@@ -57,7 +58,7 @@ int main(int argc,char** argv)
void CollisionInterfaceDemo::initPhysics()
{
//m_debugMode |= btIDebugDraw::DBG_DrawWireframe;
m_debugMode |= btIDebugDraw::DBG_DrawWireframe;
btMatrix3x3 basisA;
basisA.setIdentity();
@@ -69,7 +70,10 @@ void CollisionInterfaceDemo::initPhysics()
objects[1].getWorldTransform().setBasis(basisB);
btBoxShape* boxA = new btBoxShape(btVector3(1,1,1));
boxA->setMargin(0.f);
btBoxShape* boxB = new btBoxShape(btVector3(0.5,0.5,0.5));
boxB->setMargin(0.f);
//ConvexHullShape hullA(points0,3);
//hullA.setLocalScaling(btVector3(3,3,3));
//ConvexHullShape hullB(points1,4);
@@ -89,9 +93,13 @@ void CollisionInterfaceDemo::initPhysics()
//SimpleBroadphase* broadphase = new btSimpleBroadphase;
collisionWorld = new btCollisionWorld(dispatcher,broadphase,collisionConfiguration);
collisionWorld->setDebugDrawer(&debugDrawer);
#ifdef TEST_NOT_ADDING_OBJECTS_TO_WORLD
collisionWorld->addCollisionObject(&objects[0]);
collisionWorld->addCollisionObject(&objects[1]);
#endif //TEST_NOT_ADDING_OBJECTS_TO_WORLD
}
@@ -115,14 +123,33 @@ void CollisionInterfaceDemo::displayCallback(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);
btScalar m[16];
btVector3 worldBoundsMin,worldBoundsMax;
collisionWorld->getBroadphase()->getBroadphaseAabb(worldBoundsMin,worldBoundsMax);
int i;
for (i=0;i<numObjects;i++)
{
objects[i].getWorldTransform().getOpenGLMatrix( m );
m_shapeDrawer->drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode(),worldBoundsMin,worldBoundsMax);
}
collisionWorld->getDispatchInfo().m_debugDraw = &debugDrawer;
if (collisionWorld)
collisionWorld->performDiscreteCollisionDetection();
int i;
///one way to draw all the contact points is iterating over contact manifolds / points:
#ifndef TEST_NOT_ADDING_OBJECTS_TO_WORLD
collisionWorld->debugDrawWorld();
///one way to draw all the contact points is iterating over contact manifolds in the dispatcher:
int numManifolds = collisionWorld->getDispatcher()->getNumManifolds();
for (i=0;i<numManifolds;i++)
@@ -137,7 +164,7 @@ void CollisionInterfaceDemo::displayCallback(void) {
btManifoldPoint& pt = contactManifold->getContactPoint(j);
glBegin(GL_LINES);
glColor3f(1, 0, 1);
glColor3f(0, 0, 0);
btVector3 ptA = pt.getPositionWorldOnA();
btVector3 ptB = pt.getPositionWorldOnB();
@@ -150,33 +177,88 @@ void CollisionInterfaceDemo::displayCallback(void) {
//you can un-comment out this line, and then all points are removed
//contactManifold->clearManifold();
}
#else
glDisable(GL_TEXTURE_2D);
for (i=0;i<numObjects;i++)
{
collisionWorld->debugDrawObject(objects[i].getWorldTransform(),objects[i].getCollisionShape(), btVector3(1,1,0));
}
//another way is to directly query the dispatcher for both objects. The objects don't need to be inserted into the world
btCollisionAlgorithm* algo = collisionWorld->getDispatcher()->findAlgorithm(&objects[0],&objects[1]);
btManifoldResult contactPointResult(&objects[0],&objects[1]);
algo->processCollision(&objects[0],&objects[1],collisionWorld->getDispatchInfo(),&contactPointResult);
btManifoldArray manifoldArray;
algo->getAllContactManifolds(manifoldArray);
int numManifolds = manifoldArray.size();
for (i=0;i<numManifolds;i++)
{
btPersistentManifold* contactManifold = manifoldArray[i];
btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
glDisable(GL_DEPTH_TEST);
int numContacts = contactManifold->getNumContacts();
bool swap = obA == &objects[0];
for (int j=0;j<numContacts;j++)
{
btManifoldPoint& pt = contactManifold->getContactPoint(j);
glBegin(GL_LINES);
glColor3f(0, 0, 0);
btVector3 ptA = swap ?pt.getPositionWorldOnA():pt.getPositionWorldOnB();
btVector3 ptB = swap ? pt.getPositionWorldOnB():pt.getPositionWorldOnA();
glVertex3d(ptA.x(),ptA.y(),ptA.z());
glVertex3d(ptB.x(),ptB.y(),ptB.z());
glEnd();
}
//you can un-comment out this line, and then all points are removed
//contactManifold->clearManifold();
}
#endif
//GL_ShapeDrawer::drawCoordSystem();
btScalar m[16];
btVector3 worldBoundsMin,worldBoundsMax;
collisionWorld->getBroadphase()->getBroadphaseAabb(worldBoundsMin,worldBoundsMax);
btQuaternion qA = objects[0].getWorldTransform().getRotation();
btQuaternion qB = objects[1].getWorldTransform().getRotation();
for (i=0;i<numObjects;i++)
if (!m_idle)
{
objects[i].getWorldTransform().getOpenGLMatrix( m );
m_shapeDrawer->drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode(),worldBoundsMin,worldBoundsMax);
btScalar timeInSeconds = getDeltaTimeMicroseconds()/1000.f;
btQuaternion orn;
objects[0].getWorldTransform().getBasis().getEulerYPR(yaw,pitch,roll);
pitch += 0.00005f*timeInSeconds;
yaw += 0.0001f*timeInSeconds;
objects[0].getWorldTransform().getBasis().setEulerYPR(yaw,pitch,roll);
orn.setEuler(yaw,pitch,roll);
objects[1].getWorldTransform().setOrigin(objects[1].getWorldTransform().getOrigin()+btVector3(0,-0.00001*timeInSeconds,0));
//objects[0].getWorldTransform().setRotation(orn);
}
btQuaternion orn;
orn.setEuler(yaw,pitch,roll);
objects[1].getWorldTransform().setOrigin(objects[1].getWorldTransform().getOrigin()+btVector3(0,-0.01,0));
objects[0].getWorldTransform().setRotation(orn);
pitch += 0.005f;
yaw += 0.01f;
glFlush();
glutSwapBuffers();
}
@@ -184,7 +266,14 @@ void CollisionInterfaceDemo::displayCallback(void) {
void CollisionInterfaceDemo::clientResetScene()
{
objects[0].getWorldTransform().setOrigin(btVector3(0.0f,3.f,0.f));
objects[1].getWorldTransform().setOrigin(btVector3(0.0f,9.f,0.f));
btQuaternion rotA(0.739,-0.204,0.587,0.257);
rotA.normalize();
objects[0].getWorldTransform().setRotation(rotA);
objects[1].getWorldTransform().setOrigin(btVector3(0.0f,4.248f,0.f));
}

View File

@@ -601,17 +601,25 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
7,2,3,
7,6,2};
static btVector3 vertices[8]={ btVector3(1,1,1),btVector3(-1,1,1), btVector3(1,-1,1), btVector3(-1,-1,1), btVector3(1,1,-1), btVector3(-1,1,-1), btVector3(1,-1,-1), btVector3(-1,-1,-1)};
btVector3 vertices[8]={
btVector3(halfExtent[0],halfExtent[1],halfExtent[2]),
btVector3(-halfExtent[0],halfExtent[1],halfExtent[2]),
btVector3(halfExtent[0],-halfExtent[1],halfExtent[2]),
btVector3(-halfExtent[0],-halfExtent[1],halfExtent[2]),
btVector3(halfExtent[0],halfExtent[1],-halfExtent[2]),
btVector3(-halfExtent[0],halfExtent[1],-halfExtent[2]),
btVector3(halfExtent[0],-halfExtent[1],-halfExtent[2]),
btVector3(-halfExtent[0],-halfExtent[1],-halfExtent[2])};
#if 1
glBegin (GL_TRIANGLES);
int si=36;
for (int i=0;i<si;i+=3)
{
btVector3 v1 = vertices[indices[i]]*halfExtent;
btVector3 v2 = vertices[indices[i+1]]*halfExtent;
btVector3 v3 = vertices[indices[i+2]]*halfExtent;
const btVector3& v1 = vertices[indices[i]];;
const btVector3& v2 = vertices[indices[i+1]];
const btVector3& v3 = vertices[indices[i+2]];
btVector3 normal = (v3-v1).cross(v2-v1);
normal.normalize ();
glNormal3f(normal.getX(),normal.getY(),normal.getZ());
glVertex3f (v1.x(), v1.y(), v1.z());
glVertex3f (v2.x(), v2.y(), v2.z());
@@ -619,6 +627,7 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
}
glEnd();
#endif
useWireframeFallback = false;
break;
@@ -812,8 +821,6 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
btPolyhedralConvexShape* polyshape = (btPolyhedralConvexShape*) shape;
{
glRasterPos3f(0.0, 0.0, 0.0);
//btDrawString(BMF_GetFont(BMF_kHelvetica10),polyshape->getExtraDebugInfo());
glColor3f(1.f, 1.f, 1.f);
int i;
@@ -821,7 +828,6 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
{
btVector3 vtx;
polyshape->getVertex(i,vtx);
glRasterPos3f(vtx.x(), vtx.y(), vtx.z());
char buf[12];
sprintf(buf," %d",i);
//btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
@@ -834,7 +840,6 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
polyshape->getPlane(normal,vtx,i);
btScalar d = vtx.dot(normal);
glRasterPos3f(normal.x()*d, normal.y()*d, normal.z()*d);
char buf[12];
sprintf(buf," plane %d",i);
//btDrawString(BMF_GetFont(BMF_kHelvetica10),buf);
@@ -878,21 +883,6 @@ void GL_ShapeDrawer::drawOpenGL(btScalar* m, const btCollisionShape* shape, cons
glDisable(GL_DEPTH_TEST);
glRasterPos3f(0,0,0);//mvtx.x(), vtx.y(), vtx.z());
if (debugMode&btIDebugDraw::DBG_DrawText)
{
GLDebugDrawString(0,0,shape->getName());
}
if (debugMode& btIDebugDraw::DBG_DrawFeaturesText)
{
//btDrawString(BMF_GetFont(BMF_kHelvetica10),shape->getExtraDebugInfo());
}
glEnable(GL_DEPTH_TEST);
// glPopMatrix();
if(m_textureenabled) glDisable(GL_TEXTURE_2D);
}
glPopMatrix();