EDIT 26/05/14: This information in this tutorial is now out-of-date. Here is the new tutorial for getting started with Havok. I will leave this tutorial here in case it is still useful to someone.

Havok is a widely used middleware in game development, providing technology for animation, behaviour, and physics to name a few. For these tutorials, the physics engine is what we will be using and learning, which I will just call ‘Havok’ from now on for brevity. To visualise what we are doing I will be using the 3D Graphics toolkit Open Scene Graph (OSG). I could use the Visual Debugger to do this (I will come to what that is later), but I personally wish to understand how to make Havok communicate with a graphics engine, and I’ve used OSG before. If you are using a different graphics engine and/or want to view what Havok is doing, you can just use the Visual Debugger.

Hopefully, this and the upcoming tutorials will be helpful to those wish to do the same as me and/or those who just want to learn Havok. I am no expert in how to use Havok – or how it works – but I will try to answer any questions to the best of my ability if you have any. For more in depth questions you may be better consulting the extensive Havok documentation and demos or asking at the Intel Havok forum where Havok engineers roam.

In this tutorial, I intend to give you a brief tour of what you get with the download; show you how to set up Havok in a new project, and make a box drop onto a ground surface. This (and future) tutorials have a few prerequisites: some basic C++ skills and the Microsoft Visual C++ IDE. It would also be helpful to have some general understanding on how physics engines work.

Downloading and Exploring

Download Havok from their website, fill in the form, and accept the terms. You’ll be presented with a list of links. You want the latest version of Havok Physics and Animation SDKs for Programmers which will be at the top of the list underneath the Content Tools links. The VS20## in the brackets refers to what version of Visual Studio it supports so pick the appropriate one for you.

Once downloaded, unpack the SDK wherever and have a look at the folders within.

  • /Docs contains the quickstart and user guides which you should familiarise yourself with if you want to know more about Havok or refer to if you have questions.
  • /Demo contains a ton of demos that demonstrate features, capabilities, and use cases of Havok. All of the demos’ source code is available for you to view and take from. Furthermore, if you navigate into the Demos folder in /Demo and click on the [whatever]_release_multithreaded.exe you’ll be presented with an interface that’ll allow you to view all the demos. Use the arrow keys to navigate through and Enter to view and play one. Hitting Enter again within a demo pauses and gives you various options for that demo. Hitting Esc closes the whole interface. More demo controls can be found in the quickstart guide.
  • /Tools contains the extremely helpful Visual Debugger (VDB) that allows you to view the physical simulation results of your code, and allows you to do this over a network if you so desire.

To see the VDB in action, open it up and you’ll be presented with an empty grid world (like you see on computer graphics software) and [Connecting…] or [Idle] in the title bar. Go to Network -> Connect and you’ll be able to connect to whatever machine the simulation is on. If you’re doing it on the same machine, leave it at as ‘localhost’ and click on Auto Reconnect if you wish – if you don’t, you’ll have to connect each time you want to view the simulation. After this, navigate to StandAloneDemos/ConsoleExampleMt in the /Demo folder, and start up the executable within there. Looking in VDB, you should now see a ball bulleting through some puny walls (or the aftermath of that). By the way, the demo lasts for a minute then closes, if you’re confused on why it suddenly disappears.

The SimpleMultithreadedConsoleMain.cpp is where the majority of the code for this tutorial came from. It is well commented, so it is a good place to start learning. I also used found the code in the PhysicsVdb folder in StandAloneDemos/StepByStep to be usefulif you want to look in there.

Starting from Scratch

Now, let’s start on getting our project linked to Havok. Get your version of Visual Studio up and running, and create an empty console project. Right-click the project’s name and select Properties. You should get something like this:

Now for linking to the common and physics libraries.

  • Firstly, go to C/C++ -> General -> Additional Include Directories. Add Havok’s headers by entering the file dialog and adding the /Source folder within your version of Havok’s folder.
  • Next, go to Linker -> General -> Additional Library Directories to add the library path. You’ll want to navigate into /Lib/win32_vs2010 and select debug_multithreaded.dll. If you’re curious what the others are, take a look in the quickstart guide.
  • Lastly, go to Linker -> Input -> Additional Dependences and add the following:

hkBase.lib                                           hkCompat.lib

hkGeometryUtilities.lib                      hkInternal.lib

hkSceneData.lib                                  hkSerialize.lib

hkVisualize.lib                                    hkpConstraintSolver.lib

hkpCollide.lib                                     hkpDynamics.lib

hkpInternal.lib                                     hkpUtilities.lib

hkpVehicle.lib                                     hkcdCollide.lib

hkcdInternal.lib

If there are any problems, try looking in the quickstart guide or at the properties in the ConsoleExampleMt demo.

Getting the Bare Bones

Since we will only really want to play with Havok’s capabilities, keeping the core functionality to get Havok working done with and out of the way will be ideal. For lack of a better name, I created the class HavokCore to do this. Take a look at its header file below.

Undoubtedly, this would not exactly be a good idea for a real application but for playing about with Havok it will suffice. Let’s see what we have.

The first few variables are to do with the VDB being enabled and initialising it, and stepping through the simulation in a multithread environment. For the latter, looking at the comments in the code for explanations of those and/or the Multithreading chapter in the User Guide would be your best bet for understanding how Havok does multithreading.

HavokCore() and ~HavokCore() call the init and deinit methods respectively with the VDB capable of being enabled through HavokCore(). The public method stepSimulation() steps through the physics simulation by delta time and steps the VDB if it is enabled. Accessing the world is possible through getWorld(), which we will need to use to add rigid bodies (or entities) to the world. A rigid body, if you don’t know, is basically something that doesn’t change size or deform so like a rock or a car.

The .cpp file is a bit long to show here and I missed out the Havok headers you need in the header file  so instead I’ll post the files below. There’s a fair amount of comments that I’ve left from the demos so hopefully they will help you to understand how everything fits together.

HavokCore.h and HacokCore.cpp files.

With getting Havok to initialise and such sorted, let’s start on getting an application/game loop we can use to step the simulation. Do this in your presumably already created main.cpp, keeping in mind to add the header for HavokCore or the better name you chose for it.

I snagged this from the PhysicsVdb demo. What it is basically doing is firstly creating a stopwatch that will calculate real time for us, getting the initial amount of seconds. We then set our simulation to run at a fixed time step of 60 frames per second, meaning that Havok calculates collisions and such 60 times per second. A higher frame rate will generally cause more realistic physics at the cost of greater memory and CPU usage. Put if lower if you need to.

We then have the simulation run for 10 seconds, making the simulation and VDB step forward by the fixed time step and making sure the full time step is passed by pausing until it does. When the simulation is done we do clean up by deleting the HavokCore object. If you run this, you should get a console window similar to this to appear that will disappear after 10 seconds.

Something Interesting

That is massively boring so let’s get something appearing in the VDB. We want a box to fall on a ground surface so add these function and header declarations at the top of main.cpp:

Using these hopefully self-explanatory functions, we will spawn a box slightly above the ground box and have it fall onto it. The first function is this:

In this function, we initialise a new hkpRigidBodyCinfo that we will use to give hkpRigidBody its structural information. As it will be box-shaped, we use hkpBoxShape and set its size. This is what we will use to calculate the object’s collision geometry. We then set its position and motion type. A rigid body’s motion type defines its initial motion, whether it is moving when it is created or fixed, like the ground. We then compute its mass properties and create the actual rigid body. With the rigid body created and added to the world, we can remove references to it and its shape since something else now owns them. The next function is basically the same.

Once you implement that, you’ll need to call these functions in main() like so:

Locking is important when you add entities to the world or change it in some way. Since we are doing this multithreaded, we need to be careful two threads aren’t trying to change the world at the same time which lock prevents. However, it is rather expensive to do so it’s best to do as many operations as you can when you have called lock. If you’re confused (like me), the User Guide might be of help. Anyway, when you run this and look in the VDB – making sure to connect – you should see something like this:

Here’s the final file: main.cpp.

Wrap up

This tutorial has covered the super basics of Havok, and hopefully has given you a basic understanding on what Havok does and how to use it. Play about with the code, change values, and see what happens when you do so. Looking through the extensively commented demos will help you immensely.

Although sparse, there are some resources for learning Havok I found that may be of help:

Piotr Pluta’s Havok Tutorial. You’ll notice the similarities between his and mine; he was my inspiration for these tutorials so, hopefully, try to think of mine as an updated version of his.

HavokEnthusiast’s videos on Youtube.

Havok Overview.

If you have any feedback on this tutorial or know of more resources, don’t hesitate to comment.

Advertisements