Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Ernegien

By: a guest on Jul 7th, 2008  |  syntax: C++  |  size: 2.83 KB  |  views: 143  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "StdAfx.h"
  2. #include "Havok.h"
  3.  
  4. #define STACK_SIZE              1024 * 256
  5.  
  6. Havok::Havok(void)
  7. {
  8.         // set up memory manager
  9.     hkMemory* memoryManager = new hkPoolMemory();
  10.     ThreadMemory = new hkThreadMemory(memoryManager);
  11.         hkBaseSystem::init(memoryManager, ThreadMemory, ReportError);
  12.     char* stackBuffer = hkAllocate<char>(STACK_SIZE, HK_MEMORY_CLASS_BASE);
  13.     ThreadMemory->setStackArea(stackBuffer, STACK_SIZE);
  14.     hkThreadMemory::getInstance().setStackArea(stackBuffer, STACK_SIZE);
  15.     memoryManager->removeReference();
  16.  
  17.         // create world
  18.     hkpWorldCinfo info;
  19.     //info.m_simulationType = hkpWorldCinfo::SIMULATION_TYPE_CONTINUOUS;
  20.     info.m_simulationType = hkpWorldCinfo::SIMULATION_TYPE_DISCRETE;
  21.     info.m_gravity.set(0.0f, -9.8f, 0.0f);
  22.     info.setBroadPhaseWorldSize(10000.0f);
  23.     info.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_ASSERT;
  24.     //info.m_enableDeactivation = false;        // use for small timesteps!!!
  25.         info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM);
  26.     World = new hkpWorld(info);
  27.  
  28.         // register all collision agents
  29.     hkpAgentRegisterUtil::registerAllAgents(World->getCollisionDispatcher());
  30. }
  31. Havok::~Havok(void)
  32. {
  33.         World->removeReference();
  34.     World = HK_NULL;
  35.     hkBaseSystem::quit();
  36. }
  37.  
  38. // update havok world and sync with our game objects
  39. void Havok::Update(float timestep)
  40. {
  41.         hkVector4 pos, vel, avel;       // declare first so they get aligned properly
  42.         hkQuaternion rot;
  43.  
  44.         World->stepDeltaTime(timestep); // update havok state
  45.  
  46.         // simple test to update active objects and remove inactive ones
  47.         for (int i=0; i<Objects.Count(); i++)
  48.         {
  49.                 Object* obj = Objects[i];
  50.                 if (!obj->PhysicsModel->isAddedToWorld())
  51.                 {
  52.                         Objects.Remove(obj);    // remove if not part of world - how does this happen???
  53.                         i--;
  54.                 }
  55.                 else if (!obj->PhysicsModel->isActive())
  56.                 {
  57.                         RemoveObject(obj);              // remove when deactivated
  58.                         i--;
  59.                 }
  60.                 else
  61.                 {
  62.                         // relay those changes to our objects for rendering by the graphics engine
  63.                         pos = obj->PhysicsModel->getPosition();
  64.                         pos.store3(&obj->Position.x);
  65.                         rot = obj->PhysicsModel->getRotation();
  66.                         rot.m_vec.store4(&obj->Rotation.x);
  67.                         vel = obj->PhysicsModel->getLinearVelocity();   // extra stuff
  68.                         vel.store3(&obj->Velocity.x);
  69.                         avel = obj->PhysicsModel->getAngularVelocity();
  70.                         avel.store3(&obj->AngularVelocity.x);
  71.                 }
  72.         }
  73.  
  74. }
  75. void Havok::AddObject(Object* object)
  76. {
  77.         Objects.Add(object);
  78.         World->addEntity(object->PhysicsModel);
  79.         object->PhysicsModel->removeReference();        // give world full ownership
  80.         assert(object->PhysicsModel->getReferenceCount() == 1);
  81. }
  82. void Havok::RemoveObject(Object* object)
  83. {
  84.         object->PhysicsModel->activate();       // re-activate everything in current island
  85.         World->removeEntity(object->PhysicsModel);
  86.         assert(object->PhysicsModel->getReferenceCount() == 0);
  87.         Objects.Remove(object);
  88. }
clone this paste RAW Paste Data