2014-10-17 19:16:48 +00:00
|
|
|
#include "physicssystem.hpp"
|
|
|
|
|
2014-10-26 19:50:51 +00:00
|
|
|
#include <iostream>
|
2014-10-22 19:59:14 +00:00
|
|
|
|
2014-10-21 20:11:04 +00:00
|
|
|
#include <OgreRay.h>
|
|
|
|
#include <OgreCamera.h>
|
2014-10-23 07:51:31 +00:00
|
|
|
#include <OgreSceneManager.h>
|
2014-10-21 20:11:04 +00:00
|
|
|
|
2014-10-17 19:16:48 +00:00
|
|
|
#include <openengine/bullet/physic.hpp>
|
|
|
|
#include <components/nifbullet/bulletnifloader.hpp>
|
2014-10-22 19:59:14 +00:00
|
|
|
#include "../../model/settings/usersettings.hpp"
|
2014-10-26 10:15:47 +00:00
|
|
|
#include "../render/elements.hpp"
|
2014-10-17 19:16:48 +00:00
|
|
|
|
|
|
|
namespace CSVWorld
|
|
|
|
{
|
2014-10-24 09:14:02 +00:00
|
|
|
PhysicsSystem *PhysicsSystem::mPhysicsSystemInstance = 0;
|
|
|
|
|
2014-10-22 19:59:14 +00:00
|
|
|
PhysicsSystem::PhysicsSystem(Ogre::SceneManager *sceneMgr) : mSceneMgr(sceneMgr)
|
2014-10-17 19:16:48 +00:00
|
|
|
{
|
2014-10-24 09:14:02 +00:00
|
|
|
assert(!mPhysicsSystemInstance);
|
|
|
|
mPhysicsSystemInstance = this;
|
|
|
|
|
2014-10-17 19:16:48 +00:00
|
|
|
// Create physics. shapeLoader is deleted by the physic engine
|
|
|
|
NifBullet::ManualBulletShapeLoader* shapeLoader = new NifBullet::ManualBulletShapeLoader();
|
|
|
|
mEngine = new OEngine::Physic::PhysicEngine(shapeLoader);
|
2014-10-24 09:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsSystem::~PhysicsSystem()
|
|
|
|
{
|
|
|
|
delete mEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsSystem *PhysicsSystem::instance()
|
|
|
|
{
|
|
|
|
assert(mPhysicsSystemInstance);
|
|
|
|
return mPhysicsSystemInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::addObject(const std::string &mesh,
|
2014-10-26 19:50:51 +00:00
|
|
|
const std::string &name, const std::string &referenceId, float scale,
|
|
|
|
const Ogre::Vector3 &position, const Ogre::Quaternion &rotation, bool placeable)
|
2014-10-24 09:14:02 +00:00
|
|
|
{
|
2014-10-24 10:18:29 +00:00
|
|
|
mRefToSceneNode[referenceId] = name;
|
2014-10-24 09:14:02 +00:00
|
|
|
|
2014-10-24 10:18:29 +00:00
|
|
|
mEngine->createAndAdjustRigidBody(mesh, referenceId, scale, position, rotation,
|
2014-10-24 09:14:02 +00:00
|
|
|
0, // scaledBoxTranslation
|
|
|
|
0, // boxRotation
|
|
|
|
true, // raycasting
|
|
|
|
placeable);
|
|
|
|
}
|
|
|
|
|
2014-10-26 19:50:51 +00:00
|
|
|
void PhysicsSystem::removeObject(const std::string& name)
|
|
|
|
{
|
|
|
|
mEngine->removeRigidBody(name);
|
|
|
|
mEngine->deleteRigidBody(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::addHeightField(float* heights, int x, int y, float yoffset,
|
|
|
|
float triSize, float sqrtVerts)
|
|
|
|
{
|
|
|
|
mEngine->addHeightField(heights, x, y, yoffset, triSize, sqrtVerts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::removeHeightField(int x, int y)
|
|
|
|
{
|
|
|
|
mEngine->removeHeightField(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, Ogre::Vector3> PhysicsSystem::castRay(float mouseX, float mouseY,
|
|
|
|
Ogre::Vector3* normal, std::string* hit, Ogre::Camera *camera)
|
|
|
|
{
|
|
|
|
if(!mSceneMgr || !camera || !camera->getViewport())
|
|
|
|
return std::make_pair("", Ogre::Vector3(0,0,0)); // FIXME: this should be an exception
|
|
|
|
|
|
|
|
|
|
|
|
// using a really small value seems to mess up with the projections
|
|
|
|
float nearClipDistance = camera->getNearClipDistance(); // save existing
|
|
|
|
camera->setNearClipDistance(10.0f); // arbitrary number
|
|
|
|
Ogre::Ray ray = camera->getCameraToViewportRay(mouseX, mouseY);
|
|
|
|
camera->setNearClipDistance(nearClipDistance); // restore
|
|
|
|
|
|
|
|
Ogre::Vector3 from = ray.getOrigin();
|
|
|
|
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
|
|
|
float farClipDist = userSettings.setting("Scene/far clip distance", QString("300000")).toFloat();
|
|
|
|
Ogre::Vector3 to = ray.getPoint(farClipDist);
|
|
|
|
|
|
|
|
btVector3 _from, _to;
|
|
|
|
_from = btVector3(from.x, from.y, from.z);
|
|
|
|
_to = btVector3(to.x, to.y, to.z);
|
|
|
|
|
|
|
|
uint32_t visibilityMask = camera->getViewport()->getVisibilityMask();
|
|
|
|
bool ignoreHeightMap = !(visibilityMask & (uint32_t)CSVRender::Element_Terrain);
|
|
|
|
bool ignoreObjects = !(visibilityMask & (uint32_t)CSVRender::Element_Reference);
|
|
|
|
|
|
|
|
Ogre::Vector3 norm;
|
|
|
|
std::pair<std::string, float> result =
|
|
|
|
mEngine->rayTest(_from, _to, !ignoreObjects, ignoreHeightMap, &norm);
|
|
|
|
|
|
|
|
if(result.first == "")
|
2014-10-26 21:08:33 +00:00
|
|
|
return std::make_pair("", Ogre::Vector3(0,0,0));
|
|
|
|
else
|
|
|
|
return std::make_pair(result.first, ray.getPoint(farClipDist*result.second));
|
|
|
|
}
|
2014-10-26 19:50:51 +00:00
|
|
|
|
2014-10-26 21:08:33 +00:00
|
|
|
std::string PhysicsSystem::referenceToSceneNode(std::string reference)
|
|
|
|
{
|
|
|
|
return mRefToSceneNode[reference];
|
2014-10-26 19:50:51 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 09:14:02 +00:00
|
|
|
void PhysicsSystem::setSceneManager(Ogre::SceneManager *sceneMgr)
|
|
|
|
{
|
|
|
|
mSceneMgr = sceneMgr;
|
2014-10-17 19:16:48 +00:00
|
|
|
mEngine->setSceneManager(sceneMgr); // needed for toggleDebugRendering()
|
2014-10-26 19:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::toggleDebugRendering()
|
|
|
|
{
|
|
|
|
if(!mSceneMgr)
|
|
|
|
return; // FIXME: maybe this should be an exception
|
|
|
|
|
|
|
|
CSMSettings::UserSettings &userSettings = CSMSettings::UserSettings::instance();
|
|
|
|
if(!(userSettings.setting("debug/mouse-picking", QString("false")) == "true" ? true : false))
|
|
|
|
{
|
|
|
|
std::cerr << "Turn on mouse-picking debug option to see collision shapes." << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEngine->toggleDebugRendering();
|
|
|
|
mEngine->stepSimulation(0.0167); // DebugDrawer::step() not directly accessible
|
|
|
|
}
|
2014-10-17 19:16:48 +00:00
|
|
|
}
|