2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #include "btBulletDynamicsCommon.h"
17 #include "LinearMath/btIDebugDraw.h"
18 #include "GLDebugDrawer.h"
19 #include "UserCollisionAlgorithm.h"
20 #include "GL_ShapeDrawer.h"
21 #include "GlutStuff.h"
23 //The user defined collision algorithm
24 #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
26 GLDebugDrawer debugDrawer;
28 static const int NUM_VERTICES = 5;
29 static const int NUM_TRIANGLES=4;
31 btVector3 gVertices[NUM_VERTICES];
32 int gIndices[NUM_TRIANGLES*3];
33 const float TRIANGLE_SIZE=10.f;
36 ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;
37 inline btScalar calculateCombinedFriction(float friction0,float friction1)
39 btScalar friction = friction0 * friction1;
41 const btScalar MAX_FRICTION = 10.f;
42 if (friction < -MAX_FRICTION)
43 friction = -MAX_FRICTION;
44 if (friction > MAX_FRICTION)
45 friction = MAX_FRICTION;
50 inline btScalar calculateCombinedRestitution(float restitution0,float restitution1)
52 return restitution0 * restitution1;
60 int main(int argc,char** argv)
63 UserCollisionAlgorithm* userCollisionAlgorithm = new UserCollisionAlgorithm;
64 userCollisionAlgorithm->initPhysics();
65 userCollisionAlgorithm->setCameraDistance(30.f);
67 return glutmain(argc, argv,640,480,"Static Concave Mesh Demo",userCollisionAlgorithm);
70 void UserCollisionAlgorithm::initPhysics()
75 const int NUM_VERTS_X = 50;
76 const int NUM_VERTS_Y = 50;
77 const int totalVerts = NUM_VERTS_X*NUM_VERTS_Y;
79 btVector3* gVertices = new btVector3[totalVerts];
82 for ( i=0;i<NUM_VERTS_X;i++)
84 for (int j=0;j<NUM_VERTS_Y;j++)
86 gVertices[i+j*NUM_VERTS_X].setValue((i-NUM_VERTS_X*0.5f)*TRIANGLE_SIZE,2.f*sinf((float)i)*cosf((float)j),(j-NUM_VERTS_Y*0.5f)*TRIANGLE_SIZE);
90 btTriangleMesh* trimesh = new btTriangleMesh();
92 for ( i=0;i<NUM_VERTS_X-1;i++)
94 for (int j=0;j<NUM_VERTS_Y-1;j++)
96 trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[j*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i+1]);
97 trimesh->addTriangle(gVertices[j*NUM_VERTS_X+i],gVertices[(j+1)*NUM_VERTS_X+i+1],gVertices[(j+1)*NUM_VERTS_X+i]);
103 bool useQuantizedBvhTree = true;
104 btCollisionShape* trimeshShape = new btBvhTriangleMeshShape(trimesh,useQuantizedBvhTree);
106 //ConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
107 btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
108 btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
110 btVector3 maxAabb(10000,10000,10000);
111 btBroadphaseInterface* broadphase = new btAxisSweep3(-maxAabb,maxAabb);//SimpleBroadphase();
112 dispatcher->registerCollisionCreateFunc(GIMPACT_SHAPE_PROXYTYPE,GIMPACT_SHAPE_PROXYTYPE,new btSphereSphereCollisionAlgorithm::CreateFunc);
114 btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver();
115 m_dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,constraintSolver,collisionConfiguration);
118 btTransform startTransform;
119 startTransform.setIdentity();
120 startTransform.setOrigin(btVector3(0,-2,0));
122 btRigidBody* staticBody= localCreateRigidBody(mass, startTransform,trimeshShape);
123 //enable custom material callback
124 staticBody->setCollisionFlags(staticBody->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
127 for (int i=0;i<10;i++)
129 btCollisionShape* sphereShape = new btSphereShape(1);
130 startTransform.setOrigin(btVector3(1,2*i,1));
131 //btRigidBody* body = localCreateRigidBody(1, startTransform,sphereShape);
132 localCreateRigidBody(1, startTransform,sphereShape);
138 m_dynamicsWorld->setDebugDrawer(&debugDrawer);
141 void UserCollisionAlgorithm::clientMoveAndDisplay()
143 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145 float dt = getDeltaTimeMicroseconds() * 0.000001f;
148 m_dynamicsWorld->stepSimulation(dt);
150 //optional but useful: debug drawing
151 m_dynamicsWorld->debugDrawWorld();
165 void UserCollisionAlgorithm::displayCallback(void) {
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);