Showing posts with label visual debugger. Show all posts
Showing posts with label visual debugger. Show all posts

Sunday, November 6, 2011

Havok: Connecting your in-game camera to the Havok Visual Debugger

In projects using Havok, one thing that I've found invaluable is having the Havok Visual Debugger (HVD) visually match what you see in the game. Luckily, Havok provides a way for you to pass your camera's information to the HVD.

To send a camera's information to the HVD, you'll need the following:

  • The position of the camera
  • The forward/facing direction
  • The up vector/axis
  • The near and far plane distances
  • The camera's FOV (field of view), in degrees (NOT radians)
  • The name of the camera that you want to be seen and shown in the HVD.

Once you have all that information, you simply make the call to Havok's macro. I recommend creating an update function that you can call each frame when the camera updates, or when the renderer finally grabs the camera's information.

void UpdateVisualDebugger()
{
    Vector3 destination = m_position + m_forwardDir;

    HK_UPDATE_CAMERA( m_position, destination, m_upDir, m_nearPlaneDist, 
                      m_farPlaneDist, m_FOV, m_Name.c_str() );
}

Havok doesn't deal with STL types like strings, so you'll need to use .c_str() to send it the char pointer instead.

When you first connect the debugger to the game you will notice it isn't connected to the camera (this can be made to happen automatically, I may go over that in a future post). To connect to the camera, go to View->User Cameras->WhateverCameraNameYouChose.


If you're not properly or currently sending any camera information to Havok you may not see the 'User Cameras' option at all. Use a breakpoint to make sure you're hitting the macro during program execution.

If all worked well you should see your HVD window match your game's window. Please note that you can resize your HVD window and game window seperately, if you chose different aspect ratios for each window you will not get a perfect visual match between them, so try and keep them similar.


You'll also notice at the bottom of the HVD the name of the camera you're attached to. In my example above I've named my camera "Main Camera".

You may find that you're changing or creating cameras during the course of a game's execution, unfortunately Havok does not support automatically changing to new or different user cameras in the debugger when you change camera's from within game. I have put a support ticket in with them to consider including this in a future version. In the mean-time if you want this functionality you will need to have a user camera name that represents whatever camera happens to be active at the time. I recommend sending the active camera each frame under a generic name like "Active Camera" or "Main Camera", as well as the active camera by its unique name like "Character Camera". This will allow the developer to choose to always see the active camera from within Havok, or choose a unique camera and not have it switch automatically.

Havok: Setting mesh color in the Visual Debugger, Part 1

Anyone developing a game using Havok no doubt knows about the Havok visual debugger (which I'll refer to as the HVD), however if you're using the free SDK there is little provided information on how to send data to the visual debugger. Having worked with the full Havok source on a previous project I can provide what I hope to be some valuable insight on how to better utilize the HVD, which should help you debug your game easier.

Setting colors is the HVD is actually really simple, Havok provides a macro that does the work for you, you only have to pass a byte-packed unsigned 32-bit integer representing the color you want, and the pointer to the hkpCollidable. The format of your integer is 1 byte per channel, first byte is the alpha-channel, then red, blue, and green.

Here's a simple function that does the dirty work for you:

// Needed for calling color change macro
#include <common\visualize\hkdebugdisplay.h>

// You'll of course need any other headers for any other physics stuff 
// you're doing in your file

void SetColorForPhysicsDebugger( unsigned int Red, unsigned int Green,
                                 unsigned int Blue, unsigned int Alpha, 
                                 const hkpCollidable* pCollidable )
{
    // Havok takes an unsigned int (32-bit), allowing 8-bits for 
    // each channel (alpha, red, green, and blue, in that
    // order).

    // Because we only need 8-bits from each of the 32-bit ints 
    // passed into this function, we'll mask the first 24-bits.
    Red &= 0x000000FF;
    Green &= 0x000000FF;
    Blue &= 0x000000FF;
    Alpha &= 0x000000FF;

    // Now we pack the four channels into a single int
    const uint32_t color = (Alpha << 24) | (Red << 16) | (Green << 8) | Blue;

    // We use the macro provided by Havok
    HK_SET_OBJECT_COLOR( reinterpret_cast<hkulong>( pCollidable ), color );
}

In the image below you can see this in action. I have a running sample in which rigid bodies are red and opaque by default, whereas phantoms/triggers are blue and translucent.


Using colors for different categories of objects in your game can be extremely powerful. On one project I was on I created an entire custom interface and tool around filtering physics to show developers only what they were looking for, for example NPCs could collide with certain triggers that human players could not, so there was a filter for looking at the physics world from an NPCs point of view, while in the filter you could only see physics that the NPC could collide with.

This is part 1 of 2 of this series, part 2 shows you how to know when somebody connects to the HVD, because you'll need to resend all color information anytime someone connects.