Tuesday, March 27, 2012

Havok Tutorial: Detect a ball bounce.

So lets say you have several balls bopping around your scene. Or heads. Or grenades. Whatever. And you need to do something every time one of the balls bounces ( off of the ground, a wall, etc ), like play a sound effect.

You'll want to check out the Havok demos at  \Demo\Demos\Physics\Api\Collide\ContactPointCallbacks\EndOfStepCallback. Overriding the hkpContactListener class will give you the ability to launch a callback every time a rigidbody with that listener equipped experiences a collision.

Here is some code...

// Define our derived class for collision call backs.
class MyEndOfStepListener : public hkReferencedObject, public hkpContactListener
{
public:
 HK_DECLARE_CLASS_ALLOCATOR( HK_MEMORY_CLASS_DEMO );

 void collisionAddedCallback( const hkpCollisionEvent& event )
 {
  registerForEndOfStepContactPointCallbacks( event );
 }

 void collisionRemovedCallback( const hkpCollisionEvent& event )
 {
  unregisterForEndOfStepContactPointCallbacks( event );
 }

 void contactPointCallback( const hkpContactPointEvent& event )
 {
  if ( event.m_contactPointProperties->m_flags & hkContactPointMaterial::CONTACT_IS_NEW )
  {
   if(  hkpContactPointEvent::TYPE_MANIFOLD_AT_END_OF_STEP )
    my_func();
  }
 }
};

// Create the listener.
MyEndOfStepListener* m_listener = new MyEndOfStepListener();

// Create the rigid body
hkpRigidBody* rb = new hkpRigidBody(bodyCinfo);

// Set our listener.
rb->addContactListener( m_listener );

// Clean up when done.
rb->removeContactListener( m_listener );


Now of course you can extend this technique to detecting various kinds of collisions by using this and other collision listener types. Check out the demos, they will give you great ideas.

Sunday, March 25, 2012

3D Bezier Rails

If you'd like to have a 3D "rail" in your application you can use Bezier curves.

Rails are really useful for defining paths for objects to travel across.

Bay-Z-Ay curves can be really simple too for implementing rails. All you need are:

  • A list of 3D points in the order of your path.
  • A function to evaluate the position along this path from time 0 to 1.
Here is an example of a function one could use to get this bezier curve position (written in javascript but it doesn't really matter):


function atT( t: float,
     p0 : Vector3, p1 : Vector3, p2 : Vector3 ) : Vector3 {
        var t2 : float = t * t;
        
        var one_min_t : float = 1 - t;
        var one_min_t_2 : float = one_min_t * one_min_t;
        
        var x : float = one_min_t_2 * p0.x + 
         2 * one_min_t * t * p1.x +
         t2 * p2.x;
        var y : float = one_min_t_2 * p0.y + 
         2 * one_min_t * t * p1.y +
         t2 * p2.y;
        var z : float = one_min_t_2 * p0.z + 
         2 * one_min_t * t * p1.z +
         t2 * p2.z;
        
        return(Vector3(x,y,z));
    }


So given your list of points ( 0, 1, 2, 3, 4, 5, 6... ), you'd break them up into groups of 3 like ( [0,1,2], [2,3,4], [4,5,6]... ). Keep track of which group/segment your on and just interpolate from 0 to 1 to move along it. When you hit 1, increment to the next group/segment and restart at 0.

Saturday, March 24, 2012

C++0x Do It Now

Links:

Hey there. This post is just to spread a bit of awareness about the recent changes to C++ that have been made. Check out those links, no seriously go.

I myself have used the auto keyword for iterators and it is indeed sweet. Normally I'd typedef an iterator declaration but no more! I'll get around to the other stuff eventually but in general it's all intuitive and time saving. Nice stuff.

Friday, March 23, 2012

Havok Tutorial: Content Tools Part 1

What are the Havok Content Tools?

A time will come when you've got stuff working and want to flesh out a stage or something with physical objects like rigid bodies so that you can stand on grounds and smack walls... The Content Tools give you Maya, 3DS Max, and some other things I can remember because I don't use them, plugins that allow you to export entire scenes of physical objects for easy (seriously it's very easy) loading into your program.

I use Maya 2011 Student version, so what I have to say here will be about that.

After you've downloaded and installed the plugin (check out my other tutorials for that), open up Maya and go to WINDOWS>SETTINGS/PREFERENCES>PLUGIN MANAGER. Once there scroll down to hctMayaSceneExport.mll. Select both Auto load and Loaded.

Now on your main tool bar (the one with "File" at the far left) you'll see Havok Content Tools (to the far right probably).

To use this stuff, what you can do is create your geometry as you normally would in Maya, and then convert the meshes you want to into rigid bodies by selecting the mesh and then going to HAVOK CONTENT TOOLS > PHYSICS > CREATE RIGID BODIES. This will turn the selected mesh into a rigid body. A rigid body is a physical object the Havok engine can use to express things like surfaces and collisions with those surfaces.

Once you've created your stage/environment/level/world of rigid bodies you an go to HAVOK CONTENT TOOLS > EXPORT (click the box). Hit the export button and the Havok Content Tools Filter Manager should open up.

This part confused me even after reading the help manual that came with the Content Tools (which you need to read at some point). Basically you need to add the following filters in order to use the scene you've created as a bunch of physical objects in your game...

  • Align Scene To Node
  • Find Mesh Instances
  • Transform Scene
  • Create Rigid Bodies
  • Optimize Shape Hierarchy
  • Preview Tool
Now i'll be the first to tell you that I don't know (or care enough to remember) what some of these filters do. However I know that you need the Preview Tool for sure.

Once you have these filters in your Configuration Set hit Run Configuration. The Preview Tool will open and show you your scene running in real time. Nice.

To actually export what you've created to file (something I couldn't find out how to do anywhere) go to your live preview, FILE > SAVE. Now you can save your scene out in any format you choose to for easy loading into your game. I use HKT.

So that's how you make basic use of the Havok Content Tools Plugin for Maya 2011. I hope this helps out.

Havok Tutorial: Physics API Part 2

It's really important to understand a few things about Havok so that you don't spend hours working on a single error that doesn't really make sense.

The following are some things I've figured out in using Havok myself. I can verify that the described methods work.

  1. Havok needs to run within a single scope.
    • The physics engine initialization/destruction, physics world initialization/destruction, yadda yadda, any calls to Havok need to all take place withing a single block scope. What I mean by this is think of Havok as being another game loop. You can put Havok into the same while(running){} loop as your game or give it it's own thread, but either way it's a running thing that demands everything happen withing one block.
    • You can of course break things up into methods and functions, but they must be called within this loop block. Sub-scopes are also of course fine, just keep it withing the big one.
    • Some things will need to be initialized locally and cannot be declared as a class member. If you have a class member pointer to some havok thing and it gives an access violation error when you initialize it then sorry but you probably need to move the dudette's declaration into the local scope of it's use.
I'll continue to add things here. These "gotcha"s can really slow down development so hopefully you'll be able to absorb whats posted here and avoid the problems yourself.

Update:
So a question I posted on the Intel Havok forums was answered and it's related to the scoping and access violation errors. Here is the response...

"Hi, this looks like a fairly common problem. Here's a another thread with the same issue: http://software.intel.com/en-us/forums/showthread.php?t=80707 (see 3rd post). And the same kind of issue here: http://software.intel.com/en-us/forums/showpost.php?p=178103

The problem is your hkMallocAllocator is going out of scope. Since this is just in your main thread the easiest solution is mentioned in the first post - switch to using 'hkMallocAllocator::m_defaultMallocAllocator' which already exists at a global scope and will not be destructed for the duration of the application.

-Tyler

PS. I'm going to go ahead an change the Havok demos that use the stack allocated hkAllocators in main() to use hkMallocAllocator::m_defaultMallocAllocator instead since it seems to be causing a lot of confusion."

Havok Tutorial: Physics API Part 1

So what your going to want to do to get started using Havok in your C++ code is check out the great tutorial here: http://marcoarena.wordpress.com/2011/04/17/nice-to-meet-you-havok/. Its what I used to get started and It really is helpful.

Also, you can check out the Havok Youtube channel here: http://www.youtube.com/user/HavokEnthusiast.

Essentially you need to open up a demo project and recreate the settings in your own from scratch project. It isn't difficult if your used to VS2010 and if you aren't then it will pose a challenge.

Here are the includes I use in my project.


// Havok
 // Common
#include <Common/Base/hkBase.h>
#include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h>
#include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h>
#include <Common/Base/Container/String/hkStringBuf.h>

 // Physics
#include <Physics/Dynamics/World/hkpWorld.h>
#include <Physics/Dynamics/Entity/hkpRigidBody.h>
#include <Physics/Utilities/Dynamics/Inertia/hkpInertiaTensorComputer.h>
#include <Physics/Collide/hkpCollide.h>   
#include <Physics/Collide/Dispatch/hkpAgentRegisterUtil.h>

 // Shapes.
#include <Physics/Collide/Shape/Convex/Box/hkpBoxShape.h>  
#include <Physics/Collide/Shape/Convex/Sphere/hkpSphereShape.h>    
#include <Physics/Collide/Shape/Convex/Capsule/hkpCapsuleShape.h>
#include <Physics/Internal/Collide/BvCompressedMesh/hkpBvCompressedMeshShape.h>
#include <Physics/Internal/Collide/StaticCompound/hkpStaticCompoundShape.h>

 // CInfo.
#include <Physics/Internal/Collide/BvCompressedMesh/hkpBvCompressedMeshShapeCinfo.h>

 // Filter.
#include <Physics/Collide/Filter/Group/hkpGroupFilter.h>

 // Raycasting.
#include <Physics/Collide/Query/CastUtil/hkpWorldRayCastInput.h>         
#include <Physics/Collide/Query/CastUtil/hkpWorldRayCastOutput.h>    
#include <Physics/Collide/Query/Collector/RayCollector/hkpAllRayHitCollector.h>

 // Multithreading.
#include <Common/Base/Thread/Job/ThreadPool/Cpu/hkCpuJobThreadPool.h>
#include <Common/Base/Thread/JobQueue/hkJobQueue.h>
 
 // Character rigid Body.
#include <Physics/Utilities/CharacterControl/CharacterRigidBody/hkpCharacterRigidBody.h> 
#include <Physics/Utilities/CharacterControl/CharacterRigidBody/hkpCharacterRigidBodyListener.h>

 // Character State machine.
#include <Physics/Utilities/CharacterControl/StateMachine/hkpDefaultCharacterStates.h>

 // Reducing collision tolerances between characters and fixed entities.
#include <Physics/Collide/Agent/hkpCollisionQualityInfo.h>
#include <Physics/Collide/Dispatch/hkpCollisionDispatcher.h>
#include <Physics/Collide/Agent3/Machine/Nn/hkpAgentNnTrack.h>
#include <Physics/Dynamics/Collide/hkpSimpleConstraintContactMgr.h>

 // Scene Data.
#include <Common/SceneData/Scene/hkxScene.h>

 // Serialization.
#include <Common/Base/System/Io/IStream/hkIStream.h>
#include <Common/Base/Reflection/hkClass.h>
#include <Common/Base/Reflection/Registry/hkTypeInfoRegistry.h>
#include <Common/Serialize/Util/hkStructureLayout.h>
#include <Common/Serialize/Util/hkRootLevelContainer.h>
#include <Common/Serialize/Util/hkSerializeUtil.h>
#include <Physics/Utilities/Serialize/hkpPhysicsData.h>

 // Visual Debugger includes
#include <Common/Visualize/hkVisualDebugger.h>
#include <Physics/Utilities/VisualDebugger/hkpPhysicsContext.h>

 // Platform specific initialization
#include <Common/Base/System/Init/PlatformInit.cxx>

Havok Tutorial: Getting The Tools Downloaded

First off, your gonna need to download a few things to use Havok properly, or better yet, easily.
Here is a list of things to get...

  • The Havok Physics and Animation API.
  • The Havok Content Tools Kit.
You can get the physics and animation api from here (or something around here): http://www.havok.com/try-havok. This will give you what you will be programming with in your code. It will come with executable demos as well as source code for those demos to serve as complete code examples to learn how to do stuff.

The content tools kit can bet obtained here: http://software.intel.com/sites/havok/en/. MAKE SURE YOU GET A VERSION TO MATCH YOUR MAYA OR 3DS MAX. I had to get the non 64bit 2011 or something version so that I could use the tools in my student copy of Maya 2011.

Unzip the physics and animation api and browse on down to the demos. Check em out, run the executable (there is one that has all the working demos in it), feel cool.

Install your content tools kit too.

I'll go over these things in other tutorials but for now, yea, thats how you get the actual packages needed to use Havok.

Havok Tutorial

So I've recently needed to learn the Havok physics library/engine for a class project. Lol let me tell you something about Havok...

It's great, it really is. But is has so many features that it can be near impossible to get started with it.

BUT FEAR NOT! I have shed blood learning the Havok API well enough to support basic (but good) use. I'm going to pass on the fruits of my labor to you, the visitor of this modest blog page, and tell you what you need to do to express your ideas using Havok in your own programs/projects.

First let me make a few things clear...

  • This/These tutorials are aimed at those create VS2010 C++ projects.
  • I learned all this in order to create a Windows7 PC game.
  • I am not a professional but I *try* use efficient designs.
  • The Havok demo source code will be your primary source of learning the api (other than me lol huuray!).
  • The tutorials will be broken up into multiple posts to cover several topics.
With all that being said, I hope what I post is useful to anyone out there looking to integrate Havok into their games.

Tuesday, March 20, 2012

Intel Thread Building Block Resources

Getting started with Intel's Thread Building Blocks (TBB) library can be pretty painless if you know how to go about it. Their pdf resources are great, but I just was not impressed with the lack of complete code examples for beginners. With a complete "hello world!" you can get up and running fast and can actually figure out the rest for yourself from there on.

So, here are some resources for getting started with TBB. I use these and can say for sure that they are very useful.

Note that these resources are targeted towards Visual Studio users. I myself use vs2010.

Monday, March 19, 2012

HowTo: Blogger Syntax Highlight

The following is a way to get code syntax highlighting into your blogger posts.

I've had easier times with Wordpress if I remember correctly but w/e this is for Blogger so who cares.

Sources:
In there search for the </head> tag.
Right above that post the following:





















To actually post code you need to take a block of html like this into your posts "Edit HTML" and paste it like so:



int main( int argc, char** argv ){
printf("Hi there...\n");
return 0;
}





Which will give you this:


int main( int argc, char** argv ){
printf("Hi there...\n");
return 0;
}




Here is the html for this page. The completeness of it may help...



The following is a way to get code syntax highlighting into your blogger posts.

I've had easier times with Wordpress if I remember correctly but w/e this is for Blogger so who cares.

Sources:
In there search for the </head> tag.
Right above that post the following:





















To actually post code you need to take a block of html like this into your posts "Edit HTML" and paste it like so:



int main( int argc, char** argv ){
printf("Hi there...\n");
return 0;
}





Which will give you this:


int main( int argc, char** argv ){
printf("Hi there...\n");
return 0;
}





vs2010 Add console to win32

source: http://stackoverflow.com/questions/3009042/how-to-view-printf-output-in-win32-app-on-visual-studio-2010

I'd done this before with utilities and what not but this is by far the simplest solution I've seen to getting console output into a win32 vs project. I really like it so far.

Go to properties (alt + f7).

Linker > System > SubSystem.

Change it to Console.

Go your your main file.

Go to :



int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow){...}




Below that function add:



int main() {
return _tWinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOW);
}



And your done.

Saturday, March 17, 2012

C++ auto indention online yes....

http://indentcode.net/,

These things can strip out your newlines and what not so they aren't bullet proof solutions to jarbled source, but, you may still want to try them out for yourself.

Wednesday, March 7, 2012

Class Project - Albert Rhesus Space Racing

My team recently completed a prototype for our Games Group.

Here is the link to our Unity project: http://tier7.net/arsr/WebTest/.

Lol the controls suck (one of the things I developed). We have moved onto the next stage of development and things should get cooler soon.

Monday, March 5, 2012

First post

I figured I should take my home server wordpress and move it over to something more accessible like a blogspot.

I'll transport the posts soon!