Wednesday, October 9

Wreaking Havok

There are several steps to setting up Havok from a Maya scene, but it is a great physics engine and is worth the effort.  To begin with, make sure your Havok plugin is enabled in Maya, under the plugin manager.  Now you should have a Havok tab which contains all its specific features.  To create a simple bouncing ball scene, start with a plane and a sphere raised above it.

Next you must select each object in the scene and click the RB button in the Havok tab.  This converts the object into a rigid body that Havok will later apply physics to.  Once your objects are rigid bodies, you can edit their setting in Maya to change mass, centre of gravity, restitution etc.  It is important to add mass to any object you wish gravity to affect, otherwise it will just float there motionless.

An example scene in Maya.  Note the mass of 1 for the sphere.
 Next you must perform a Havok export on the scene.  This window contains all the options for configuring your Havok physics file.  There are many options you can play with, but there are some mandatory features you must add to the configuration (which must be in the correct order).  At the very least, you must transform the scene (which converts it to the correct coordinate frames) and write to file (saves the proper format you specify).  Obviously for our scene, we will also need to create rigid bodies and create a world for them to be in.  These 2 allow objects to simulate having physical properties.  It is necessary for gravity and collisions to work properly.

You can also add 'Bake Scale' to ensure the engine does not have to perform scale calculations.
Also note that you can choose the XML format so that it can be read by you.  Once you have run the particular configuration you have set up, your scene is ready for export.  It is recommended to export each object individually, to keep your files organized.  For this example, we will use the export all feature of Maya, and export the scene as a .FBX.  This is the file type that Havok prefers, and will support the most features.

Before we get into TwoLoc, keep in mind there are some constraints for Havok scenes.  You can create materials and lighting for your objects, but you cannot do everything.  First, some types of lighting (ambient) and transformations are restricted, and more importantly you cannot use any material other than lambert on your objects.  Also, only single colours are allowed if you don't put a texture on your models.  To preview your scene, you may add the preview tool to the Havok configuration.

The ball has just bounced off the plane.  Everything seems to be working so far.
With both the .HBK and .FBX files saved, you now go into TwoLoc and find the FBX Loader code.  Within the code there is a section to specify the file paths for your respective files (also check your textures).  Make sure these are correct, and then simply run the program.  If all steps were done correctly, the scene you created in Maya should now be running from TwoLoc.  To double check that Havok is working, you can open up the Havok Visual Debugger and it should display the exact same thing.  In the VD you can even manipulate objects and they will be updated in real-time in your game engine.

It works!
Finally, whenever you update your model or texture/lighting in Maya, you must go into the Havok files and delete the respective cache files.  TwoLoc will not update anything until these have been deleted.  If your model doesn't look right in your engine, make sure the cache has been deleted!

Using these simple concepts, you can create increasingly complex Havok scenes to use physics with.  Its all about experimentation, and playing around with the many different settings that Havok provides.