Tags

, , , , , , , , , , , , ,

In my previous post, Bullet physics: restitution, I used Bullet Real-Time Physics Simulation to bounce a ball on a table in my virtual world.

But can I update the C++ Microsoft Visual Studio application (with OpenGL graphics library and the Oculus SDK for Windows) to bounce the ball off the table and out of the room? Yes. Thanks to the Bullet sample code on GitHub.

First we need to tilt the table surface that the ball will bounce on by 5 degrees:

static btScalar gTilt = 5.0f / 180.0f*SIMD_PI; // tilt 5 degrees

btTransform startTransform;
startTransform.setIdentity();

startTransform.setOrigin(btVector3(
	btScalar(0),
	btScalar(-0.15),
	btScalar(0)));

btQuaternion incline;
incline.setRotation(btVector3(0, 0, 1), gTilt);
startTransform.setRotation(incline);

btDefaultMotionState* groundMotionState = new btDefaultMotionState(startTransform);

Of course, I need to tilt the table in Blender by 5 degrees too (and import into my virtual world) so that the visuals match the physics.

Talking of which, we also need to update the position and rotation of our visual ball to match the physics:

btVector3 origin = trans.getOrigin();
btQuaternion rot = trans.getRotation();

mesh->Pos.x = origin.getX();
mesh->Pos.y = origin.getY();
mesh->Pos.z = origin.getZ();

mesh->Rot.x = rot.getX();
mesh->Rot.y = rot.getY();
mesh->Rot.z = rot.getZ();
mesh->Rot.w = rot.getW();

Okay, let’s put on the Oculus Rift virtual reality headset and watch the ball drop from the night sky, hit the table at an angle, and bounce out of the room. Here’s the video:

Ciao!

P.S. I should really have a separate rigid body for the floor, so that the second bounce of the ball is not against the plane angled to the table surface. But, fuck it, it’s only 5 degrees out.