Physics prototype update

12 Jan 2014

I didn’t spend too much time working on ‘2k14: The Game’ last week, but nevertheless made a lot more progress than I had expected to. The first task on my todo list was to create a Box2D application to prototype the game physics, which would have to include some kind of simple visualization to see what’s going on in the physics simulation. I had a few ideas how to go about this: a headless application logging the positions of Box2D shapes, some Matlab plot scripts to visualize the logs, or even an OS X application with a real-time visualization of the Box2D world. At any rate I was prepared to write quite a bit of supporting code before I could even get to the actual prototyping of the game physics. I never used or even looked at Box2D itself before, so I had no idea how much work would be involved in setting up and running the simulation, and how it would relate to the amount of work required to visualize it.

Fortunately, while looking for Box2D tutorials, I came across this excellent site. Strangely there seems to be no mention anywhere who the author of this site is, but this guy has made some seriously impressive Box2D based scenes such as a fully working combustion engine and a pendulum clock which are completely simulated inside Box2D without any scripting or whatever to make them work. His site contains quite a few Box2D tutorials that start at the very basics, and get progressively more advanced, to cover most of the features in Box2D.

One of the very first topics of the tutorials is the testbed application included with the Box2D source code. As it turns out, the testbed implements exactly what I was looking for: a simple visualization of a Box2D world with simple input hooks, a timer loop, ways to extract simulation data from the scene, pausing and resetting it, etc. Best of all, the testbed makes it ridiculously easy to write new test cases that can be selected from the testbed GUI to run them: just copy and rename one of the existing test classes, which are defined in a single C++ header file, strip out and replace the class constructor that sets up the simulation, and make sure to include your new header file and register the test class in the C++ file that lists all test cases. Recompile Box2D to build the testbed, and presto: you have a physics simulation to play around with. The test classes derive from a base class that provides some virtual functions that are called by the testbed to handle inputs (keyboard and mouse), to signal object collisions, to refresh the simulation at regular intervals, etc. You basically just override them in your own test class and add whatever your test case needs. Very easy, and extremely effective!

After creating my own test class, setting up the actual physics simulation was almost trivial. In Box2D, objects are defined by creating a body, representing the object’s non-visible properties such as position, speed, rotation, and one or more fixtures, representing size, shape and materials. Forces can be applied to bodies, and bodies can be attached to each other using various types of joints. I simply started out with a triangular-shaped body to represent the ‘spaceship’, and plugged in key handlers to rotate the ship or engage thrust. At each simulation step, if thrust is engaged, a force is applied to the ship in the opposite direction the ship is pointing. Gravity takes care of the rest. This may all sound terribly simple, which is probably just because it all is very simple what it is ;-). To make things a little fancier I extended the test a little bit to shoot some square shapes in the direction of thrust when it’s engaged. Here’s a screen cast that shows the first prototype in action:

So that covered flying the ship around, next thing to try out was add the tractor beam and orb. Again, this turned out to be a breeze. The only thing necessary was to add a second circular-shaped body with the right size and density (this determines the mass of the body), and attach it to the ship with a distance joint. Here’s a video of the result:

As you can see, the behavior of the ship and orb are almost spot on compared to the original game, I really don’t see much else that needs to be investigated or tweaked with respect to the game physics, at least not for now. All in all, getting the physics prototype up and running took about 3 hours, most of which was spent reading the Box2D tutorials and messing around to get the thrust particles just right ;-)

The next steps will be setting up some classes to represent the various actors in the game, and coming up with a way to represent level data, keeping in mind the fact that we want about everything to be scriptable later on.