Updated BulletDino.c demo, to test C-API.

This commit is contained in:
ejcoumans
2007-08-02 06:09:35 +00:00
parent 30ffcbb5fe
commit 574aad98bc
3 changed files with 413 additions and 11 deletions

View File

@@ -1,5 +1,8 @@
/// this demo will be modified to use the upcoming Bullet C-API, stay tuned.
/* This demo has been modified to use the Bullet C-API.
The C-API is minimal, and will develop based on developer feedback.
The C++ API is recommended, and compatible with the C-API.
*/
/* Copyright (c) Mark J. Kilgard, 1994, 1997. */
@@ -68,6 +71,16 @@
#endif
//#include "../../include/Bullet-C-Api.h"
#include "../include/Bullet-C-Api.h"
plPhysicsSdkHandle physicsSdk=0;
plDynamicsWorldHandle dynamicsWorld=0;
plRigidBodyHandle floorRigidBody;
plRigidBodyHandle dinoRigidBody;
/* Variable controlling various rendering modes. */
static int stencilReflection = 1, stencilShadow = 1, offsetShadow = 1;
static int renderShadow = 1, renderDinosaur = 1, renderReflection = 1;
@@ -136,6 +149,7 @@ makeFloorTexture(void)
GLubyte floorTexture[16][16][3];
GLubyte *loc;
int s, t;
loc=0;
/* Setup RGB image for the texture. */
loc = (GLubyte*) floorTexture;
@@ -325,10 +339,22 @@ static void
drawDinosaur(void)
{
glPushMatrix();
plReal matrix[16];
plVector3 dinoWorldPos;
glPushMatrix();
/* Translate the dinosaur to be at (0,8,0). */
glTranslatef(-8, 0, -bodyWidth / 2);
glTranslatef(0.0, jump, 0.0);
plGetOpenGLMatrix(dinoRigidBody,matrix);
// plGetPosition(dinoRigidBody,dinoWorldPos);
// glTranslatef(-8, 0, -bodyWidth / 2);
//glTranslatef(0.0, jump, 0.0);
// glTranslatef(dinoWorldPos[0],dinoWorldPos[1],dinoWorldPos[2]);
glMultMatrixf(matrix);
// glutSolidCube(15);
glTranslatef(-8.5, -8.5, 0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
glCallList(BODY_WHOLE);
glTranslatef(0.0, 0.0, bodyWidth);
@@ -341,6 +367,8 @@ drawDinosaur(void)
glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
glCallList(EYE_WHOLE);
glPopMatrix();
}
@@ -660,7 +688,7 @@ motion(int x, int y)
starty = y;
glutPostRedisplay();
}
if (lightMoving) {
if (0){//lightMoving) {
lightAngle += (x - lightStartX)/40.0;
lightHeight += (lightStartY - y)/20.0;
lightStartX = x;
@@ -674,13 +702,21 @@ static void
idle(void)
{
static float time = 0.0;
static float prevtime = 0.0;
float dtime;
prevtime = time;
time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
dtime = time - prevtime;
jump = 4.0 * fabs(sin(time)*0.5);
if (!lightMoving) {
lightAngle += 0.03;
lightAngle = time;
}
if (dynamicsWorld)
plStepSimulation(dynamicsWorld,dtime);
glutPostRedisplay();
}
@@ -792,12 +828,65 @@ supportsOneDotOne(void)
return 0; /* OpenGL version string malformed! */
}
int
main(int argc, char **argv)
{
int i;
plCollisionShapeHandle floorShape;
plCollisionShapeHandle dinoShape,dinoChildShape;
plVector3 floorPos,childPos;
plVector3 dinoPos;
plQuaternion childOrn,dinoOrient;
void* user_data;
physicsSdk = plNewBulletSdk();
dynamicsWorld = plCreateDynamicsWorld(physicsSdk);
printf("BulletDino\n");
//create ground plane
floorShape = plNewConvexHullShape();
for (i=0;i<4;i++)
{
// floorVertices
plAddVertex(floorShape,floorVertices[i][0],floorVertices[i][1],floorVertices[i][2]);
}
floorShape = plNewBoxShape(120,0,120);
floorRigidBody = plCreateRigidBody(user_data,0.f,floorShape);
floorPos[0] = 0;
floorPos[1] = 0;
floorPos[2] = 0;
plSetPosition(floorRigidBody,floorPos);
plAddRigidBody(dynamicsWorld,floorRigidBody);
//create dino rigidbody
dinoChildShape = plNewBoxShape(8.5,8.5,8.5);
dinoShape = plNewCompoundShape();
childPos[0] = 0;
childPos[1] = 0;
childPos[2] = 0;
childOrn[0] = 0;
childOrn[1] = 0;
childOrn[2] = 0;
childOrn[3] = 1;
plAddChildShape(dinoShape,dinoChildShape,childPos,childOrn);
dinoPos[0] = -10; dinoPos[1] = 28; dinoPos[2] = 0;
dinoRigidBody = plCreateRigidBody(0,1.0,dinoShape);
plSetPosition(dinoRigidBody,dinoPos);
plSetEuler(0,0,3.15*0.20,dinoOrient);
plSetOrientation(dinoRigidBody,dinoOrient);
plAddRigidBody(dynamicsWorld,dinoRigidBody);
printf("BulletDino\n");
glutInit(&argc, argv);
for (i=1; i<argc; i++) {
@@ -895,5 +984,10 @@ printf("BulletDino\n");
findPlane(floorPlane, floorVertices[1], floorVertices[2], floorVertices[3]);
glutMainLoop();
plDeleteDynamicsWorld(dynamicsWorld);
plDeletePhysicsSdk(physicsSdk);
return 0; /* ANSI C requires main to return int. */
}

View File

@@ -43,7 +43,8 @@ extern "C" {
PL_DECLARE_HANDLE(plCollisionShapeHandle);
/* Constraint for Rigid Bodies */
PL_DECLARE_HANDLE(plConstraintHandle);
/* Triangle Mesh interface */
PL_DECLARE_HANDLE(plMeshInterfaceHandle);
/*
Create and Delete a Physics SDK
@@ -79,21 +80,37 @@ extern "C" {
extern plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height);
extern plCollisionShapeHandle plNewConeShape(plReal radius, plReal height);
extern plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height);
extern plCollisionShapeHandle plNewCompoundShape();
extern void plAddChildShape(plCollisionShapeHandle compoundShape,plCollisionShapeHandle childShape, plVector3 childPos,plQuaternion childOrn);
extern void plDeleteShape(plCollisionShapeHandle shape);
/* Convex Meshes */
extern plCollisionShapeHandle plNewConvexHullShape();
extern void plAddVertex(plCollisionShapeHandle convexHull, plReal x,plReal y,plReal z);
/* Concave static triangle meshes */
extern plMeshInterfaceHandle plNewMeshInterface();
extern void plAddTriangle(plMeshInterfaceHandle meshHandle, plVector3 v0,plVector3 v1,plVector3 v2);
extern plCollisionShapeHandle plNewStaticTriangleMeshShape(plMeshInterfaceHandle);
extern void plSetScaling(plCollisionShapeHandle shape, plVector3 scaling);
/* SOLID has Response Callback/Table/Management */
/* PhysX has Triggers, User Callbacks and filtering */
/* ODE has the typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2); */
/* typedef void plUpdatedPositionCallback(void* userData, plRigidBodyHandle rbHandle, plVector3 pos); */
/* typedef void plUpdatedOrientationCallback(void* userData, plRigidBodyHandle rbHandle, plQuaternion orientation); */
typedef void plUpdatedPositionCallback(void* userData, plRigidBodyHandle rbHandle, plVector3 pos);
typedef void plUpdatedOrientationCallback(void* userData, plRigidBodyHandle rbHandle, plQuaternion orientation);
/* get world transform */
extern void plGetOpenGLMatrix(plRigidBodyHandle object, plReal* matrix);
extern void plGetPosition(plRigidBodyHandle object,plVector3 position);
extern void plGetOrientation(plRigidBodyHandle object,plQuaternion orientation);
/* set world transform (position/orientation) */
extern void plSetPosition(plRigidBodyHandle object, const plVector3 position);
extern void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation);
extern void plSetEuler(plReal yaw,plReal pitch,plReal roll, plQuaternion orient);
typedef struct plRayCastResult {
plRigidBodyHandle m_body;

View File

@@ -0,0 +1,291 @@
/*
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.
*/
/*
Draft high-level generic physics C-API. For low-level access, use the physics SDK native API's.
Work in progress, functionality will be added on demand.
If possible, use the richer Bullet C++ API, by including <src/btBulletDynamicsCommon.h>
*/
#include "Bullet-C-Api.h"
#include "btBulletDynamicsCommon.h"
/*
Create and Delete a Physics SDK
*/
struct btPhysicsSdk
{
// btDispatcher* m_dispatcher;
// btOverlappingPairCache* m_pairCache;
// btConstraintSolver* m_constraintSolver
btVector3 m_worldAabbMin;
btVector3 m_worldAabbMax;
//todo: version, hardware/optimization settings etc?
btPhysicsSdk()
:m_worldAabbMin(-1000,-1000,-1000),
m_worldAabbMax(1000,1000,1000)
{
}
};
plPhysicsSdkHandle plNewBulletSdk()
{
return (plPhysicsSdkHandle)new btPhysicsSdk;
}
void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk)
{
btPhysicsSdk* phys = reinterpret_cast<btPhysicsSdk*>(physicsSdk);
delete phys;
}
/* Dynamics World */
plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle)
{
btPhysicsSdk* physicsSdk = reinterpret_cast<btPhysicsSdk*>(physicsSdkHandle);
btDispatcher* dispatcher = new btCollisionDispatcher();
btOverlappingPairCache* pairCache = new btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax);
btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver();
return (plDynamicsWorldHandle) new btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver);
}
void plDeleteDynamicsWorld(plDynamicsWorldHandle world)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
delete dynamicsWorld;
}
void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
dynamicsWorld->stepSimulation(timeStep);
}
void plAddRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
assert(body);
dynamicsWorld->addRigidBody(body);
}
void plRemoveRigidBody(plDynamicsWorldHandle world, plRigidBodyHandle object)
{
btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world);
assert(dynamicsWorld);
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
assert(body);
dynamicsWorld->removeRigidBody(body);
}
/* Rigid Body */
plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionShapeHandle cshape )
{
btTransform trans;
trans.setIdentity();
btVector3 localInertia(0,0,0);
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
if (mass)
{
shape->calculateLocalInertia(mass,localInertia);
}
btRigidBody* body = new btRigidBody(mass, 0,shape,localInertia);
body->setWorldTransform(trans);
body->setUserPointer(user_data);
return (plRigidBodyHandle) body;
}
void plDeleteRigidBody(plRigidBodyHandle cbody)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody);
assert(body);
delete body;
}
/* Collision Shape definition */
plCollisionShapeHandle plNewSphereShape(plReal radius)
{
return (plCollisionShapeHandle) new btSphereShape(radius);
}
plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z)
{
return (plCollisionShapeHandle) new btBoxShape(btVector3(x,y,z));
}
plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height)
{
//capsule is convex hull of 2 spheres, so use btMultiSphereShape
btVector3 inertiaHalfExtents(radius,height,radius);
const int numSpheres = 2;
btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)};
btScalar radi[numSpheres] = {radius,radius};
return (plCollisionShapeHandle) new btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres);
}
plCollisionShapeHandle plNewConeShape(plReal radius, plReal height)
{
return (plCollisionShapeHandle) new btConeShape(radius,height);
}
plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height)
{
return (plCollisionShapeHandle) new btCylinderShape(btVector3(radius,height,radius));
}
/* Convex Meshes */
plCollisionShapeHandle plNewConvexHullShape()
{
return (plCollisionShapeHandle) new btConvexHullShape();
}
/* Concave static triangle meshes */
plMeshInterfaceHandle plNewMeshInterface()
{
return 0;
}
plCollisionShapeHandle plNewCompoundShape()
{
return (plCollisionShapeHandle) new btCompoundShape();
}
void plAddChildShape(plCollisionShapeHandle compoundShapeHandle,plCollisionShapeHandle childShapeHandle, plVector3 childPos,plQuaternion childOrn)
{
btCollisionShape* colShape = reinterpret_cast<btCollisionShape*>(compoundShapeHandle);
btAssert(colShape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE);
btCompoundShape* compoundShape = reinterpret_cast<btCompoundShape*>(colShape);
btCollisionShape* childShape = reinterpret_cast<btCollisionShape*>(childShapeHandle);
btTransform localTrans;
localTrans.setIdentity();
localTrans.setOrigin(btVector3(childPos[0],childPos[1],childPos[2]));
localTrans.setRotation(btQuaternion(childOrn[0],childOrn[1],childOrn[2],childOrn[3]));
compoundShape->addChildShape(localTrans,childShape);
}
void plSetEuler(plReal yaw,plReal pitch,plReal roll, plQuaternion orient)
{
btQuaternion orn;
orn.setEuler(yaw,pitch,roll);
orient[0] = orn.getX();
orient[1] = orn.getY();
orient[2] = orn.getZ();
orient[3] = orn.getW();
}
// extern void plAddTriangle(plMeshInterfaceHandle meshHandle, plVector3 v0,plVector3 v1,plVector3 v2);
// extern plCollisionShapeHandle plNewStaticTriangleMeshShape(plMeshInterfaceHandle);
void plAddVertex(plCollisionShapeHandle cshape, plReal x,plReal y,plReal z)
{
btCollisionShape* colShape = reinterpret_cast<btCollisionShape*>( cshape);
btAssert(colShape->getShapeType()==CONVEX_HULL_SHAPE_PROXYTYPE);
btConvexHullShape* convexHullShape = reinterpret_cast<btConvexHullShape*>( cshape);
convexHullShape->addPoint(btPoint3(x,y,z));
}
void plDeleteShape(plCollisionShapeHandle cshape)
{
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
delete shape;
}
void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling)
{
btCollisionShape* shape = reinterpret_cast<btCollisionShape*>( cshape);
assert(shape);
btVector3 scaling(cscaling[0],cscaling[1],cscaling[2]);
shape->setLocalScaling(scaling);
}
void plSetPosition(plRigidBodyHandle object, const plVector3 position)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
btAssert(body);
btVector3 pos(position[0],position[1],position[2]);
btTransform worldTrans = body->getWorldTransform();
worldTrans.setOrigin(pos);
body->setWorldTransform(worldTrans);
}
void plSetOrientation(plRigidBodyHandle object, const plQuaternion orientation)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
btAssert(body);
btQuaternion orn(orientation[0],orientation[1],orientation[2],orientation[3]);
btTransform worldTrans = body->getWorldTransform();
worldTrans.setRotation(orn);
body->setWorldTransform(worldTrans);
}
void plGetOpenGLMatrix(plRigidBodyHandle object, plReal* matrix)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
btAssert(body);
body->getWorldTransform().getOpenGLMatrix(matrix);
}
void plGetPosition(plRigidBodyHandle object,plVector3 position)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
btAssert(body);
const btVector3& pos = body->getWorldTransform().getOrigin();
position[0] = pos.getX();
position[1] = pos.getY();
position[2] = pos.getZ();
}
void plGetOrientation(plRigidBodyHandle object,plQuaternion orientation)
{
btRigidBody* body = reinterpret_cast< btRigidBody* >(object);
btAssert(body);
const btQuaternion& orn = body->getWorldTransform().getRotation();
orientation[0] = orn.getX();
orientation[1] = orn.getY();
orientation[2] = orn.getZ();
orientation[3] = orn.getW();
}
//plRigidBodyHandle plRayCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);
// extern plRigidBodyHandle plObjectCast(plDynamicsWorldHandle world, const plVector3 rayStart, const plVector3 rayEnd, plVector3 hitpoint, plVector3 normal);