Trouble Aligning Meshes And Shapes

Trouble Aligning Meshes And Shapes

Hi all,

I'm new to Havok still, and just working my way through demos and the documentation as I try to integrate some basic physics with my current renderer. I'm currently using a DirectX 9 system.

To begin with I use a small asset editor tool to build a library of assets. This first tool allows default scales and rotations of the meshes used to be selected, as well as physics shapes (only boxes and spheres at the moment!) to be dragged out across the Asset. The next tool I use is a basic scene editing tool, and in it I can lay out a bunch of the assets created earlier. The scene is then saved to a file, including the details of each asset and it's havok shapes. The problems I'm having are when I then load that data into my renderer and re-create the havok shapes. I've been using the wonderful visual debugger to check if havok shapes and the visual meshes are lined up correctly.

When an asset is loaded from the file, all it's visual components are created, then the havok data is created and added to the world. For now all of each asset's shapes for a hkConvexShapeList to create the rigid body. I initially had quite a bit of trouble getting the data from my D3DXMATRIX over to the hkTransform for the shapes, and found that certain axes seemed to come out backwards relative to my visual representation. I ended up swapping the Y and Z axes for most of my input data (scale/size and translation/position) and that seemed to make everything line up correctly, and continue to do so when I then got the transformations from the shapes to update my visual meshes after some physics simulation. It was only after I started adding assets are more varied angles I noticed that some of the shapes still didn't line up correctly, and so I'm looking for some help with this issue please!

This first piece of code is from a loop that creates the shapes from the input data:


// Create Shape
hkVector4 boxSize(mShapeScales[i].x * pEntity->GetScaleX() * 0.5f,
mShapeScales[i].y * pEntity->GetScaleY() * 0.5f,
mShapeScales[i].z * pEntity->GetScaleZ() * 0.5f);
hkpBoxShape* shape = new hkpBoxShape(boxSize);

// Create Rotation Matrix
D3DXMATRIX rotationX, rotationY, rotationZ;
D3DXMatrixRotationX(&rotationX, mShapeRotations[i].x);
D3DXMatrixRotationY(&rotationY, mShapeRotations[i].y);
D3DXMatrixRotationZ(&rotationZ, mShapeRotations[i].z);
D3DXMATRIX mat = rotationZ * rotationY * rotationX;

// Create Transform
hkTransform trans;
hkRotation rot;
hkVector4 pos;

// Rotation
rot.setRows(hkVector4(mat._11, mat._21, mat._31),
hkVector4(mat._12, mat._22, mat._32),
hkVector4(mat._13, mat._23, mat._33));

// Position/Translation
trans.setTranslation(hkVector4(mShapePositions[i].x, mShapePositions[i].y, mShapePositions[i].z));

// Transform Shape
hkpConvexTransformShape* transShape = new hkpConvexTransformShape(shape, trans);

// Add Shape To List


The second is the rigid body being created from the final shape list:


// Create Li st Shape
hkpConvexListShape* listShape = new hkpConvexListShape(&shapes[0], mNumShapes);

// Create Body Info
hkpRigidBodyCinfo shapeInfo;
shapeInfo.m_shape = listShape;

// Decide Body Motion Type
switch (mMotionType)
case HKMOTION_FIXED: shapeInfo.m_motionType = hkpMotion::MOTION_FIXED; break;
case HKMOTION_DYNAMIC: shapeInfo.m_motionType = hkpMotion::MOTION_DYNAMIC; break;

// Set Body Position
shapeInfo.m_position.set(pEntity->GetPositionX(), pEntity->GetPositionY(), pEntity->GetPositionZ());

// Set Body Rotation Matrix
hkRotation rot;
D3DXMATRIX mat = pEntity->GetParentMatrix();
rot.setRows(hkVector4(mat._11, mat._21, mat._31),
hkVector4(mat._12, mat._22, mat._32),
hkVector4(mat._13, mat._23, mat._33));
shapeInfo.m_rotation = hkQuaternion(rot);

// Compute Body Mass Properties
hkReal shapeMass = 10.0f * mNumShapes;
hkpMassProperties shapeMassProperties;
hkpInertiaTensorComputer::computeShapeVolumeMassProperties(listShape, shapeMass, shapeMassProperties);
shapeInfo.m_inertiaTensor = shapeMassProperties.m_inertiaTensor;
shapeInfo.m_centerOfMass = shapeMassProperties.m_centerOfMass;
shapeInfo.m_mass = shapeMassProperties.m_mass;

// Create List Rigid Body
mRigidBody = new hkpRigidBody(shapeInfo);


In the code, I've reset all the axes back to how I think they should be for now. pEntity is a pointer to the visual representation data.

Right, sorry for that being a bit messy, but hopefully someone can give me a hint of where I should start looking, or have a vague idea where I've gone wrong, because so far I'm a bit stumped other than realising that Havok's axes might be on a different system to DirectX's.

Thanks to anyone that contributes!
5 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

I think I've managed to track this issue down to a few missing transformations, so I'll work on that and then come back if I'm still having problems.

Hey CukyDoh,

Have you had a look at the "Rotations, Handedness, and all that" article in the Physics->Physics Articles section of the documentation? Specifically, "Representation Conventions" (which I think is section 2.3.5).

This explains the differences in representation of matrices between DirectX and Havok. Specifically, how Havok is column-major whereas DirectX is row-major. Considering this information, I think the order of access to your mat D3DXMATRIX where you do rot.setRows() may be a little off.

If you do the following, how do things change?

rot.setRows(hkVector4(mat._11, mat._12, mat._13),
hkVector4(mat._21, mat._22, mat._23),
hkVector4(mat._31, mat._32, mat._33));

A bit late, but I just wanted to say thanks for the earlier advice!

I spent a bit of time reading over the suggested material and realised a few errors I'd made in other areas aside from here which saved me from some big headaches later. Thankfully everything seems to work correctly now and interacts correctly with mny character controller too!

Heya CukyDoh,

Glad it's all working out for you. Hope we can see what sort of Havok-powered gameplay you come out with in the end!


Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui