Showing posts with label Havok tutorial. Show all posts
Showing posts with label Havok tutorial. Show all posts

Sunday, April 1, 2012

Havok Tutorial: A way to detect bullet collisions

Hey. This will be less of a tutorial and more of a "here's a way to do something".

One way you can detect your bullets hitting objects in Havok is through the use of Phantoms. These are aabb's that exist simply to report what collides with them (and to apply events to those colliding objects if you'd like). This makes Phantoms great for things like triggering events based on a player's location, and bullets.

Check out the demo at: Demo\Demos\Physics\Api\Dynamics\Phantoms\PhantomObject for example code on using Phantoms.

So for example, you can draw your bullets as particles, and maintain a phantom for each of these bullets. When a bullet phantom's collision list contains an enemy you can say doBulletHitEnemy() or whatever.

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.

Friday, March 23, 2012

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>