Thursday, 7 November 2013

Phantom Triggers/Events with Havok

So for my next blog I decided to do some more Havok!

Here I will post how we plan to implement Phantom Triggers for our game. These are some notes I took while reviewing the havok demos on phantom events.

Can use: #include <common/visualize/hkDebugDisplay.h> to display some collision based info
This is where all of the ‘magic’ for this demo takes place and it all centres around the custom implementations of the two pure virtual methods in the base class, namely phantomEnterEvent(…) and phantomLeaveEvent(…). These methods notify us of an entry or exit event for the phantom volume and most importantly the provide a reference to the collidable that has penetrated the phantom. The pseudo code is below

Class MyPhantomShape: public hkpPhantomCallbackShape
{
            Public:
            myPhantomShape(){}
//hkpPhantom interface implementation
virtual void phantomEnterEvent(const hkpCollidable8 collidableA, const hkpCollidable* collidableB, const hkpCollisionInput& env)
{

            //the color can only be changed once the entity has been added to the world
hkpRigidBody* owner = hkpGetRigidBody(collidableB);
//the “collidables” here are faked so its necessary to get the owner first in order to get the real collidable
//this is where the event code goes
}
//hkpPhantom interface implementation
virtual void phantomLeaveEvent(const hkpCollidable* collidableA, const hkpCollidable* collidableB)
{

            //the color can only be changed once the entity has been added to the world
hkpRigidBody* owner = hkpGetRigidBody(collidableB);
//the “collidables” here are faked so its necessary to get the owner first in order to get the real collidable
//this is where the event code goes
}

}

//the reason why we call getOwner() on the original collidable only to later call getCollidable() on the owner is that the collidable passed by reference may be a ‘sub’ collidable of the rigid body, perhaps a hkpTriangleShape belonging to a larger hkpMoppBvTreeShape.
Class PhantomEvents : public hkDefaultPhysicsDemo (env, ERROR_FLAGS)
{
Public:
            PhantomEvents(hkEnviroment* env)
            {

                        //setup the camera
                        //create the world
                        //register the collision agents
                        hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() );
                        //create terrain
                        //create models/scenary
                        //below is the construction code for the phantom volume:
                        //creating a PHANTOM FLOOR
                        {
                        hkpRigidBodyCinfo boxInfo;
                        hkVector4 boxSize( 4.0f, 1.5f , 2.0f );
                        hkpShape* boxShape = new hkpBoxShape( boxSize , 0 );
                        boxInfo.m_motionType = hkpMotion::MOTION_FIXED;

                        boxInfo.m_position.set(2.0f, -4.0f, 0.0f);
                       
                       
                        // MyPhantomShape is the demo implementation of the hkpPhantomCallbackShape.
                        MyPhantomShape* myPhantomShape = new MyPhantomShape();
                        hkpBvShape* bvShape = new hkpBvShape( boxShape, myPhantomShape );
                        boxShape->removeReference();
                        myPhantomShape->removeReference();

                        boxInfo.m_shape = bvShape;
           
                        hkpRigidBody* boxRigidBody = new hkpRigidBody( boxInfo );
                        bvShape->removeReference();

                        m_world->addEntity( boxRigidBody );
                       

                        // the color can only be changed after the entity has been added to the world
                        HK_SET_OBJECT_COLOR((hkUlong)boxRigidBody->getCollidable(), hkColor::rgbFromChars(255, 255, 255, 50));

                        boxRigidBody->removeReference();
                       
            }
}


The phantom volume is created using a hkpBvShape using a hkpBoxShape as the bounding volume and a hkpPhantomCallbackShape as the child shape. The volume is set to be fixed in space and coloured with a slight alpha blend. Once the simulation starts the ball drops into the phantom volume, upon notification of this event we colour the ball red and wait for an exit event. As soon as this event is raised we colour the ball green and apply an impulse upwards back towards the wall and the simulation repeats.

So basically after making the phantom shape you use it as the space for your phantom call back as the child shape. That volume is fixed in space but is not restricted to be a dynamic object such as a moving objective. 



No comments:

Post a Comment