Simplified BulletDinoDemo and how to compile it on Linux

abaraba
Posts: 56
Joined: Thu Jun 19, 2008 7:54 am

Simplified BulletDinoDemo and how to compile it on Linux

Post by abaraba »

hi,

my knowledge about Bullet Physics Engine just increased from 0% to around 1% ..and so let me save you some time if you too want to boil down "Bullet" demos...

the question was - from which side to bite this Bullet?
and it just happens 1st demo i decided to 'cook' is actually using C-API, which i did not realize at the moment... anyway, this one was easy to start with as it was all in one file

lets cook and boil this down !

1.)
plGetOpenGLMatrix(dinoRigidBody, matrix);

...sweet!
"Bullet" takes care of everything and most conveniently give us the result in OpenGL matrix ...ok, "draw()" is taken care of, but before we draw we need what's usually something like "move()" call in our main loop:

2.)
plStepSimulation(dynamicsWorld, dtime);

...too easy,
and for the end lets consider this: we will define/draw box in our "Graphics World" with "glutSolidCube(10)", just how great it would be to have something similar to "draw" box in "Physics World" too ...and so there is:

3.)
plCreateRigidBody(0, 1.0, plNewBoxShape(5, 5, 5));

...nice,
for initial setup glTranslate and glRotate in "Physics World" look something like this:

objPos[0] = x;
objPos[1] = y;
objPos[2] = z;
plSetPosition(box, objPos);

plSetEuler(rotX, rotY, rotZ, objOrient);
plSetOrientation(box, objOrient);


3, 2, 1 - that's really all there is to know to make the most basic demo with Bullet. here it is, boiled down code of BulletDinoDemo, lets call it "main.c" and put it in "BulletDinoDemo" folder, compile with:

gcc main.c -lglut -L../../out/linuxx86/optimize/libs -lbulletdynamics -lbulletmath -lbulletcollision

//---------------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

#include "../../src/Bullet-C-Api.h"

static float time = 0.0;
static plReal matrix[16];

plPhysicsSdkHandle phySdk;
plDynamicsWorldHandle dynWorld;
plRigidBodyHandle box1, box2;

static void draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//*** draw box1
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
plGetOpenGLMatrix(box1, matrix);
glMultMatrixf(matrix);
glutSolidCube(40);
glPopMatrix();

//*** draw box2
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
plGetOpenGLMatrix(box2, matrix);
glMultMatrixf(matrix);
glutSolidCube(10);
glPopMatrix();

glutSwapBuffers();
}


static void timer(void)
{
float dtime = time;
time = glutGet(GLUT_ELAPSED_TIME) / 500.0;
dtime = time - dtime;

if(dynWorld)
plStepSimulation(dynWorld, dtime);

glutPostRedisplay();
}


int main(int argc, char **argv)
{
//*** init Bullet Physics
plVector3 objPos;
plQuaternion objOrient;

phySdk = plNewBulletSdk();
dynWorld = plCreateDynamicsWorld(phySdk);

//*** box1 - STATIC
box1 = plCreateRigidBody(0, 0.0, plNewBoxShape(20, 20, 20));

objPos[0] = 0;
objPos[1] = -20;
objPos[2] = 0;
plSetPosition(box1, objPos);

plSetEuler(0, 0.25, -0.05, objOrient);
plSetOrientation(box1, objOrient);

plAddRigidBody(dynWorld, box1);

//*** box2 - DYNAMIC
box2 = plCreateRigidBody(0, 1.0, plNewBoxShape(5, 5, 5));

objPos[0] = -10;
objPos[1] = 50;
objPos[2] = 0;
plSetPosition(box2, objPos);

plSetEuler(0.8, 0.7, 0.4, objOrient);
plSetOrientation(box2, objOrient);

plAddRigidBody(dynWorld, box2);


//*** init GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Jumpin' little BoX");

glutDisplayFunc(draw);
glutIdleFunc(timer);


//*** init OpenGL
glMatrixMode(GL_PROJECTION);
gluPerspective( 50.0, 1.0, 20.0, 100.0);

glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 5.0, 90.0, 0.0, 8.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_CULL_FACE);
glEnable(GL_COLOR_MATERIAL);

glutMainLoop();

//*** EXIT
plDeleteDynamicsWorld(dynWorld);
plDeletePhysicsSdk(phySdk);
return 0;
}
//---------------------------------------------------------------------------------------------------



thanks for making "Bullet" Open Source
cheers


p.s.
let me know if this was any help, so i'll post other "boiled down recipes" as i go through the rest of the demos

[EDIT: "*beginners* - Boiled Down 'HelloWorld' demo with OpenGL", is a new thread in 'Demos' section of this forum where maybe i should have posted this one as well... anyway, its the "~same" program as this listing here, except it is translated to use C++ API] http://www.bulletphysics.com/Bullet/php ... =17&t=2277
meclory
Posts: 12
Joined: Wed Jun 18, 2008 4:29 pm

Re: Simplified BulletDinoDemo and how to compile it on Linux

Post by meclory »

Thank you!

It's great and a big help to study. Can I also see other "boiled down recipes"?
abaraba
Posts: 56
Joined: Thu Jun 19, 2008 7:54 am

Re: Simplified BulletDinoDemo and how to compile it on Linux

Post by abaraba »

you're welcome... and i do have one more,
it builds up on this simple example, with addition of "stage_map", ie. instead of big blue cube there is a *stunt race track* as a static background, so new lesson is how to initialize "btBvhTriangleMeshShape", keys - w,s,a,d

check back here, i'll post it some time later today


[edit:]
here it is:
http://www.geocities.com/ze_aks/Boiled3.tar.bz2

files should be in some folder inside /bullet/Demo, eg "../bullet/Demos/Boiled3/main.cpp"


this is a new bit,
it makes a NEW SHAPE out of mesh vertices data, quite similar to creating a DISPLAY LIST in OpenGL, actually if you look at the "world.h" you may notice that there is the ~same loop as this one:
//---------------------------------------------------------------------------------------------------
//*** init WORLD MESH - STATIC
int i,j;
btVector3 btV3[3];
btTriangleMesh *mTriMesh = new btTriangleMesh();
for(i=0;i<sizeof(face_indicies)/sizeof(face_indicies[0]);i++)
{
for(j=0;j<3;j++)
{
int vi=face_indicies[j];
btV3[j] = btVector3(vertices[vi][0],vertices[vi][1],vertices[vi][2]);
}
mTriMesh->addTriangle(btV3[0], btV3[1], btV3[2]);
}

shape = new btBvhTriangleMeshShape(mTriMesh, true);

trans.setIdentity();
motionState = new btDefaultMotionState(trans);
world_mesh = new btRigidBody(btScalar(0.0), motionState, shape);

dynamicsWorld->addRigidBody(world_mesh);
//---------------------------------------------------------------------------------------------------



cheers