Back to index

supertuxkart  0.5+dfsg1
Bullet-C-API.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 /*
00017        Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
00018        Work in progress, functionality will be added on demand.
00019 
00020        If possible, use the richer Bullet C++ API, by including <src/btBulletDynamicsCommon.h>
00021 */
00022 
00023 #include "Bullet-C-Api.h"
00024 #include "btBulletDynamicsCommon.h"
00025 #include "LinearMath/btAlignedAllocator.h"
00026 
00027 /*
00028        Create and Delete a Physics SDK    
00029 */
00030 
00031 struct btPhysicsSdk
00032 {
00033 
00034 //     btDispatcher*                      m_dispatcher;
00035 //     btOverlappingPairCache*            m_pairCache;
00036 //     btConstraintSolver*                m_constraintSolver
00037 
00038        btVector3     m_worldAabbMin;
00039        btVector3     m_worldAabbMax;
00040 
00041 
00042        //todo: version, hardware/optimization settings etc?
00043        btPhysicsSdk()
00044               :m_worldAabbMin(-1000,-1000,-1000),
00045               m_worldAabbMax(1000,1000,1000)
00046        {
00047 
00048        }
00049 
00050        
00051 };
00052 
00053 plPhysicsSdkHandle   plNewBulletSdk()
00054 {
00055        void* mem = btAlignedAlloc(sizeof(btPhysicsSdk),16);
00056        return (plPhysicsSdkHandle)new (mem)btPhysicsSdk;
00057 }
00058 
00059 void          plDeletePhysicsSdk(plPhysicsSdkHandle     physicsSdk)
00060 {
00061        btPhysicsSdk* phys = reinterpret_cast<btPhysicsSdk*>(physicsSdk);
00062        btAlignedFree(phys); 
00063 }
00064 
00065 
00066 /* Dynamics World */
00067 plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle)
00068 {
00069        btPhysicsSdk* physicsSdk = reinterpret_cast<btPhysicsSdk*>(physicsSdkHandle);
00070        void* mem = btAlignedAlloc(sizeof(btDefaultCollisionConfiguration),16);
00071        btDefaultCollisionConfiguration* collisionConfiguration = new (mem)btDefaultCollisionConfiguration();
00072        mem = btAlignedAlloc(sizeof(btCollisionDispatcher),16);
00073        btDispatcher*                      dispatcher = new (mem)btCollisionDispatcher(collisionConfiguration);
00074        mem = btAlignedAlloc(sizeof(btAxisSweep3),16);
00075        btBroadphaseInterface*             pairCache = new (mem)btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax);
00076        mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16);
00077        btConstraintSolver*                constraintSolver = new(mem) btSequentialImpulseConstraintSolver();
00078 
00079        mem = btAlignedAlloc(sizeof(btDiscreteDynamicsWorld),16);
00080        return (plDynamicsWorldHandle) new (mem)btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration);
00081 }
00082 void           plDeleteDynamicsWorld(plDynamicsWorldHandle world)
00083 {
00084        //todo: also clean up the other allocations, axisSweep, pairCache,dispatcher,constraintSolver,collisionConfiguration
00085        btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
00086        btAlignedFree(dynamicsWorld);
00087 }
00088 
00089 void   plStepSimulation(plDynamicsWorldHandle world,    plReal timeStep)
00090 {
00091        btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
00092        assert(dynamicsWorld);
00093        dynamicsWorld->stepSimulation(timeStep);
00094 }
00095 
00096 void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
00097 {
00098        btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
00099        assert(dynamicsWorld);
00100        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00101        assert(body);
00102 
00103        dynamicsWorld->addRigidBody(body);
00104 }
00105 
00106 void plRemoveRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
00107 {
00108        btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
00109        assert(dynamicsWorld);
00110        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00111        assert(body);
00112 
00113        dynamicsWorld->removeRigidBody(body);
00114 }
00115 
00116 /* Rigid Body  */
00117 
00118 plRigidBodyHandle plCreateRigidBody(      void* user_data,  float mass, plCollisionShapeHandle cshape )
00119 {
00120        btTransform trans;
00121        trans.setIdentity();
00122        btVector3 localInertia(0,0,0);
00123        btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
00124        assert(shape);
00125        if (mass)
00126        {
00127               shape->calculateLocalInertia(mass,localInertia);
00128        }
00129        void* mem = btAlignedAlloc(sizeof(btRigidBody),16);
00130        btRigidBody::btRigidBodyConstructionInfo rbci(mass, 0,shape,localInertia);
00131        btRigidBody* body = new (mem)btRigidBody(rbci);
00132        body->setWorldTransform(trans);
00133        body->setUserPointer(user_data);
00134        return (plRigidBodyHandle) body;
00135 }
00136 
00137 void plDeleteRigidBody(plRigidBodyHandle cbody)
00138 {
00139        btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
00140        assert(body);
00141        btAlignedFree( body);
00142 }
00143 
00144 
00145 /* Collision Shape definition */
00146 
00147 plCollisionShapeHandle plNewSphereShape(plReal radius)
00148 {
00149        void* mem = btAlignedAlloc(sizeof(btSphereShape),16);
00150        return (plCollisionShapeHandle) new (mem)btSphereShape(radius);
00151        
00152 }
00153        
00154 plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z)
00155 {
00156        void* mem = btAlignedAlloc(sizeof(btBoxShape),16);
00157        return (plCollisionShapeHandle) new (mem)btBoxShape(btVector3(x,y,z));
00158 }
00159 
00160 plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
00161 {
00162        //capsule is convex hull of 2 spheres, so use btMultiSphereShape
00163        btVector3 inertiaHalfExtents(radius,height,radius);
00164        const int numSpheres = 2;
00165        btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
00166        btScalar radi[numSpheres] = {radius,radius};
00167        void* mem = btAlignedAlloc(sizeof(btMultiSphereShape),16);
00168        return (plCollisionShapeHandle) new (mem)btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres);
00169 }
00170 plCollisionShapeHandle plNewConeShape(plReal radius, plReal height)
00171 {
00172        void* mem = btAlignedAlloc(sizeof(btConeShape),16);
00173        return (plCollisionShapeHandle) new (mem)btConeShape(radius,height);
00174 }
00175 
00176 plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height)
00177 {
00178        void* mem = btAlignedAlloc(sizeof(btCylinderShape),16);
00179        return (plCollisionShapeHandle) new (mem)btCylinderShape(btVector3(radius,height,radius));
00180 }
00181 
00182 /* Convex Meshes */
00183 plCollisionShapeHandle plNewConvexHullShape()
00184 {
00185        void* mem = btAlignedAlloc(sizeof(btConvexHullShape),16);
00186        return (plCollisionShapeHandle) new (mem)btConvexHullShape();
00187 }
00188 
00189 
00190 /* Concave static triangle meshes */
00191 plMeshInterfaceHandle                 plNewMeshInterface()
00192 {
00193        return 0;
00194 }
00195 
00196 plCollisionShapeHandle plNewCompoundShape()
00197 {
00198        void* mem = btAlignedAlloc(sizeof(btCompoundShape),16);
00199        return (plCollisionShapeHandle) new (mem)btCompoundShape();
00200 }
00201 
00202 void   plAddChildShape(plCollisionShapeHandle compoundShapeHandle,plCollisionShapeHandle childShapeHandle, plVector3 childPos,plQuaternion childOrn)
00203 {
00204        btCollisionShape* colShape = reinterpret_cast<btCollisionShape*>(compoundShapeHandle);
00205        btAssert(colShape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE);
00206        btCompoundShape* compoundShape = reinterpret_cast<btCompoundShape*>(colShape);
00207        btCollisionShape* childShape = reinterpret_cast<btCollisionShape*>(childShapeHandle);
00208        btTransform   localTrans;
00209        localTrans.setIdentity();
00210        localTrans.setOrigin(btVector3(childPos[0],childPos[1],childPos[2]));
00211        localTrans.setRotation(btQuaternion(childOrn[0],childOrn[1],childOrn[2],childOrn[3]));
00212        compoundShape->addChildShape(localTrans,childShape);
00213 }
00214 
00215 void plSetEuler(plReal yaw,plReal pitch,plReal roll, plQuaternion orient)
00216 {
00217        btQuaternion orn;
00218        orn.setEuler(yaw,pitch,roll);
00219        orient[0] = orn.getX();
00220        orient[1] = orn.getY();
00221        orient[2] = orn.getZ();
00222        orient[3] = orn.getW();
00223 
00224 }
00225 
00226 
00227 //     extern  void         plAddTriangle(plMeshInterfaceHandle meshHandle, plVector3 v0,plVector3 v1,plVector3 v2);
00228 //     extern  plCollisionShapeHandle plNewStaticTriangleMeshShape(plMeshInterfaceHandle);
00229 
00230 
00231 void          plAddVertex(plCollisionShapeHandle cshape, plReal x,plReal y,plReal z)
00232 {
00233        btCollisionShape* colShape = reinterpret_cast<btCollisionShape*>( cshape);
00234        btAssert(colShape->getShapeType()==CONVEX_HULL_SHAPE_PROXYTYPE);
00235        btConvexHullShape* convexHullShape = reinterpret_cast<btConvexHullShape*>( cshape);
00236        convexHullShape->addPoint(btPoint3(x,y,z));
00237 
00238 }
00239 
00240 void plDeleteShape(plCollisionShapeHandle cshape)
00241 {
00242        btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
00243        assert(shape);
00244        btAlignedFree(shape);
00245 }
00246 void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling)
00247 {
00248        btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
00249        assert(shape);
00250        btVector3 scaling(cscaling[0],cscaling[1],cscaling[2]);
00251        shape->setLocalScaling(scaling);   
00252 }
00253 
00254 
00255 
00256 void plSetPosition(plRigidBodyHandle object, const plVector3 position)
00257 {
00258        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00259        btAssert(body);
00260        btVector3 pos(position[0],position[1],position[2]);
00261        btTransform worldTrans = body->getWorldTransform();
00262        worldTrans.setOrigin(pos);
00263        body->setWorldTransform(worldTrans);
00264 }
00265 
00266 void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation)
00267 {
00268        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00269        btAssert(body);
00270        btQuaternion orn(orientation[0],orientation[1],orientation[2],orientation[3]);
00271        btTransform worldTrans = body->getWorldTransform();
00272        worldTrans.setRotation(orn);
00273        body->setWorldTransform(worldTrans);
00274 }
00275 
00276 void   plGetOpenGLMatrix(plRigidBodyHandle object, plReal* matrix)
00277 {
00278        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00279        btAssert(body);
00280        body->getWorldTransform().getOpenGLMatrix(matrix);
00281 
00282 }
00283 
00284 void   plGetPosition(plRigidBodyHandle object,plVector3 position)
00285 {
00286        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00287        btAssert(body);
00288        const btVector3& pos = body->getWorldTransform().getOrigin();
00289        position[0] = pos.getX();
00290        position[1] = pos.getY();
00291        position[2] = pos.getZ();
00292 }
00293 
00294 void plGetOrientation(plRigidBodyHandle object,plQuaternion orientation)
00295 {
00296        btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
00297        btAssert(body);
00298        const btQuaternion& orn = body->getWorldTransform().getRotation();
00299        orientation[0] = orn.getX();
00300        orientation[1] = orn.getY();
00301        orientation[2] = orn.getZ();
00302        orientation[3] = orn.getW();
00303 }
00304 
00305 
00306 
00307 //plRigidBodyHandle plRayCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);
00308 
00309 //     extern  plRigidBodyHandle plObjectCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);