Sunday, 14 October 2012

Engines 0.4 Havok Primitives

So the easy deadline came and went (well almost. I was extended to Monday), and I now have an ok amount of XP. I still need more to be able to write the midterm though.

I feel like we have A LOT of work to do. Havok isn't exactly the easiest thing to figure out. In fact, good luck with that. May the programming gods help you. We all have to wrap the Havok skeletal animation system as a requirement and I think that will be quite hard.

I've done the "Boucing Ball" easy question where we have to make a ball bounce using Havok rigid bodies but not use the hkx and fbx loaders that are included. To do this I had to make a Havok sphere and a hard surface for it to bounce on. Then I had to place an Ogre sphere and plane where the Havok objects were. Havok objects are invisible except, according to Dan, in the "Havok Visual Debugger". I haven't tried using that though.

Now I had my visible objects and collidable objects but nothing was happening because Ogre and Havok don't talk to each-other unless you make them. So even though my sphere had mass and was affected by gravity in Havok, it was just a sphere mesh in Ogre. This was the hard part. I was trying everything to make my sphere move. I had no idea it was moving in Havok because I couldn't see it when I ran my program.

Then Harry, who was also trying to figure out this question, thought "hmm...maybe it's moving but not moving the Ogre object with it". So I hacked it and finally got it to work.

This is what I did:

  1. Make Ogre objects (or load them in from Maya as .mesh files) and attach them to nodes. Position them wherever you want, but keep in mind you'll have to move your Havok objects to the same positions.
  2. Initialize the Havok World and Havok Scene Manager, then lock the world. (This should be done for you by the project generator but I thought I was initializing the world wrong and spent lots of time re-initializing, just to have to comment all of that out). 
  3. Mark the world "forWrite". This allows you to change things in the Havok world. If you don't do this you get errors because the program can't touch anything in the world. 
  4. Make an hkpSphereShape object and give it the radius of your Ogre sphere. 
  5. Set up the hkpRigidBodyCinfo for the sphere shape. Give it a mass, a shape (the hkpSphereShape you made earlier), friction, and set the motion type as hkpMotion::MOTION_SPHERE_INERTIA. You should also set its position and inertiaTensor (not quite sure what that means, but look at the Havok demos). 
  6. Make a rigid body. Yup, all the steps before don't actually give you a rigid body. It's just setting up the attributes of the rigid body.
  7. I activated the rigid body too, just to make sure it'll move as soon as my program starts. 
  8. Add the rigid body entity to the Havok world.
  9. Do steps 4-7 for the plane. I used hkpBoxShape because I don't know how to make a Havok plane. Also, note that giving something a mass of 0 will make it stationary. 
  10. Unmark the world "forWrite" so that the program knows you're done writing stuff in the world.
  11. Unlock the world (this should also be done for you by the project generator).
So now we have everything we need. The Havok sphere is falling onto the plane and bouncing but you can't see it. To link the positions, I made an hkpVector4 to store the position of the ball rigid body. Then I set the Ogre ball node position to the values stored in the hkpVector4. This is harder than it seems just because Havok is so convoluted. The way you get an x value out of the vector is like this: myVector(0) , or myVector.getSimdAt(0) . Y is at 1, and Z is at 2. 

And that's it. The only thing I have a problem with is WHO THOUGHT IT WAS A GOOD IDEA TO CALL GETTING THE X VALUE "getSimdAt()" ?! 


No comments:

Post a Comment