Using Bullet Only For Collision Detection 2014-08-12

Bullet Physics is a great library for adding physics to a game, it can also be used purely as a collision detection tool. I wanted to document the simplest way to set bullet up for collision detection, and then get the contact information back from bullet.

There is nothing special about the code, but I wanted to have it here for my own reference

btCollisionConfiguration* bt_collision_configuration;
btCollisionDispatcher* bt_dispatcher;
btBroadphaseInterface* bt_broadphase;
btCollisionWorld* bt_collision_world;

double scene_size = 500;
unsigned int max_objects = 16000;

bt_collision_configuration = new btDefaultCollisionConfiguration();
bt_dispatcher = new btCollisionDispatcher(bt_collision_configuration);

btScalar sscene_size = (btScalar) scene_size;
btVector3 worldAabbMin(-sscene_size, -sscene_size, -sscene_size);
btVector3 worldAabbMax(sscene_size, sscene_size, sscene_size);
//This is one type of broadphase, bullet has others that might be faster depending on the application
bt_broadphase = new bt32BitAxisSweep3(worldAabbMin, worldAabbMax, max_objects, 0, true);  // true for disabling raycast accelerator

bt_collision_world = new btCollisionWorld(bt_dispatcher, bt_broadphase, bt_collision_configuration);
//Create two collision objects
btCollisionObject* sphere_A = new btCollisionObject();
btCollisionObject* sphere_B = new btCollisionObject();
//Move each to a specific location
sphere_A->getWorldTransform().setOrigin(btVector3((btScalar) 2, (btScalar) 1.5, (btScalar) 0));
sphere_B->getWorldTransform().setOrigin(btVector3((btScalar) 2, (btScalar) 0, (btScalar) 0));
//Create a sphere with a radius of 1
btSphereShape sphere_shape(1);
//Set the shape of each collision object
//Add the collision objects to our collision world

//Perform collision detection

int numManifolds = bt_collision_world->getDispatcher()->getNumManifolds();
//For each contact manifold
for (int i = 0; i < numManifolds; i++) {
  btPersistentManifold* contactManifold = bt_collision_world->getDispatcher()->getManifoldByIndexInternal(i);
  btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
    btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
    contactManifold->refreshContactPoints(obA->getWorldTransform(), obB->getWorldTransform());
    int numContacts = contactManifold->getNumContacts();
    //For each contact point in that manifold
    for (int j = 0; j < numContacts; j++) {
      //Get the contact information
        btManifoldPoint& pt = contactManifold->getContactPoint(j);
        btVector3 ptA = pt.getPositionWorldOnA();
        btVector3 ptB = pt.getPositionWorldOnB();
        double ptdist = pt.getDistance();

Minkowski Portal Refinement References 2014-06-09

Original Reference

The Minkowski Portal Refinement method, also known as XenoCollide was developed by Gary Snethen (Xeno). As far as I know the original implementation in 3D is only described in Game Programming Gems 7 where working source code was also provided.

Source Code

As of this writing these are the main implementations that I found online:


Library for collision detection between two convex shapes

Jitter Physics

Jitter physics is a realtime physics engine for .Net


Convergence Behavior of Several Solvers in GAMS and Matlab 2014-06-05

In this post I wanted to document the behavior of several different numerical methods using a set of benchmark test problems.

Test Problems

  • Ball sliding on plane
  • Block sliding on plane
  • Block sliding from rest on incline
  • Block coming to resting on incline
  • Ball falling on stack of 20 balls


Solver (Matlab)   Solver (Gams)
Nesterov   Conopt
Barzilai-Borwein   IPopt
Jacobi   Path

OpenGL text without any external libraries 2014-05-30

In the world of OpenGL, dealing with text is not always straightforward. GLUT is one method but it’s becoming old and in some cases deprecated (OSX 10.9). The freetype2 library is one method for generating text, using this library isn’t difficult but getting the best performance requires generating an “Atlas” for your font. An atlas is essentially a texture that contains every single character for a font at a given font size along with information on how to access that character. There are a few libraries that can generate an atlas for you, freetype-gl being one of them.

There is an alternative that in my mind is more portable and easier to use. freetype-gl comes with several awesome little demos on how to use the library, it also comes with a small executable called makefont that generates a header file with all of the needed information.

Provide it with a path for a .ttf font, the name of the header file, the font size and the variable name in the header that you will access it with.

makefont [--help] --font <font file> --header <header file> --size <font size> --variable <variable name>

If you find that the texture size is too small go into the code for makefont.c and change the following line so that it has the size you require. the default was set to 128x128 and I changed it to 256x256 . If your texture size cannot contain the full font, characters will be missing. Look at the output of the makefont command to make sure that characters are not missing.

texture_atlas_t * atlas = texture_atlas_new( 256, 256, 1 );

There is a demo called demo-makefont that demonstrates usage of this file for classic GL, modern GL usage with shaders is slightly different.


Lets begin with the shaders and that get that out of the way


OpenGL GLSL shader as a string 2014-05-29

One annoyance that I have with GLSL shaders is that the vertex and fragment shader files must be placed into the directory with the binaries (or some other path accessible by the program). There are alternatives that involve using a resource manager but for smaller programs this seems like overkill.

One solution is to make a string out of the shader and include it with the code. The major problem with this is that editing the shader becomes a bit more tedious. One solution which I will discuss here involves converting the shader at compile time into a header and including it into the binary/library. I am using CMake for my build system which makes it trivial to do this.


GLSL Shader to Header

Cmake Embed Resources

Using these two posts you should be able to figure everything out but I wanted to describe in detail the full process.


Cmake takes care of the heavy lifting and determines whether not not the header file needs to be generated. In my build system I have the shaders stored in a “resource” folder in my ${CMAKE_SOURCE_DIR} and the header files will be generated in the ${CMAKE_BINARY_DIR}.


This awesome little piece of code was written by the user ScottManDeath from this post. I made a small modification and got rid of the debug wait for user input and made the char array a const.

The program takes three arguments: