PhysicsSystem

pull/21/head
Sebastian Wick 14 years ago
parent 8d4dc096a4
commit 4b846a54f1

@ -140,6 +140,7 @@ source_group(apps\\openmw\\mwsound FILES ${GAMESOUND} ${GAMESOUND_HEADER})
set(GAMEWORLD
mwworld/world.cpp
mwworld/scene.cpp
mwworld/physicssystem.cpp
mwworld/globals.cpp
mwworld/class.cpp
mwworld/actionteleport.cpp
@ -152,6 +153,7 @@ set(GAMEWORLD
set(GAMEWORLD_HEADER
mwworld/refdata.hpp
mwworld/world.hpp
mwworld/physicssystem.hpp
mwworld/scene.hpp
mwworld/environment.hpp
mwworld/globals.hpp

@ -34,8 +34,8 @@ bool ExteriorCellRender::lightOutQuadInLin = false;
int ExteriorCellRender::uniqueID = 0;
ExteriorCellRender::ExteriorCellRender(ESMS::CellStore<MWWorld::RefData> &_cell, MWWorld::Environment& environment,
MWScene &_scene)
: mCell(_cell), mEnvironment (environment), mScene(_scene), mBase(NULL), mInsert(NULL), mAmbientMode (0)
MWScene &_scene, MWWorld::PhysicsSystem *physics)
: mCell(_cell), mEnvironment (environment), mScene(_scene), mBase(NULL), mInsert(NULL), mAmbientMode (0), mPhysics(physics)
{
uniqueID = uniqueID +1;
sg = mScene.getMgr()->createStaticGeometry( "sg" + Ogre::StringConverter::toString(uniqueID));
@ -233,13 +233,15 @@ void ExteriorCellRender::insertMesh(const std::string &mesh)
void ExteriorCellRender::insertObjectPhysics()
{
if (!mInsertMesh.empty())
mScene.addObject (mInsert->getName(), mInsertMesh, mInsert->getOrientation(),
{
mPhysics->addObject (mInsert->getName(), mInsertMesh, mInsert->getOrientation(),
mInsert->getScale().x, mInsert->getPosition());
}
}
void ExteriorCellRender::insertActorPhysics()
{
mScene.addActor (mInsert->getName(), mInsertMesh, mInsert->getPosition());
mPhysics->addActor (mInsert->getName(), mInsertMesh, mInsert->getPosition());
}
// insert a light related to the most recent insertBegin call.

@ -3,6 +3,7 @@
#include "cell.hpp"
#include "cellimp.hpp"
#include "../mwworld/physicssystem.hpp"
#include "OgreColourValue.h"
#include <OgreMath.h>
@ -49,6 +50,7 @@ namespace MWRender
ESMS::CellStore<MWWorld::RefData> &mCell;
MWWorld::Environment &mEnvironment;
MWScene &mScene;
MWWorld::PhysicsSystem *mPhysics;
/// The scene node that contains all objects belonging to this
/// cell.
@ -101,7 +103,7 @@ namespace MWRender
public:
ExteriorCellRender(ESMS::CellStore<MWWorld::RefData> &_cell, MWWorld::Environment& environment,
MWScene &_scene);
MWScene &_scene, MWWorld::PhysicsSystem *physics);
virtual ~ExteriorCellRender() { destroy(); }

@ -195,13 +195,15 @@ void InteriorCellRender::insertMesh(const std::string &mesh)
void InteriorCellRender::insertObjectPhysics()
{
if (!mInsertMesh.empty())
scene.addObject (insert->getName(), mInsertMesh, insert->getOrientation(),
{
mPhysics->addObject (insert->getName(), mInsertMesh, insert->getOrientation(),
insert->getScale().x, insert->getPosition());
}
}
void InteriorCellRender::insertActorPhysics()
{
scene.addActor (insert->getName(), mInsertMesh, insert->getPosition());
mPhysics->addActor (insert->getName(), mInsertMesh, insert->getPosition());
}
// insert a light related to the most recent insertBegin call.

@ -3,6 +3,7 @@
#include "cell.hpp"
#include "cellimp.hpp"
#include "../mwworld/physicssystem.hpp"
#include "OgreColourValue.h"
#include <OgreSceneNode.h>
@ -48,6 +49,7 @@ namespace MWRender
ESMS::CellStore<MWWorld::RefData> &cell;
MWWorld::Environment &mEnvironment;
MWScene &scene;
MWWorld::PhysicsSystem *mPhysics;
/// The scene node that contains all objects belonging to this
/// cell.
@ -93,8 +95,11 @@ namespace MWRender
public:
InteriorCellRender(ESMS::CellStore<MWWorld::RefData> &_cell, MWWorld::Environment& environment,
MWScene &_scene)
: cell(_cell), mEnvironment (environment), scene(_scene), base(NULL), insert(NULL), ambientMode (0) {}
MWScene &_scene, MWWorld::PhysicsSystem *physics)
: cell(_cell), mEnvironment (environment), scene(_scene), base(NULL), insert(NULL), ambientMode (0)
{
mPhysics = physics;
}
virtual ~InteriorCellRender() { destroy(); }

@ -76,121 +76,6 @@ std::pair<std::string, float> MWScene::getFacedHandle (MWWorld::World& world)
return eng->rayTest(from,to);
}
void MWScene::doPhysics (float duration, MWWorld::World& world,
const std::vector<std::pair<std::string, Ogre::Vector3> >& actors)
{
// stop changes to world from being reported back to the physics system
MWWorld::DoingPhysics scopeGuard;
//set the DebugRenderingMode. To disable it,set it to 0
//eng->setDebugRenderingMode(1);
//set the walkdirection to 0 (no movement) for every actor)
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
act->setWalkDirection(btVector3(0,0,0));
}
for (std::vector<std::pair<std::string, Ogre::Vector3> >::const_iterator iter (actors.begin());
iter!=actors.end(); ++iter)
{
OEngine::Physic::PhysicActor* act = eng->getCharacter(iter->first);
//dirty stuff to get the camera orientation. Must be changed!
Ogre::SceneNode *sceneNode = rend.getScene()->getSceneNode (iter->first);
Ogre::Vector3 dir;
Ogre::Node* yawNode = sceneNode->getChildIterator().getNext();
Ogre::Node* pitchNode = yawNode->getChildIterator().getNext();
if(mFreeFly)
{
Ogre::Quaternion yawQuat = yawNode->getOrientation();
Ogre::Quaternion pitchQuat = pitchNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.07*(yawQuat*pitchQuat*dir1);
}
else
{
Ogre::Quaternion quat = yawNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.025*(quat*dir1);
}
//set the walk direction
act->setWalkDirection(btVector3(dir.x,-dir.z,dir.y));
}
eng->stepSimulation(duration);
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
btVector3 newPos = act->getPosition();
MWWorld::Ptr ptr = world.getPtrViaHandle (it->first);
world.moveObject (ptr, newPos.x(), newPos.y(), newPos.z());
}
}
void MWScene::addObject (const std::string& handle, const std::string& mesh,
const Ogre::Quaternion& rotation, float scale, const Ogre::Vector3& position)
{
OEngine::Physic::RigidBody* body = eng->createRigidBody(mesh,handle);
eng->addRigidBody(body);
btTransform tr;
tr.setOrigin(btVector3(position.x,position.y,position.z));
tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w));
body->setWorldTransform(tr);
}
void MWScene::addActor (const std::string& handle, const std::string& mesh,
const Ogre::Vector3& position)
{
//TODO:optimize this. Searching the std::map isn't very efficient i think.
eng->addCharacter(handle);
OEngine::Physic::PhysicActor* act = eng->getCharacter(handle);
act->setPosition(btVector3(position.x,position.y,position.z));
}
void MWScene::removeObject (const std::string& handle)
{
//TODO:check if actor???
eng->removeCharacter(handle);
eng->removeRigidBody(handle);
eng->deleteRigidBody(handle);
}
void MWScene::moveObject (const std::string& handle, const Ogre::Vector3& position, bool updatePhysics)
{
rend.getScene()->getSceneNode(handle)->setPosition(position);
if(updatePhysics)//TODO: is it an actor? Done?
{
if (OEngine::Physic::RigidBody* body = eng->getRigidBody(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
btTransform tr = body->getWorldTransform();
tr.setOrigin(btVector3(position.x,position.y,position.z));
body->setWorldTransform(tr);
}
if (OEngine::Physic::PhysicActor* act = eng->getCharacter(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
act->setPosition(btVector3(position.x,position.y,position.z));
}
}
}
void MWScene::rotateObject (const std::string& handle, const Ogre::Quaternion& rotation)
{
}
void MWScene::scaleObject (const std::string& handle, float scale)
{
}
bool MWScene::toggleCollisionMode()
{
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)

@ -65,30 +65,6 @@ namespace MWRender
/// can be faced
std::pair<std::string, float> getFacedHandle (MWWorld::World& world);
/// Run physics simulation and modify \a world accordingly.
void doPhysics (float duration, MWWorld::World& world,
const std::vector<std::pair<std::string, Ogre::Vector3> >& actors);
/// Add object to physics system.
void addObject (const std::string& handle, const std::string& mesh,
const Ogre::Quaternion& rotation, float scale, const Ogre::Vector3& position);
/// Add actor to physics system.
void addActor (const std::string& handle, const std::string& mesh,
const Ogre::Vector3& position);
/// Remove object from physic systems.
void removeObject (const std::string& handle);
/// Move object.
void moveObject (const std::string& handle, const Ogre::Vector3& position, bool updatePhysics);
/// Change object's orientation.
void rotateObject (const std::string& handle, const Ogre::Quaternion& rotation);
/// Change object's scale.
void scaleObject (const std::string& handle, float scale);
/// Toggle collision mode for player. If disabled player object should ignore
/// collisions and gravity.
/// \return Resulting mode

@ -0,0 +1,171 @@
#include "physicssystem.hpp"
#include "../mwworld/doingphysics.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/world.hpp" // FIXME
#include "OgreRoot.h"
#include "OgreRenderWindow.h"
#include "OgreSceneManager.h"
#include "OgreViewport.h"
#include "OgreCamera.h"
#include "OgreTextureManager.h"
namespace MWWorld
{
PhysicsSystem::PhysicsSystem(OEngine::Render::OgreRenderer &_rend , OEngine::Physic::PhysicEngine* physEng) :
mRender(_rend), mEngine(physEng)
{
}
PhysicsSystem::~PhysicsSystem()
{
}
void PhysicsSystem::doPhysics (float duration, MWWorld::World& world,
const std::vector<std::pair<std::string, Ogre::Vector3> >& actors)
{
// stop changes to world from being reported back to the physics system
MWWorld::DoingPhysics scopeGuard;
//set the DebugRenderingMode. To disable it,set it to 0
//eng->setDebugRenderingMode(1);
//set the walkdirection to 0 (no movement) for every actor)
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = mEngine->PhysicActorMap.begin(); it != mEngine->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
act->setWalkDirection(btVector3(0,0,0));
}
for (std::vector<std::pair<std::string, Ogre::Vector3> >::const_iterator iter (actors.begin());
iter!=actors.end(); ++iter)
{
OEngine::Physic::PhysicActor* act = mEngine->getCharacter(iter->first);
//dirty stuff to get the camera orientation. Must be changed!
Ogre::SceneNode *sceneNode = mRender.getScene()->getSceneNode (iter->first);
Ogre::Vector3 dir;
Ogre::Node* yawNode = sceneNode->getChildIterator().getNext();
Ogre::Node* pitchNode = yawNode->getChildIterator().getNext();
if(mFreeFly)
{
Ogre::Quaternion yawQuat = yawNode->getOrientation();
Ogre::Quaternion pitchQuat = pitchNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.07*(yawQuat*pitchQuat*dir1);
}
else
{
Ogre::Quaternion quat = yawNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.025*(quat*dir1);
}
//set the walk direction
act->setWalkDirection(btVector3(dir.x,-dir.z,dir.y));
}
mEngine->stepSimulation(duration);
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = mEngine->PhysicActorMap.begin(); it != mEngine->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
btVector3 newPos = act->getPosition();
MWWorld::Ptr ptr = world.getPtrViaHandle (it->first);
world.moveObject (ptr, newPos.x(), newPos.y(), newPos.z());
}
}
void PhysicsSystem::addObject (const std::string& handle, const std::string& mesh,
const Ogre::Quaternion& rotation, float scale, const Ogre::Vector3& position)
{
OEngine::Physic::RigidBody* body = mEngine->createRigidBody(mesh,handle);
mEngine->addRigidBody(body);
btTransform tr;
tr.setOrigin(btVector3(position.x,position.y,position.z));
tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w));
body->setWorldTransform(tr);
}
void PhysicsSystem::addActor (const std::string& handle, const std::string& mesh,
const Ogre::Vector3& position)
{
//TODO:optimize this. Searching the std::map isn't very efficient i think.
mEngine->addCharacter(handle);
OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle);
act->setPosition(btVector3(position.x,position.y,position.z));
}
void PhysicsSystem::removeObject (const std::string& handle)
{
//TODO:check if actor???
mEngine->removeCharacter(handle);
mEngine->removeRigidBody(handle);
mEngine->deleteRigidBody(handle);
}
void PhysicsSystem::moveObject (const std::string& handle, const Ogre::Vector3& position, bool updatePhysics)
{
mRender.getScene()->getSceneNode(handle)->setPosition(position);
if(updatePhysics)//TODO: is it an actor? Done?
{
if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
btTransform tr = body->getWorldTransform();
tr.setOrigin(btVector3(position.x,position.y,position.z));
body->setWorldTransform(tr);
}
if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
act->setPosition(btVector3(position.x,position.y,position.z));
}
}
}
void PhysicsSystem::rotateObject (const std::string& handle, const Ogre::Quaternion& rotation)
{
}
void PhysicsSystem::scaleObject (const std::string& handle, float scale)
{
}
bool PhysicsSystem::toggleCollisionMode()
{
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = mEngine->PhysicActorMap.begin(); it != mEngine->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
bool cmode = act->getCollisionMode();
if(cmode)
{
act->enableCollisions(false);
act->setGravity(0.);
act->setVerticalVelocity(0);
mFreeFly = true;
return false;
}
else
{
mFreeFly = false;
act->enableCollisions(true);
act->setGravity(4.);
act->setVerticalVelocity(0);
return true;
}
}
return false; // This should never happen, but it shall not bother us now, since
// this part of the code needs a rewrite anyway.
}
}

@ -0,0 +1,45 @@
#ifndef GAME_MWWORLD_PHYSICSSYSTEM_H
#define GAME_MWWORLD_PHYSICSSYSTEM_H
#include <vector>
#include <openengine/ogre/renderer.hpp>
#include <openengine/bullet/physic.hpp>
namespace MWWorld
{
class PhysicsSystem
{
public:
PhysicsSystem (OEngine::Render::OgreRenderer &_rend , OEngine::Physic::PhysicEngine* physEng);
~PhysicsSystem ();
void doPhysics (float duration, MWWorld::World& world,
const std::vector<std::pair<std::string, Ogre::Vector3> >& actors);
void addObject (const std::string& handle, const std::string& mesh,
const Ogre::Quaternion& rotation, float scale, const Ogre::Vector3& position);
void addActor (const std::string& handle, const std::string& mesh,
const Ogre::Vector3& position);
void removeObject (const std::string& handle);
void moveObject (const std::string& handle, const Ogre::Vector3& position, bool updatePhysics);
void rotateObject (const std::string& handle, const Ogre::Quaternion& rotation);
void scaleObject (const std::string& handle, float scale);
bool toggleCollisionMode();
private:
OEngine::Render::OgreRenderer &mRender;
OEngine::Physic::PhysicEngine* mEngine;
bool mFreeFly;
};
}
#endif

@ -1,7 +0,0 @@
#include "physikssystem.hpp"
namespace MWWorld
{
}

@ -1,4 +0,0 @@
#ifndef GAME_MWWORLD_PHYSIKSSYSTEM_H
#define GAME_MWWORLD_PHYSIKSSYSTEM_H
#endif

@ -27,7 +27,7 @@ namespace MWWorld
{ // silence annoying g++ warning
for (std::vector<std::string>::const_iterator iter (functor.mHandles.begin());
iter!=functor.mHandles.end(); ++iter)
mScene.removeObject (*iter);
mPhysics->removeObject (*iter);
}
mWorld->removeScripts (iter->first);
@ -113,7 +113,7 @@ namespace MWWorld
mWorld->getExterior(x, y)->loadExt (x, y, mWorld->getStore(), mWorld->getEsmReader());
Ptr::CellStore *cell = mWorld->getExterior(x, y);
loadCell (cell, new MWRender::ExteriorCellRender (*cell, mEnvironment, mScene));
loadCell (cell, new MWRender::ExteriorCellRender (*cell, mEnvironment, mScene, mPhysics));
}
}
@ -144,9 +144,9 @@ namespace MWWorld
mCellChanged = true;
}
Scene::Scene (Environment& environment, World *world, MWRender::MWScene& scene)
Scene::Scene (Environment& environment, World *world, MWRender::MWScene& scene, PhysicsSystem *physics)
: mScene (scene), mCurrentCell (0),
mCellChanged (false), mEnvironment (environment), mWorld(world)
mCellChanged (false), mEnvironment (environment), mWorld(world), mPhysics(physics)
{
}
@ -184,7 +184,7 @@ namespace MWWorld
mWorld->getInterior(cellName)->loadInt (cellName, mWorld->getStore(), mWorld->getEsmReader());
Ptr::CellStore *cell = mWorld->getInterior(cellName);
loadCell (cell, new MWRender::InteriorCellRender (*cell, mEnvironment, mScene));
loadCell (cell, new MWRender::InteriorCellRender (*cell, mEnvironment, mScene, mPhysics));
// adjust player
mCurrentCell = cell;

@ -9,6 +9,7 @@
#include <components/esm_store/cell_store.hpp>
#include "../mwrender/mwscene.hpp"
#include "physicssystem.hpp"
#include "refdata.hpp"
#include "ptr.hpp"
@ -62,13 +63,14 @@ namespace MWWorld
bool mCellChanged;
Environment& mEnvironment;
World *mWorld;
PhysicsSystem *mPhysics;
void playerCellChange (Ptr::CellStore *cell, const ESM::Position& position,
bool adjustPlayerPos = true);
public:
Scene (Environment& environment, World *world, MWRender::MWScene& scene);
Scene (Environment& environment, World *world, MWRender::MWScene& scene, PhysicsSystem *physics);
~Scene();

@ -283,6 +283,8 @@ namespace MWWorld
mSky (false), mEnvironment (environment), mNextDynamicRecord (0)
{
mPhysEngine = physEng;
mPhysics = new PhysicsSystem(renderer, physEng);
boost::filesystem::path masterPath (fileCollections.getCollection (".esm").getPath (master));
@ -294,7 +296,7 @@ namespace MWWorld
mStore.load (mEsm);
mPlayer = new MWWorld::Player (mScene.getPlayer(), mStore.npcs.find ("player"), *this);
mScene.addActor (mPlayer->getPlayer().getRefData().getHandle(), "", Ogre::Vector3 (0, 0, 0));
mPhysics->addActor (mPlayer->getPlayer().getRefData().getHandle(), "", Ogre::Vector3 (0, 0, 0));
// global variables
mGlobalVariables = new Globals (mStore);
@ -310,7 +312,7 @@ namespace MWWorld
mPhysEngine = physEng;
mWorldScene = new Scene(environment, this, mScene);
mWorldScene = new Scene(environment, this, mScene, mPhysics);
}
World::~World()
@ -319,6 +321,7 @@ namespace MWWorld
delete mSkyManager;
delete mGlobalVariables;
delete mWorldScene;
delete mPhysics;
}
const ESM::Cell *World::getExterior (const std::string& cellName) const
@ -635,7 +638,7 @@ namespace MWWorld
mEnvironment.mSoundManager->stopSound3D (ptr);
if (!DoingPhysics::isDoingPhysics())
mScene.removeObject (ptr.getRefData().getHandle());
mPhysics->removeObject (ptr.getRefData().getHandle());
}
render->deleteObject (ptr.getRefData().getHandle());
@ -672,7 +675,7 @@ namespace MWWorld
}
}
mScene.moveObject (ptr.getRefData().getHandle(), Ogre::Vector3 (x, y, z),
mPhysics->moveObject (ptr.getRefData().getHandle(), Ogre::Vector3 (x, y, z),
!DoingPhysics::isDoingPhysics());
// TODO cell change for non-player ref
@ -710,7 +713,7 @@ namespace MWWorld
void World::doPhysics (const std::vector<std::pair<std::string, Ogre::Vector3> >& actors,
float duration)
{
mScene.doPhysics (duration, *this, actors);
mPhysics->doPhysics (duration, *this, actors);
}
bool World::toggleCollisionMode()

@ -14,6 +14,7 @@
#include "ptr.hpp"
#include "globals.hpp"
#include "scene.hpp"
#include "physicssystem.hpp"
#include <openengine/bullet/physic.hpp>
@ -73,6 +74,7 @@ namespace MWWorld
ESMS::ESMStore mStore;
ScriptList mLocalScripts;
MWWorld::Globals *mGlobalVariables;
MWWorld::PhysicsSystem *mPhysics;
bool mSky;
Environment& mEnvironment;
int mNextDynamicRecord;

Loading…
Cancel
Save