Search code examples
opengl3djoglbulletjbullet

How to apply Bullet physics to drawn Opengl 3d shapes


I was just wondering whether there is a way to apply bullet physics to opengl drawn objects (created using glVertex3f or triangle mesh with glVertexAttribPointer). I am currently using jogl and jbullet to apply physics to my 3D objects. Specifically if I give a buffer of vertices for a triangle mesh shape for the 3d object. I need Bullet to create a CollisionShape based on the triangle mesh shape, and apply the physics to it, which at the same time apply physics to the drawn opengl objects. At the moment, the physical collision shape may move (in bullet), but the drawn opengl shape is not.

Otherwise, can I create a collision shape, get the vertices of the collision shape after the physics has been applied after each simulation step, and then draw the object based on the location of the vertices. I have looked at the Hello world example for bullet but it only help me apply physics on the position of the opengl object (based on the z,y,x axis_ and not cool things like for example a corner of the cube hitting a plane starts rolling and spinning.

It would be great if someone can give me some code or demo that can do this. Or give me some hints as to how I can make this work. I already looked at this tutorial on bullet: http://www.raywenderlich.com/53077/bullet-physics-tutorial-getting-started. But I can't seem to find the information to how bullet is applied to opengl objects.


Solution

  • After you downloaded Bullet Physics SDK, look the file GL_ShapeDrawer.cpp,you will find some interresting functions such like drawSphere, drawCylinder,drawOpenGl.. the last one i mentionned lets you draw any type of suported shape :

    • CUSTOM_CONVEX_SHAPE_TYPE
    • BOX_SHAPE_PROXYTYPE
    • UNIFORM_SCALING_SHAPE_PROXYTYPE
    • COMPOUND_SHAPE_PROXYTYPE
    • BOX_SHAPE_PROXYTYPE
    • SPHERE_SHAPE_PROXYTYPE

    Each type of shape from shape->getShapeType() has its own dedicated function for rendering with opengl

    But my method involves to

    • 1 - Load the 3D model with a mesh loader
    • 2 - Create the graphics shape with opengl functions using the previous mesh loader
    • 3 - Create bullet shapes from the vertices used by the mesh loader
    • 4 - Implement the Bullet Motion State (when a shape is going to be rotated or translated or just "transformed", you syncronize the "btTransform" of your bullet shape with the transformation matrix of the graphic shape, then update the graphics vertices)

    Personnaly, i'm using Irrlicht because Irrlicht is the "write less,do more" solution of opengl :p