Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 366
Noir Avatar asked Nov 15 '14 16:11

Noir


People also ask

How do I give bullet VBO in a 3D physics simulation?

Physics engines don’t know anything about OpenGL; and in fact, all of them can run without any 3D visualization at all. So you can’t directly give Bullet your VBO. You have to add a Rigid Body in the simulation instead. Notice that the Rigid Body use the Collision Shape to determine its shape.

How to draw a shape with Bullet physics SDK?

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 : Each type of shape from shape->getShapeType () has its own dedicated function for rendering with opengl

How does bullet work without OpenGL?

Bullet is independent of OpenGL, it's up to you to apply the transformation to your geometry. You can store your transforms into separate objects instead of modifying your vertices.

How do I rotate a shape in OpenGL?

There is not any function in openGl that allow you to apply rotation and translation to any entire shape because in opengl you just manipulate vertices, so i think you need to create your own graphics shape class. This class must at least contain all the vertices used to represent your graphics shape.


1 Answers

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

like image 93
Irrmich Avatar answered Sep 28 '22 01:09

Irrmich