2006-05-25 19:18:29 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
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,
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
/// CollisionInterfaceDemo shows high level usage of the Collision Detection.
|
|
|
|
///
|
|
|
|
|
|
|
|
#include "GL_Simplex1to4.h"
|
2006-09-25 08:58:57 +00:00
|
|
|
|
|
|
|
//include common Bullet Collision Detection headerfiles
|
|
|
|
#include "btBulletCollisionCommon.h"
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
#include "LinearMath/btIDebugDraw.h"
|
2006-05-25 19:18:29 +00:00
|
|
|
#include "GL_ShapeDrawer.h"
|
2006-09-11 05:31:22 +00:00
|
|
|
#include "CollisionInterfaceDemo.h"
|
2006-05-25 19:18:29 +00:00
|
|
|
#include "GlutStuff.h"
|
2006-11-14 21:53:59 +00:00
|
|
|
#include "GLDebugDrawer.h"
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
float yaw=0.f,pitch=0.f,roll=0.f;
|
|
|
|
const int maxNumObjects = 4;
|
|
|
|
const int numObjects = 2;
|
|
|
|
|
|
|
|
GL_Simplex1to4 simplex;
|
|
|
|
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btCollisionObject objects[maxNumObjects];
|
|
|
|
btCollisionWorld* collisionWorld = 0;
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-23 14:51:54 +00:00
|
|
|
int screenWidth = 640;
|
|
|
|
int screenHeight = 480;
|
2006-11-14 21:53:59 +00:00
|
|
|
GLDebugDrawer debugDrawer;
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
int main(int argc,char** argv)
|
|
|
|
{
|
2006-09-11 05:31:22 +00:00
|
|
|
CollisionInterfaceDemo* collisionInterfaceDemo = new CollisionInterfaceDemo();
|
|
|
|
|
|
|
|
collisionInterfaceDemo->initPhysics();
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
collisionInterfaceDemo->clientResetScene();
|
|
|
|
|
|
|
|
return glutmain(argc, argv,screenWidth,screenHeight,"Collision Interface Demo",collisionInterfaceDemo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionInterfaceDemo::initPhysics()
|
|
|
|
{
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
m_debugMode |= btIDebugDraw::DBG_DrawWireframe;
|
2006-09-11 05:31:22 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btMatrix3x3 basisA;
|
2006-05-25 19:18:29 +00:00
|
|
|
basisA.setIdentity();
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btMatrix3x3 basisB;
|
2006-05-25 19:18:29 +00:00
|
|
|
basisB.setIdentity();
|
|
|
|
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[0].getWorldTransform().setBasis(basisA);
|
|
|
|
objects[1].getWorldTransform().setBasis(basisB);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
//btPoint3 points0[3]={btPoint3(1,0,0),btPoint3(0,1,0),btPoint3(0,0,1)};
|
|
|
|
//btPoint3 points1[5]={btPoint3(1,0,0),btPoint3(0,1,0),btPoint3(0,0,1),btPoint3(0,0,-1),btPoint3(-1,-1,0)};
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btBoxShape* boxA = new btBoxShape(btVector3(1,1,1));
|
|
|
|
btBoxShape* boxB = new btBoxShape(btVector3(0.5,0.5,0.5));
|
2006-05-25 19:18:29 +00:00
|
|
|
//ConvexHullShape hullA(points0,3);
|
2006-09-27 20:43:51 +00:00
|
|
|
//hullA.setLocalScaling(btVector3(3,3,3));
|
2006-05-25 19:18:29 +00:00
|
|
|
//ConvexHullShape hullB(points1,4);
|
2006-09-27 20:43:51 +00:00
|
|
|
//hullB.setLocalScaling(btVector3(4,4,4));
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[0].setCollisionShape(boxA);//&hullA;
|
|
|
|
objects[1].setCollisionShape(boxB);//&hullB;
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btCollisionDispatcher* dispatcher = new btCollisionDispatcher;
|
|
|
|
btVector3 worldAabbMin(-1000,-1000,-1000);
|
|
|
|
btVector3 worldAabbMax(1000,1000,1000);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax);
|
2006-09-11 05:31:22 +00:00
|
|
|
|
|
|
|
//SimpleBroadphase is a brute force alternative, performing N^2 aabb overlap tests
|
2006-09-27 20:43:51 +00:00
|
|
|
//SimpleBroadphase* broadphase = new btSimpleBroadphase;
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
collisionWorld = new btCollisionWorld(dispatcher,broadphase);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-28 01:11:16 +00:00
|
|
|
collisionWorld->addCollisionObject(&objects[0]);
|
|
|
|
collisionWorld->addCollisionObject(&objects[1]);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
|
2006-05-25 19:18:29 +00:00
|
|
|
//to be implemented by the demo
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
void CollisionInterfaceDemo::clientMoveAndDisplay()
|
2006-05-25 19:18:29 +00:00
|
|
|
{
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
displayCallback();
|
2006-05-25 19:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
static btVoronoiSimplexSolver sGjkSimplexSolver;
|
|
|
|
btSimplexSolverInterface& gGjkSimplexSolver = sGjkSimplexSolver;
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
void CollisionInterfaceDemo::displayCallback(void) {
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
|
2006-11-14 21:53:59 +00:00
|
|
|
|
|
|
|
btDispatcherInfo dispatchInfo;
|
|
|
|
dispatchInfo.m_debugDraw = &debugDrawer;
|
|
|
|
|
2006-05-25 19:18:29 +00:00
|
|
|
if (collisionWorld)
|
2006-11-14 21:53:59 +00:00
|
|
|
collisionWorld->performDiscreteCollisionDetection(dispatchInfo);
|
2006-07-01 00:22:15 +00:00
|
|
|
|
2006-05-25 19:18:29 +00:00
|
|
|
int i;
|
2006-09-11 05:31:22 +00:00
|
|
|
|
2006-07-01 00:22:15 +00:00
|
|
|
///one way to draw all the contact points is iterating over contact manifolds / points:
|
|
|
|
|
2006-09-28 01:11:16 +00:00
|
|
|
int numManifolds = collisionWorld->getDispatcher()->getNumManifolds();
|
2006-05-25 19:18:29 +00:00
|
|
|
for (i=0;i<numManifolds;i++)
|
|
|
|
{
|
2006-09-28 01:11:16 +00:00
|
|
|
btPersistentManifold* contactManifold = collisionWorld->getDispatcher()->getManifoldByIndexInternal(i);
|
|
|
|
btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
|
|
|
|
btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
|
2006-11-02 03:42:53 +00:00
|
|
|
contactManifold->refreshContactPoints(obA->getWorldTransform(),obB->getWorldTransform());
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-28 01:11:16 +00:00
|
|
|
int numContacts = contactManifold->getNumContacts();
|
2006-05-25 19:18:29 +00:00
|
|
|
for (int j=0;j<numContacts;j++)
|
|
|
|
{
|
2006-09-28 01:11:16 +00:00
|
|
|
btManifoldPoint& pt = contactManifold->getContactPoint(j);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
glColor3f(1, 0, 1);
|
|
|
|
|
2006-09-28 01:11:16 +00:00
|
|
|
btVector3 ptA = pt.getPositionWorldOnA();
|
|
|
|
btVector3 ptB = pt.getPositionWorldOnB();
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
glVertex3d(ptA.x(),ptA.y(),ptA.z());
|
|
|
|
glVertex3d(ptB.x(),ptB.y(),ptB.z());
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
//you can un-comment out this line, and then all points are removed
|
2006-09-28 01:11:16 +00:00
|
|
|
//contactManifold->clearManifold();
|
2006-09-11 05:31:22 +00:00
|
|
|
}
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-09-28 01:11:16 +00:00
|
|
|
//GL_ShapeDrawer::drawCoordSystem();
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
float m[16];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0;i<numObjects;i++)
|
|
|
|
{
|
|
|
|
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[i].getWorldTransform().getOpenGLMatrix( m );
|
|
|
|
GL_ShapeDrawer::drawOpenGL(m,objects[i].getCollisionShape(),btVector3(1,1,1),getDebugMode());
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-27 20:43:51 +00:00
|
|
|
btQuaternion orn;
|
2006-05-25 19:18:29 +00:00
|
|
|
orn.setEuler(yaw,pitch,roll);
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[1].getWorldTransform().setOrigin(objects[1].getWorldTransform().getOrigin()+btVector3(0,-0.01,0));
|
2006-05-25 19:18:29 +00:00
|
|
|
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[0].getWorldTransform().setRotation(orn);
|
2006-05-25 19:18:29 +00:00
|
|
|
|
|
|
|
pitch += 0.005f;
|
|
|
|
yaw += 0.01f;
|
|
|
|
|
|
|
|
glFlush();
|
|
|
|
glutSwapBuffers();
|
|
|
|
}
|
|
|
|
|
2006-09-11 05:31:22 +00:00
|
|
|
void CollisionInterfaceDemo::clientResetScene()
|
2006-05-25 19:18:29 +00:00
|
|
|
{
|
2006-11-02 03:42:53 +00:00
|
|
|
objects[0].getWorldTransform().setOrigin(btVector3(0.0f,3.f,0.f));
|
|
|
|
objects[1].getWorldTransform().setOrigin(btVector3(0.0f,9.f,0.f));
|
2006-05-25 19:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|