C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/PhysicsManager.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 Copyright (C) 2006 Michael Harnetty
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 -----------------------------------------------------------------------------
00019 */
00020 
00021 #include "PhysicsManager.h"
00022 
00023 namespace GameEngine {
00024 
00025         char const * const PhysicsMovementEvent::eventName = "ObjectMoved";
00026         char const * const PhysicsCollisionEvent::eventName = "Collision";
00027 
00028 
00029         PhysicsManager::PhysicsManager(const Real& worldWidth, const Real& worldHeight, const Real& worldDepth, const Vector3& gravity)
00030                 :       mWidth(worldWidth), mHeight(worldHeight), mDepth(worldDepth)
00031         {
00032                 mPhysicsSim = opal::createSimulator();
00033                 mPhysicsSim->addGlobalCollisionEventHandler(this);
00034 
00035                 opal::Vec3r g(0, 0, 0);
00036                 mPhysicsSim->setGravity(gravity);
00037 
00038                 mPhysicsSim->setStepSize(0.02);
00039         }
00040 
00041         PhysicsManager::~PhysicsManager()
00042         {
00043                 mPhysicsSim->destroy();
00044         }
00045 
00046 
00047 
00048         void PhysicsManager::update(const double& dt)
00049         {
00050                 Process::update(dt);
00051 
00052                 mPhysicsSim->simulate(dt);
00053 
00054         }
00055 
00056 
00057 
00058         void OPAL_CALL PhysicsManager::handleMovementEvent( const opal::MovementEvent& event )
00059         {
00060                 // Fetch the solid moved and get it's position
00061                 opal::Solid* solid = event.solid;
00062                 Vector3 pos = Vector3::vecFromOpal(event.solid->getPosition());
00063 
00064                 // Wrap solids position to keep it within the world
00065                 pos = wrapCoords(pos);
00066                 solid->setPosition(pos);
00067                 solid->getQuaternion().normalize();
00068 
00069                 boost::shared_ptr<Event> physicsMovementEvent (new PhysicsMovementEvent(solid->getUserData(),  pos, Quaternion::quatFromOpal( solid->getQuaternion() ) ));
00070                 EventManager::getSingletonPtr()->queueEvent(physicsMovementEvent);
00071         }
00072 
00073 
00074         void OPAL_CALL PhysicsManager::handleCollisionEvent( const opal::CollisionEvent& event )
00075         {
00076                 opal::Solid* s1 = event.thisSolid;
00077                 opal::Solid* s2 = event.otherSolid;
00078 
00079                 boost::shared_ptr<Event> physicsCollisionEvent ( new PhysicsCollisionEvent(s1->getUserData(), s2->getUserData(), event.pos) );
00080                 EventManager::getSingletonPtr()->queueEvent(physicsCollisionEvent);
00081         }
00082 
00083 
00084         Vector3& PhysicsManager::wrapCoords(Vector3& coords) const
00085         {
00086                 while (coords.mX >  mWidth/2)  coords.mX -= mWidth; 
00087                 while (coords.mY >  mHeight/2) coords.mY -= mHeight; 
00088                 while (coords.mZ >  mDepth/2) coords.mZ -= mDepth; 
00089 
00090                 while (coords.mX < -mWidth/2)  coords.mX += mWidth; 
00091                 while (coords.mY < -mHeight/2) coords.mY += mHeight; 
00092                 while (coords.mZ < -mDepth/2) coords.mZ += mDepth; 
00093 
00094                 return coords;
00095 
00096         }
00097 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2