Debug triangles in Bullet Physics

By , last updated October 24, 2019

debug triangles bullet physics and irrlichtIn our game “Burnt Islands” that is a heavy physics based game in a dynamic world we needed to make physics shapes of the objects in the scene. We use Irrlicht graphics engine in the game with “Bullet physics” as a physics engine. In a previous post “From Irrlicht mesh to Bullet Physics mesh” we described how to make a physics mesh from a graphics mesh.

But how can you be sure that you have done it right? You need to test your mesh. Debugging can be easily done with Bullet physics engine:

// Bullet physics world
btDiscreteDynamicsWorld	*m_PhysicsWorld;

if ( isDebugging )
{
	m_PhysicsWorld->debugDrawWorld();
}

screenshot_20140330T204418.218864Introduce a debugging mode to the game and see your physics mesh.

One more thing:

This class is the glue between Irrlicht and Bullet Physics. It’s plain and simple without any advanced features. Your class must inherit from btIDebugDraw, and then you have to implement the pure virtual methods required. At a minimum, at least both drawLine methods should be implemented to do something.

void BulletDebugRender::drawLine( const btVector3& from,const btVector3& to, const btVector3& color )
{
	drawLine(from, to, color, color);
}

void BulletDebugRender::drawLine( const btVector3& from,const btVector3& to, const btVector3& fromColor, const btVector3& toColor )
{
	SColorf fromC;	fromC.set(fromColor[0],fromColor[1],fromColor[2],fromColor[3]);
	SColorf toC;	toC.set(toColor[0],toColor[1],toColor[2],toColor[3]);

	Graphics->drawLine(from, to, fromC, toC);
}

// Implementation of Graphics::drawLine
void Graphics::drawLine( const vector3df &from, const vector3df &to, const SColorf &fromColor, const SColorf &tocolor )
{
	matrix4 id;
	id.makeIdentity();
	m_Driver->setTransform(video::ETS_WORLD, id);
	m_Driver->draw3DLine(from, to, fromColor.toSColor());
}

And finally you have to remember to set the actual renderer for Bullet

BulletDebugRender m_BulletDebug;
btDiscreteDynamicsWorld	*m_PhysicsWorld; // Bullet physics world

m_PhysicsWorld->setDebugDrawer( &m_BulletDebug );

Senior Software Engineer developing all kinds of stuff.