2012-07-03 10:30:50 +00:00
|
|
|
#include "physicssystem.hpp"
|
|
|
|
|
2011-10-29 07:50:11 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
#include <OgreRoot.h>
|
|
|
|
#include <OgreRenderWindow.h>
|
|
|
|
#include <OgreSceneManager.h>
|
|
|
|
#include <OgreViewport.h>
|
|
|
|
#include <OgreCamera.h>
|
|
|
|
#include <OgreTextureManager.h>
|
2014-02-19 17:40:29 +00:00
|
|
|
#include <OgreSceneNode.h>
|
2011-08-01 13:55:36 +00:00
|
|
|
|
2015-05-02 22:39:01 +00:00
|
|
|
#include <osg/Group>
|
|
|
|
|
2013-02-07 20:11:10 +00:00
|
|
|
#include <openengine/bullet/trace.h>
|
|
|
|
#include <openengine/bullet/physic.hpp>
|
2015-04-01 15:02:15 +00:00
|
|
|
//#include <openengine/bullet/BulletShapeLoader.h>
|
2013-02-07 05:44:58 +00:00
|
|
|
|
2013-02-24 21:52:23 +00:00
|
|
|
#include <components/nifbullet/bulletnifloader.hpp>
|
2015-01-12 10:29:56 +00:00
|
|
|
#include <components/misc/resourcehelpers.hpp>
|
2011-08-01 13:55:36 +00:00
|
|
|
|
2014-02-23 19:11:05 +00:00
|
|
|
#include <components/esm/loadgmst.hpp>
|
|
|
|
|
2013-08-03 10:16:51 +00:00
|
|
|
#include "../mwbase/world.hpp" // FIXME
|
2012-11-05 18:40:02 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2011-08-01 13:55:36 +00:00
|
|
|
|
2013-12-27 20:21:18 +00:00
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2014-09-17 00:20:46 +00:00
|
|
|
#include "../mwmechanics/movement.hpp"
|
2013-12-27 20:21:18 +00:00
|
|
|
|
2013-08-03 10:16:51 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/cellstore.hpp"
|
2013-08-03 10:16:51 +00:00
|
|
|
|
2015-05-02 22:39:01 +00:00
|
|
|
#include "../mwrender/bulletdebugdraw.hpp"
|
|
|
|
|
2015-04-01 15:02:15 +00:00
|
|
|
//#include "../apps/openmw/mwrender/animation.hpp"
|
2014-06-23 18:43:24 +00:00
|
|
|
#include "../apps/openmw/mwbase/world.hpp"
|
|
|
|
#include "../apps/openmw/mwbase/environment.hpp"
|
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "ptr.hpp"
|
2012-07-24 14:52:08 +00:00
|
|
|
#include "class.hpp"
|
2012-03-20 00:03:48 +00:00
|
|
|
|
2011-10-20 22:15:30 +00:00
|
|
|
using namespace Ogre;
|
2014-06-23 18:43:24 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2014-09-28 16:02:57 +00:00
|
|
|
void animateCollisionShapes (std::map<OEngine::Physic::RigidBody*, OEngine::Physic::AnimatedShapeInstance>& map, btDynamicsWorld* dynamicsWorld)
|
2014-06-23 18:43:24 +00:00
|
|
|
{
|
2015-04-01 15:02:15 +00:00
|
|
|
/*
|
2014-06-23 18:43:24 +00:00
|
|
|
for (std::map<OEngine::Physic::RigidBody*, OEngine::Physic::AnimatedShapeInstance>::iterator it = map.begin();
|
|
|
|
it != map.end(); ++it)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = MWBase::Environment::get().getWorld()->searchPtrViaHandle(it->first->mName);
|
2014-06-25 12:52:12 +00:00
|
|
|
if (ptr.isEmpty()) // Shouldn't happen
|
|
|
|
throw std::runtime_error("can't find Ptr");
|
|
|
|
|
2014-06-23 18:43:24 +00:00
|
|
|
MWRender::Animation* animation = MWBase::Environment::get().getWorld()->getAnimation(ptr);
|
2015-01-12 10:29:56 +00:00
|
|
|
if (!animation)
|
|
|
|
continue;
|
2014-06-23 18:43:24 +00:00
|
|
|
|
|
|
|
OEngine::Physic::AnimatedShapeInstance& instance = it->second;
|
|
|
|
|
2015-01-12 10:29:56 +00:00
|
|
|
std::map<int, int>& shapes = instance.mAnimatedShapes;
|
|
|
|
for (std::map<int, int>::iterator shapeIt = shapes.begin();
|
2014-06-23 18:43:24 +00:00
|
|
|
shapeIt != shapes.end(); ++shapeIt)
|
|
|
|
{
|
|
|
|
|
2015-01-12 10:29:56 +00:00
|
|
|
const std::string& mesh = animation->getObjectRootName();
|
|
|
|
int boneHandle = NifOgre::NIFSkeletonLoader::lookupOgreBoneHandle(mesh, shapeIt->first);
|
|
|
|
Ogre::Node* bone = animation->getNode(boneHandle);
|
2014-06-28 16:20:57 +00:00
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
if (bone == NULL)
|
2015-01-12 10:29:56 +00:00
|
|
|
continue;
|
2014-06-26 01:33:09 +00:00
|
|
|
|
2015-01-15 00:23:58 +00:00
|
|
|
btCompoundShape* compound = static_cast<btCompoundShape*>(instance.mCompound);
|
2014-06-23 18:43:24 +00:00
|
|
|
|
|
|
|
btTransform trans;
|
2015-01-14 16:59:04 +00:00
|
|
|
trans.setOrigin(BtOgre::Convert::toBullet(bone->_getDerivedPosition()) * compound->getLocalScaling());
|
2014-06-23 18:43:24 +00:00
|
|
|
trans.setRotation(BtOgre::Convert::toBullet(bone->_getDerivedOrientation()));
|
|
|
|
|
2015-01-14 16:59:04 +00:00
|
|
|
compound->getChildShape(shapeIt->second)->setLocalScaling(
|
|
|
|
compound->getLocalScaling() *
|
|
|
|
BtOgre::Convert::toBullet(bone->_getDerivedScale()));
|
2014-06-23 18:43:24 +00:00
|
|
|
compound->updateChildTransform(shapeIt->second, trans);
|
|
|
|
}
|
2014-09-28 16:02:57 +00:00
|
|
|
|
|
|
|
// needed because we used btDynamicsWorld::setForceUpdateAllAabbs(false)
|
|
|
|
dynamicsWorld->updateSingleAabb(it->first);
|
2014-06-23 18:43:24 +00:00
|
|
|
}
|
2015-04-01 15:02:15 +00:00
|
|
|
*/
|
2014-06-23 18:43:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-02 22:50:56 +00:00
|
|
|
namespace BtOgre
|
|
|
|
{
|
|
|
|
//Converts from and to Bullet and Ogre stuff. Pretty self-explanatory.
|
|
|
|
class Convert
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Convert() {};
|
|
|
|
~Convert() {};
|
|
|
|
|
|
|
|
static btQuaternion toBullet(const Ogre::Quaternion &q)
|
|
|
|
{
|
|
|
|
return btQuaternion(q.x, q.y, q.z, q.w);
|
|
|
|
}
|
|
|
|
static btVector3 toBullet(const Ogre::Vector3 &v)
|
|
|
|
{
|
|
|
|
return btVector3(v.x, v.y, v.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Ogre::Quaternion toOgre(const btQuaternion &q)
|
|
|
|
{
|
|
|
|
return Ogre::Quaternion(q.w(), q.x(), q.y(), q.z());
|
|
|
|
}
|
|
|
|
static Ogre::Vector3 toOgre(const btVector3 &v)
|
|
|
|
{
|
|
|
|
return Ogre::Vector3(v.x(), v.y(), v.z());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:43:24 +00:00
|
|
|
|
2011-08-01 13:55:36 +00:00
|
|
|
namespace MWWorld
|
|
|
|
{
|
|
|
|
|
2014-05-18 02:05:08 +00:00
|
|
|
static const float sMaxSlope = 49.0f;
|
2014-12-21 15:56:14 +00:00
|
|
|
static const float sStepSizeUp = 34.0f;
|
|
|
|
static const float sStepSizeDown = 62.0f;
|
|
|
|
|
2013-02-07 21:18:16 +00:00
|
|
|
// Arbitrary number. To prevent infinite loops. They shouldn't happen but it's good to be prepared.
|
2013-08-20 11:41:52 +00:00
|
|
|
static const int sMaxIterations = 8;
|
2013-02-05 20:45:10 +00:00
|
|
|
|
|
|
|
class MovementSolver
|
|
|
|
{
|
|
|
|
private:
|
2013-08-17 08:11:57 +00:00
|
|
|
static float getSlope(const Ogre::Vector3 &normal)
|
|
|
|
{
|
|
|
|
return normal.angleBetween(Ogre::Vector3(0.0f,0.0f,1.0f)).valueDegrees();
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:51:19 +00:00
|
|
|
static bool stepMove(btCollisionObject *colobj, Ogre::Vector3 &position,
|
2015-01-01 23:34:16 +00:00
|
|
|
const Ogre::Vector3 &toMove, float &remainingTime,
|
2013-02-05 20:45:10 +00:00
|
|
|
OEngine::Physic::PhysicEngine *engine)
|
|
|
|
{
|
2014-03-23 02:24:04 +00:00
|
|
|
/*
|
|
|
|
* Slide up an incline or set of stairs. Should be called only after a
|
|
|
|
* collision detection otherwise unnecessary tracing will be performed.
|
|
|
|
*
|
|
|
|
* NOTE: with a small change this method can be used to step over an obstacle
|
|
|
|
* of height sStepSize.
|
|
|
|
*
|
|
|
|
* If successful return 'true' and update 'position' to the new possible
|
|
|
|
* location and adjust 'remainingTime'.
|
|
|
|
*
|
|
|
|
* If not successful return 'false'. May fail for these reasons:
|
|
|
|
* - can't move directly up from current position
|
|
|
|
* - having moved up by between epsilon() and sStepSize, can't move forward
|
2015-01-01 23:34:16 +00:00
|
|
|
* - having moved forward by between epsilon() and toMove,
|
2014-03-23 02:24:04 +00:00
|
|
|
* = moved down between 0 and just under sStepSize but slope was too steep, or
|
|
|
|
* = moved the full sStepSize down (FIXME: this could be a bug)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Starting position. Obstacle or stairs with height upto sStepSize in front.
|
|
|
|
*
|
|
|
|
* +--+ +--+ |XX
|
2015-01-01 23:34:16 +00:00
|
|
|
* | | -------> toMove | | +--+XX
|
2014-03-23 02:24:04 +00:00
|
|
|
* | | | | |XXXXX
|
|
|
|
* | | +--+ | | +--+XXXXX
|
|
|
|
* | | |XX| | | |XXXXXXXX
|
|
|
|
* +--+ +--+ +--+ +--------
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try moving up sStepSize using stepper.
|
|
|
|
* FIXME: does not work in case there is no front obstacle but there is one above
|
|
|
|
*
|
|
|
|
* +--+ +--+
|
|
|
|
* | | | |
|
|
|
|
* | | | | |XX
|
|
|
|
* | | | | +--+XX
|
|
|
|
* | | | | |XXXXX
|
|
|
|
* +--+ +--+ +--+ +--+XXXXX
|
|
|
|
* |XX| |XXXXXXXX
|
|
|
|
* +--+ +--------
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2013-08-18 15:24:39 +00:00
|
|
|
OEngine::Physic::ActorTracer tracer, stepper;
|
|
|
|
|
2014-12-21 15:56:14 +00:00
|
|
|
stepper.doTrace(colobj, position, position+Ogre::Vector3(0.0f,0.0f,sStepSizeUp), engine);
|
2013-08-25 18:04:55 +00:00
|
|
|
if(stepper.mFraction < std::numeric_limits<float>::epsilon())
|
2014-03-23 02:24:04 +00:00
|
|
|
return false; // didn't even move the smallest representable amount
|
|
|
|
// (TODO: shouldn't this be larger? Why bother with such a small amount?)
|
2013-02-20 15:51:36 +00:00
|
|
|
|
2014-03-23 02:24:04 +00:00
|
|
|
/*
|
|
|
|
* Try moving from the elevated position using tracer.
|
|
|
|
*
|
|
|
|
* +--+ +--+
|
|
|
|
* | | |YY| FIXME: collision with object YY
|
|
|
|
* | | +--+
|
|
|
|
* | |
|
|
|
|
* <------------------->| |
|
|
|
|
* +--+ +--+
|
2015-01-01 23:34:16 +00:00
|
|
|
* |XX| the moved amount is toMove*tracer.mFraction
|
2014-03-23 02:24:04 +00:00
|
|
|
* +--+
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2015-01-01 23:34:16 +00:00
|
|
|
tracer.doTrace(colobj, stepper.mEndPos, stepper.mEndPos + toMove, engine);
|
2013-08-25 18:04:55 +00:00
|
|
|
if(tracer.mFraction < std::numeric_limits<float>::epsilon())
|
2014-03-23 02:24:04 +00:00
|
|
|
return false; // didn't even move the smallest representable amount
|
2013-02-05 20:45:10 +00:00
|
|
|
|
2014-03-23 02:24:04 +00:00
|
|
|
/*
|
2014-12-21 15:56:14 +00:00
|
|
|
* Try moving back down sStepSizeDown using stepper.
|
2014-03-23 02:24:04 +00:00
|
|
|
* NOTE: if there is an obstacle below (e.g. stairs), we'll be "stepping up".
|
|
|
|
* Below diagram is the case where we "stepped over" an obstacle in front.
|
|
|
|
*
|
|
|
|
* +--+
|
|
|
|
* |YY|
|
|
|
|
* +--+ +--+
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* +--+ | |
|
|
|
|
* |XX| | |
|
|
|
|
* +--+ +--+
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2014-12-21 15:56:14 +00:00
|
|
|
stepper.doTrace(colobj, tracer.mEndPos, tracer.mEndPos-Ogre::Vector3(0.0f,0.0f,sStepSizeDown), engine);
|
2013-08-25 18:04:55 +00:00
|
|
|
if(stepper.mFraction < 1.0f && getSlope(stepper.mPlaneNormal) <= sMaxSlope)
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2014-12-16 19:44:42 +00:00
|
|
|
// don't allow stepping up other actors
|
|
|
|
if (stepper.mHitObject->getBroadphaseHandle()->m_collisionFilterGroup == OEngine::Physic::CollisionType_Actor)
|
|
|
|
return false;
|
2013-02-05 20:45:10 +00:00
|
|
|
// only step down onto semi-horizontal surfaces. don't step down onto the side of a house or a wall.
|
2014-03-23 02:24:04 +00:00
|
|
|
// TODO: stepper.mPlaneNormal does not appear to be reliable - needs more testing
|
|
|
|
// NOTE: caller's variables 'position' & 'remainingTime' are modified here
|
2013-08-18 15:24:39 +00:00
|
|
|
position = stepper.mEndPos;
|
2014-03-23 02:24:04 +00:00
|
|
|
remainingTime *= (1.0f-tracer.mFraction); // remaining time is proportional to remaining distance
|
2013-02-05 20:45:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-23 02:24:04 +00:00
|
|
|
// moved between 0 and just under sStepSize distance but slope was too great,
|
|
|
|
// or moved full sStepSize distance (FIXME: is this a bug?)
|
2013-02-05 20:45:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-17 08:11:57 +00:00
|
|
|
///Project a vector u on another vector v
|
|
|
|
static inline Ogre::Vector3 project(const Ogre::Vector3 u, const Ogre::Vector3 &v)
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2013-08-17 08:11:57 +00:00
|
|
|
return v * u.dotProduct(v);
|
2013-02-05 20:45:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:11:57 +00:00
|
|
|
///Helper for computing the character sliding
|
|
|
|
static inline Ogre::Vector3 slide(Ogre::Vector3 direction, const Ogre::Vector3 &planeNormal)
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2013-08-17 08:11:57 +00:00
|
|
|
return direction - project(direction, planeNormal);
|
2013-02-05 20:45:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:11:57 +00:00
|
|
|
|
2013-02-05 20:45:10 +00:00
|
|
|
public:
|
2014-06-28 12:59:33 +00:00
|
|
|
static Ogre::Vector3 traceDown(const MWWorld::Ptr &ptr, OEngine::Physic::PhysicEngine *engine, float maxHeight)
|
2013-04-03 21:55:57 +00:00
|
|
|
{
|
|
|
|
const ESM::Position &refpos = ptr.getRefData().getPosition();
|
|
|
|
Ogre::Vector3 position(refpos.pos);
|
|
|
|
|
|
|
|
OEngine::Physic::PhysicActor *physicActor = engine->getCharacter(ptr.getRefData().getHandle());
|
|
|
|
if (!physicActor)
|
|
|
|
return position;
|
|
|
|
|
2013-08-17 11:42:35 +00:00
|
|
|
OEngine::Physic::ActorTracer tracer;
|
2014-06-21 21:32:58 +00:00
|
|
|
tracer.findGround(physicActor, position, position-Ogre::Vector3(0,0,maxHeight), engine);
|
2013-08-17 11:42:35 +00:00
|
|
|
if(tracer.mFraction >= 1.0f)
|
2013-08-19 11:56:02 +00:00
|
|
|
{
|
|
|
|
physicActor->setOnGround(false);
|
2013-08-17 10:55:04 +00:00
|
|
|
return position;
|
2013-08-19 11:56:02 +00:00
|
|
|
}
|
2014-10-03 16:32:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check if we actually found a valid spawn point (use an infinitely thin ray this time).
|
|
|
|
// Required for some broken door destinations in Morrowind.esm, where the spawn point
|
|
|
|
// intersects with other geometry if the actor's base is taken into account
|
|
|
|
btVector3 from = BtOgre::Convert::toBullet(position);
|
|
|
|
btVector3 to = from - btVector3(0,0,maxHeight);
|
|
|
|
|
|
|
|
btCollisionWorld::ClosestRayResultCallback resultCallback1(from, to);
|
|
|
|
resultCallback1.m_collisionFilterGroup = 0xff;
|
|
|
|
resultCallback1.m_collisionFilterMask = OEngine::Physic::CollisionType_World|OEngine::Physic::CollisionType_HeightMap;
|
|
|
|
|
|
|
|
engine->mDynamicsWorld->rayTest(from, to, resultCallback1);
|
|
|
|
if (resultCallback1.hasHit() &&
|
|
|
|
(BtOgre::Convert::toOgre(resultCallback1.m_hitPointWorld).distance(tracer.mEndPos) > 30
|
|
|
|
|| getSlope(tracer.mPlaneNormal) > sMaxSlope))
|
|
|
|
{
|
|
|
|
physicActor->setOnGround(getSlope(BtOgre::Convert::toOgre(resultCallback1.m_hitNormalWorld)) <= sMaxSlope);
|
|
|
|
return BtOgre::Convert::toOgre(resultCallback1.m_hitPointWorld) + Ogre::Vector3(0,0,1.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
physicActor->setOnGround(getSlope(tracer.mPlaneNormal) <= sMaxSlope);
|
|
|
|
|
|
|
|
return tracer.mEndPos;
|
|
|
|
}
|
2013-04-03 21:55:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-07 21:18:16 +00:00
|
|
|
static Ogre::Vector3 move(const MWWorld::Ptr &ptr, const Ogre::Vector3 &movement, float time,
|
2014-07-29 17:01:40 +00:00
|
|
|
bool isFlying, float waterlevel, float slowFall, OEngine::Physic::PhysicEngine *engine
|
|
|
|
, std::map<std::string, std::string>& collisionTracker
|
|
|
|
, std::map<std::string, std::string>& standingCollisionTracker)
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2013-02-05 22:02:14 +00:00
|
|
|
const ESM::Position &refpos = ptr.getRefData().getPosition();
|
|
|
|
Ogre::Vector3 position(refpos.pos);
|
|
|
|
|
2014-06-10 00:15:09 +00:00
|
|
|
// Early-out for totally static creatures
|
|
|
|
// (Not sure if gravity should still apply?)
|
2015-01-09 08:40:53 +00:00
|
|
|
if (!ptr.getClass().isMobile(ptr))
|
2014-06-10 00:15:09 +00:00
|
|
|
return position;
|
|
|
|
|
2013-02-05 20:45:10 +00:00
|
|
|
OEngine::Physic::PhysicActor *physicActor = engine->getCharacter(ptr.getRefData().getHandle());
|
2014-12-18 02:24:10 +00:00
|
|
|
if (!physicActor)
|
|
|
|
return position;
|
|
|
|
|
2014-10-05 20:24:11 +00:00
|
|
|
// Reset per-frame data
|
|
|
|
physicActor->setWalkingOnWater(false);
|
2015-01-13 02:11:29 +00:00
|
|
|
// Anything to collide with?
|
2014-12-18 02:24:10 +00:00
|
|
|
if(!physicActor->getCollisionMode())
|
2013-02-06 20:39:26 +00:00
|
|
|
{
|
2014-03-07 05:11:00 +00:00
|
|
|
return position + (Ogre::Quaternion(Ogre::Radian(refpos.rot[2]), Ogre::Vector3::NEGATIVE_UNIT_Z) *
|
|
|
|
Ogre::Quaternion(Ogre::Radian(refpos.rot[0]), Ogre::Vector3::NEGATIVE_UNIT_X))
|
|
|
|
* movement * time;
|
2013-02-06 20:39:26 +00:00
|
|
|
}
|
2013-02-05 20:45:10 +00:00
|
|
|
|
2013-08-17 09:51:19 +00:00
|
|
|
btCollisionObject *colobj = physicActor->getCollisionBody();
|
|
|
|
Ogre::Vector3 halfExtents = physicActor->getHalfExtents();
|
2014-03-16 09:09:18 +00:00
|
|
|
position.z += halfExtents.z;
|
2014-03-16 09:56:11 +00:00
|
|
|
|
2015-01-31 19:54:08 +00:00
|
|
|
static const float fSwimHeightScale = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
|
|
|
.find("fSwimHeightScale")->getFloat();
|
|
|
|
float swimlevel = waterlevel + halfExtents.z - (halfExtents.z * 2 * fSwimHeightScale);
|
2013-08-21 14:06:07 +00:00
|
|
|
|
2013-08-17 11:42:35 +00:00
|
|
|
OEngine::Physic::ActorTracer tracer;
|
2015-01-13 02:11:29 +00:00
|
|
|
Ogre::Vector3 inertia = physicActor->getInertialForce();
|
2013-02-07 21:18:16 +00:00
|
|
|
Ogre::Vector3 velocity;
|
2014-03-15 21:09:14 +00:00
|
|
|
|
2015-01-31 19:54:08 +00:00
|
|
|
if(position.z < swimlevel || isFlying)
|
2013-02-06 20:39:26 +00:00
|
|
|
{
|
2014-03-07 05:11:00 +00:00
|
|
|
velocity = (Ogre::Quaternion(Ogre::Radian(refpos.rot[2]), Ogre::Vector3::NEGATIVE_UNIT_Z)*
|
|
|
|
Ogre::Quaternion(Ogre::Radian(refpos.rot[0]), Ogre::Vector3::NEGATIVE_UNIT_X)) * movement;
|
2013-02-06 20:39:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-16 09:09:18 +00:00
|
|
|
velocity = Ogre::Quaternion(Ogre::Radian(refpos.rot[2]), Ogre::Vector3::NEGATIVE_UNIT_Z) * movement;
|
2014-09-17 00:20:46 +00:00
|
|
|
|
2015-01-13 02:11:29 +00:00
|
|
|
if (velocity.z > 0.f)
|
|
|
|
inertia = velocity;
|
|
|
|
if(!physicActor->getOnGround())
|
2013-08-19 15:09:23 +00:00
|
|
|
{
|
2015-01-13 02:11:29 +00:00
|
|
|
velocity = velocity + physicActor->getInertialForce();
|
2013-08-19 15:09:23 +00:00
|
|
|
}
|
2013-02-06 20:39:26 +00:00
|
|
|
}
|
2014-09-17 00:20:46 +00:00
|
|
|
ptr.getClass().getMovementSettings(ptr).mPosition[2] = 0;
|
2013-02-06 20:39:26 +00:00
|
|
|
|
2014-06-26 18:26:46 +00:00
|
|
|
// Now that we have the effective movement vector, apply wind forces to it
|
|
|
|
if (MWBase::Environment::get().getWorld()->isInStorm())
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2014-06-26 18:26:46 +00:00
|
|
|
Ogre::Vector3 stormDirection = MWBase::Environment::get().getWorld()->getStormDirection();
|
|
|
|
Ogre::Degree angle = stormDirection.angleBetween(velocity);
|
|
|
|
static const float fStromWalkMult = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
|
|
|
.find("fStromWalkMult")->getFloat();
|
|
|
|
velocity *= 1.f-(fStromWalkMult * (angle.valueDegrees()/180.f));
|
2013-02-05 20:45:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 23:58:12 +00:00
|
|
|
Ogre::Vector3 origVelocity = velocity;
|
|
|
|
|
2014-03-16 09:09:18 +00:00
|
|
|
Ogre::Vector3 newPosition = position;
|
2014-03-15 21:09:14 +00:00
|
|
|
/*
|
|
|
|
* A loop to find newPosition using tracer, if successful different from the starting position.
|
|
|
|
* nextpos is the local variable used to find potential newPosition, using velocity and remainingTime
|
|
|
|
* The initial velocity was set earlier (see above).
|
|
|
|
*/
|
2013-08-17 08:11:57 +00:00
|
|
|
float remainingTime = time;
|
2014-03-15 21:09:14 +00:00
|
|
|
for(int iterations = 0; iterations < sMaxIterations && remainingTime > 0.01f; ++iterations)
|
2013-08-17 08:11:57 +00:00
|
|
|
{
|
2014-03-15 21:09:14 +00:00
|
|
|
Ogre::Vector3 nextpos = newPosition + velocity * remainingTime;
|
|
|
|
|
2014-05-03 09:33:20 +00:00
|
|
|
// If not able to fly, don't allow to swim up into the air
|
2015-01-31 19:54:08 +00:00
|
|
|
if(newPosition.z < swimlevel &&
|
2014-03-15 21:09:14 +00:00
|
|
|
!isFlying && // can't fly
|
2015-01-31 19:54:08 +00:00
|
|
|
nextpos.z > swimlevel && // but about to go above water
|
|
|
|
newPosition.z <= swimlevel)
|
2013-08-21 14:06:07 +00:00
|
|
|
{
|
|
|
|
const Ogre::Vector3 down(0,0,-1);
|
|
|
|
Ogre::Real movelen = velocity.normalise();
|
|
|
|
Ogre::Vector3 reflectdir = velocity.reflect(down);
|
|
|
|
reflectdir.normalise();
|
|
|
|
velocity = slide(reflectdir, down)*movelen;
|
2014-03-16 09:56:11 +00:00
|
|
|
// NOTE: remainingTime is unchanged before the loop continues
|
2014-03-15 21:09:14 +00:00
|
|
|
continue; // velocity updated, calculate nextpos again
|
2013-08-21 14:06:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 06:39:42 +00:00
|
|
|
if(newPosition.squaredDistance(nextpos) > 0.0001)
|
2014-06-08 00:26:12 +00:00
|
|
|
{
|
|
|
|
// trace to where character would go if there were no obstructions
|
|
|
|
tracer.doTrace(colobj, newPosition, nextpos, engine);
|
2013-02-05 20:45:10 +00:00
|
|
|
|
2014-06-08 00:26:12 +00:00
|
|
|
// check for obstructions
|
|
|
|
if(tracer.mFraction >= 1.0f)
|
|
|
|
{
|
|
|
|
newPosition = tracer.mEndPos; // ok to move, so set newPosition
|
|
|
|
break;
|
|
|
|
}
|
2014-07-29 17:01:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const btCollisionObject* standingOn = tracer.mHitObject;
|
|
|
|
if (const OEngine::Physic::RigidBody* body = dynamic_cast<const OEngine::Physic::RigidBody*>(standingOn))
|
|
|
|
{
|
|
|
|
collisionTracker[ptr.getRefData().getHandle()] = body->mName;
|
|
|
|
}
|
|
|
|
}
|
2014-06-08 00:26:12 +00:00
|
|
|
}
|
|
|
|
else
|
2013-02-05 20:45:10 +00:00
|
|
|
{
|
2014-06-08 00:26:12 +00:00
|
|
|
// The current position and next position are nearly the same, so just exit.
|
|
|
|
// Note: Bullet can trigger an assert in debug modes if the positions
|
|
|
|
// are the same, since that causes it to attempt to normalize a zero
|
|
|
|
// length vector (which can also happen with nearly identical vectors, since
|
|
|
|
// precision can be lost due to any math Bullet does internally). Since we
|
|
|
|
// aren't performing any collision detection, we want to reject the next
|
|
|
|
// position, so that we don't slowly move inside another object.
|
2013-08-17 08:11:57 +00:00
|
|
|
break;
|
2013-02-05 20:45:10 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 00:26:12 +00:00
|
|
|
|
2014-04-11 11:24:00 +00:00
|
|
|
Ogre::Vector3 oldPosition = newPosition;
|
2014-03-23 02:24:04 +00:00
|
|
|
// We hit something. Try to step up onto it. (NOTE: stepMove does not allow stepping over)
|
2014-04-11 11:24:00 +00:00
|
|
|
// NOTE: stepMove modifies newPosition if successful
|
2015-01-01 23:34:16 +00:00
|
|
|
bool result = stepMove(colobj, newPosition, velocity*remainingTime, remainingTime, engine);
|
|
|
|
if (!result) // to make sure the maximum stepping distance isn't framerate-dependent or movement-speed dependent
|
|
|
|
result = stepMove(colobj, newPosition, velocity.normalisedCopy()*10.f, remainingTime, engine);
|
2014-12-30 14:46:33 +00:00
|
|
|
if(result)
|
2014-04-11 11:24:00 +00:00
|
|
|
{
|
2014-04-27 12:59:21 +00:00
|
|
|
// don't let pure water creatures move out of water after stepMove
|
2015-01-09 08:40:53 +00:00
|
|
|
if (ptr.getClass().isPureWaterCreature(ptr)
|
2015-01-31 19:54:08 +00:00
|
|
|
&& newPosition.z + halfExtents.z > waterlevel)
|
2014-04-11 11:24:00 +00:00
|
|
|
newPosition = oldPosition;
|
|
|
|
}
|
2013-08-17 08:11:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Can't move this way, try to find another spot along the plane
|
2015-01-14 23:58:12 +00:00
|
|
|
Ogre::Vector3 direction = velocity;
|
|
|
|
Ogre::Real movelen = direction.normalise();
|
2013-08-17 11:42:35 +00:00
|
|
|
Ogre::Vector3 reflectdir = velocity.reflect(tracer.mPlaneNormal);
|
2013-08-17 08:11:57 +00:00
|
|
|
reflectdir.normalise();
|
2015-01-14 23:58:12 +00:00
|
|
|
|
|
|
|
Ogre::Vector3 newVelocity = slide(reflectdir, tracer.mPlaneNormal)*movelen;
|
|
|
|
if ((newVelocity-velocity).squaredLength() < 0.01)
|
|
|
|
break;
|
|
|
|
if (velocity.dotProduct(origVelocity) <= 0.f)
|
|
|
|
break;
|
|
|
|
|
|
|
|
velocity = newVelocity;
|
2013-08-17 08:11:57 +00:00
|
|
|
|
|
|
|
// Do not allow sliding upward if there is gravity. Stepping will have taken
|
|
|
|
// care of that.
|
2015-01-31 19:54:08 +00:00
|
|
|
if(!(newPosition.z < swimlevel || isFlying))
|
2013-08-17 08:11:57 +00:00
|
|
|
velocity.z = std::min(velocity.z, 0.0f);
|
|
|
|
}
|
|
|
|
}
|
2013-02-05 20:45:10 +00:00
|
|
|
|
2014-10-05 20:59:24 +00:00
|
|
|
bool isOnGround = false;
|
2015-01-31 19:54:08 +00:00
|
|
|
if (!(inertia.z > 0.f) && !(newPosition.z < swimlevel))
|
2013-02-15 02:34:51 +00:00
|
|
|
{
|
2014-09-29 20:30:21 +00:00
|
|
|
Ogre::Vector3 from = newPosition;
|
2014-10-05 20:59:24 +00:00
|
|
|
Ogre::Vector3 to = newPosition - (physicActor->getOnGround() ?
|
2014-12-21 15:56:14 +00:00
|
|
|
Ogre::Vector3(0,0,sStepSizeDown+2.f) : Ogre::Vector3(0,0,2.f));
|
2014-09-29 20:30:21 +00:00
|
|
|
tracer.doTrace(colobj, from, to, engine);
|
2014-12-16 22:18:41 +00:00
|
|
|
if(tracer.mFraction < 1.0f && getSlope(tracer.mPlaneNormal) <= sMaxSlope
|
|
|
|
&& tracer.mHitObject->getBroadphaseHandle()->m_collisionFilterGroup != OEngine::Physic::CollisionType_Actor)
|
2013-08-19 15:09:23 +00:00
|
|
|
{
|
2014-09-29 20:30:21 +00:00
|
|
|
const btCollisionObject* standingOn = tracer.mHitObject;
|
|
|
|
if (const OEngine::Physic::RigidBody* body = dynamic_cast<const OEngine::Physic::RigidBody*>(standingOn))
|
|
|
|
{
|
|
|
|
standingCollisionTracker[ptr.getRefData().getHandle()] = body->mName;
|
|
|
|
}
|
2014-10-05 20:24:11 +00:00
|
|
|
if (standingOn->getBroadphaseHandle()->m_collisionFilterGroup == OEngine::Physic::CollisionType_Water)
|
|
|
|
physicActor->setWalkingOnWater(true);
|
2014-09-29 20:30:21 +00:00
|
|
|
|
2014-10-05 20:59:24 +00:00
|
|
|
if (!isFlying)
|
|
|
|
newPosition.z = tracer.mEndPos.z + 1.0f;
|
2014-09-29 20:30:21 +00:00
|
|
|
|
2013-08-19 15:09:23 +00:00
|
|
|
isOnGround = true;
|
|
|
|
}
|
2013-02-15 02:34:51 +00:00
|
|
|
else
|
2014-12-16 22:18:41 +00:00
|
|
|
{
|
|
|
|
// standing on actors is not allowed (see above).
|
|
|
|
// in addition to that, apply a sliding effect away from the center of the actor,
|
|
|
|
// so that we do not stay suspended in air indefinitely.
|
|
|
|
if (tracer.mFraction < 1.0f && tracer.mHitObject->getBroadphaseHandle()->m_collisionFilterGroup == OEngine::Physic::CollisionType_Actor)
|
|
|
|
{
|
2015-01-13 02:11:29 +00:00
|
|
|
if (Ogre::Vector3(velocity.x, velocity.y, 0).squaredLength() < 100.f*100.f)
|
2014-12-16 22:18:41 +00:00
|
|
|
{
|
|
|
|
btVector3 aabbMin, aabbMax;
|
|
|
|
tracer.mHitObject->getCollisionShape()->getAabb(tracer.mHitObject->getWorldTransform(), aabbMin, aabbMax);
|
|
|
|
btVector3 center = (aabbMin + aabbMax) / 2.f;
|
|
|
|
inertia = Ogre::Vector3(position.x - center.x(), position.y - center.y(), 0);
|
|
|
|
inertia.normalise();
|
|
|
|
inertia *= 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-18 12:38:50 +00:00
|
|
|
isOnGround = false;
|
2014-12-16 22:18:41 +00:00
|
|
|
}
|
2013-02-15 02:34:51 +00:00
|
|
|
}
|
2013-08-17 08:11:57 +00:00
|
|
|
|
2015-01-31 19:54:08 +00:00
|
|
|
if(isOnGround || newPosition.z < swimlevel || isFlying)
|
2013-08-18 12:38:50 +00:00
|
|
|
physicActor->setInertialForce(Ogre::Vector3(0.0f));
|
|
|
|
else
|
|
|
|
{
|
2014-11-10 07:42:44 +00:00
|
|
|
inertia.z += time * -627.2f;
|
2014-01-17 14:47:34 +00:00
|
|
|
if (inertia.z < 0)
|
2014-11-10 07:42:44 +00:00
|
|
|
inertia.z *= slowFall;
|
2013-08-18 12:38:50 +00:00
|
|
|
physicActor->setInertialForce(inertia);
|
|
|
|
}
|
2013-08-18 15:24:39 +00:00
|
|
|
physicActor->setOnGround(isOnGround);
|
2013-08-17 08:11:57 +00:00
|
|
|
|
2015-01-13 02:11:29 +00:00
|
|
|
newPosition.z -= halfExtents.z; // remove what was added at the beginning
|
2013-02-05 20:45:10 +00:00
|
|
|
return newPosition;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-02 22:39:01 +00:00
|
|
|
PhysicsSystem::PhysicsSystem(osg::ref_ptr<osg::Group> parentNode) :
|
|
|
|
mEngine(0), mTimeAccum(0.0f), mWaterEnabled(false), mWaterHeight(0), mDebugDrawEnabled(false), mParentNode(parentNode)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-05-08 23:06:55 +00:00
|
|
|
mEngine = new OEngine::Physic::PhysicEngine;
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
2011-08-22 19:34:51 +00:00
|
|
|
|
2011-08-01 13:55:36 +00:00
|
|
|
PhysicsSystem::~PhysicsSystem()
|
|
|
|
{
|
2015-05-02 14:39:03 +00:00
|
|
|
if (mWaterCollisionObject.get())
|
|
|
|
mEngine->mDynamicsWorld->removeCollisionObject(mWaterCollisionObject.get());
|
|
|
|
delete mEngine;
|
2012-01-29 15:31:18 +00:00
|
|
|
}
|
2013-02-07 20:11:10 +00:00
|
|
|
|
2015-05-02 22:39:01 +00:00
|
|
|
bool PhysicsSystem::toggleDebugRendering()
|
|
|
|
{
|
|
|
|
mDebugDrawEnabled = !mDebugDrawEnabled;
|
|
|
|
|
|
|
|
if (mDebugDrawEnabled && !mDebugDrawer.get())
|
|
|
|
{
|
|
|
|
mDebugDrawer.reset(new MWRender::DebugDrawer(mParentNode, mEngine->mDynamicsWorld));
|
|
|
|
mEngine->mDynamicsWorld->setDebugDrawer(mDebugDrawer.get());
|
|
|
|
mDebugDrawer->setDebugMode(mDebugDrawEnabled);
|
|
|
|
}
|
|
|
|
else if (mDebugDrawer.get())
|
|
|
|
mDebugDrawer->setDebugMode(mDebugDrawEnabled);
|
|
|
|
return mDebugDrawEnabled;
|
|
|
|
}
|
|
|
|
|
2012-01-29 15:31:18 +00:00
|
|
|
OEngine::Physic::PhysicEngine* PhysicsSystem::getEngine()
|
|
|
|
{
|
|
|
|
return mEngine;
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
2012-04-30 21:55:22 +00:00
|
|
|
|
2013-08-23 14:01:30 +00:00
|
|
|
std::pair<std::string,Ogre::Vector3> PhysicsSystem::getHitContact(const std::string &name,
|
2013-08-23 19:25:57 +00:00
|
|
|
const Ogre::Vector3 &origin,
|
|
|
|
const Ogre::Quaternion &orient,
|
2013-08-23 14:01:30 +00:00
|
|
|
float queryDistance)
|
2013-07-25 19:58:43 +00:00
|
|
|
{
|
2013-08-23 19:25:57 +00:00
|
|
|
const MWWorld::Store<ESM::GameSetting> &store = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
|
|
|
|
|
|
|
|
btConeShape shape(Ogre::Degree(store.find("fCombatAngleXY")->getFloat()/2.0f).valueRadians(),
|
|
|
|
queryDistance);
|
|
|
|
shape.setLocalScaling(btVector3(1, 1, Ogre::Degree(store.find("fCombatAngleZ")->getFloat()/2.0f).valueRadians() /
|
|
|
|
shape.getRadius()));
|
|
|
|
|
|
|
|
// The shape origin is its center, so we have to move it forward by half the length. The
|
|
|
|
// real origin will be provided to getFilteredContact to find the closest.
|
|
|
|
Ogre::Vector3 center = origin + (orient * Ogre::Vector3(0.0f, queryDistance*0.5f, 0.0f));
|
|
|
|
|
|
|
|
btCollisionObject object;
|
|
|
|
object.setCollisionShape(&shape);
|
|
|
|
object.setWorldTransform(btTransform(btQuaternion(orient.x, orient.y, orient.z, orient.w),
|
|
|
|
btVector3(center.x, center.y, center.z)));
|
|
|
|
|
|
|
|
std::pair<const OEngine::Physic::RigidBody*,btVector3> result = mEngine->getFilteredContact(
|
|
|
|
name, btVector3(origin.x, origin.y, origin.z), &object);
|
|
|
|
if(!result.first)
|
|
|
|
return std::make_pair(std::string(), Ogre::Vector3(&result.second[0]));
|
|
|
|
return std::make_pair(result.first->mName, Ogre::Vector3(&result.second[0]));
|
2013-07-25 19:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 19:43:21 +00:00
|
|
|
bool PhysicsSystem::castRay(const Vector3& from, const Vector3& to, bool ignoreHeightMap)
|
2012-02-24 15:12:43 +00:00
|
|
|
{
|
|
|
|
btVector3 _from, _to;
|
|
|
|
_from = btVector3(from.x, from.y, from.z);
|
|
|
|
_to = btVector3(to.x, to.y, to.z);
|
2012-04-30 21:55:22 +00:00
|
|
|
|
2015-05-01 19:43:21 +00:00
|
|
|
std::pair<std::string, float> result = mEngine->rayTest(_from, _to,ignoreHeightMap);
|
2012-02-24 15:12:43 +00:00
|
|
|
return !(result.first == "");
|
|
|
|
}
|
2011-10-20 22:15:30 +00:00
|
|
|
|
2012-07-25 16:25:53 +00:00
|
|
|
std::pair<bool, Ogre::Vector3>
|
|
|
|
PhysicsSystem::castRay(const Ogre::Vector3 &orig, const Ogre::Vector3 &dir, float len)
|
|
|
|
{
|
|
|
|
Ogre::Ray ray = Ogre::Ray(orig, dir);
|
|
|
|
Ogre::Vector3 to = ray.getPoint(len);
|
|
|
|
|
|
|
|
btVector3 btFrom = btVector3(orig.x, orig.y, orig.z);
|
|
|
|
btVector3 btTo = btVector3(to.x, to.y, to.z);
|
|
|
|
|
|
|
|
std::pair<std::string, float> test = mEngine->rayTest(btFrom, btTo);
|
2013-04-03 21:55:57 +00:00
|
|
|
if (test.second == -1) {
|
2012-07-25 16:25:53 +00:00
|
|
|
return std::make_pair(false, Ogre::Vector3());
|
|
|
|
}
|
|
|
|
return std::make_pair(true, ray.getPoint(len * test.second));
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:43:06 +00:00
|
|
|
std::vector<std::string> PhysicsSystem::getCollisions(const Ptr &ptr, int collisionGroup, int collisionMask)
|
2013-04-28 12:59:15 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
return mEngine->getCollisions(ptr.getRefData().getBaseNodeOld()->getName(), collisionGroup, collisionMask);
|
2013-04-28 12:59:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 12:59:33 +00:00
|
|
|
Ogre::Vector3 PhysicsSystem::traceDown(const MWWorld::Ptr &ptr, float maxHeight)
|
2013-04-03 21:55:57 +00:00
|
|
|
{
|
2014-06-28 12:59:33 +00:00
|
|
|
return MovementSolver::traceDown(ptr, mEngine, maxHeight);
|
2013-04-03 21:55:57 +00:00
|
|
|
}
|
2011-08-01 13:55:36 +00:00
|
|
|
|
2012-03-13 16:09:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:29:56 +00:00
|
|
|
void PhysicsSystem::addObject (const Ptr& ptr, const std::string& mesh, bool placeable)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-04-25 13:19:17 +00:00
|
|
|
/*
|
2015-04-12 13:34:50 +00:00
|
|
|
Ogre::SceneNode* node = ptr.getRefData().getBaseNodeOld();
|
2012-11-05 18:40:02 +00:00
|
|
|
handleToMesh[node->getName()] = mesh;
|
2014-06-23 18:43:24 +00:00
|
|
|
mEngine->createAndAdjustRigidBody(
|
2014-10-04 18:37:35 +00:00
|
|
|
mesh, node->getName(), ptr.getCellRef().getScale(), node->getPosition(), node->getOrientation(), 0, 0, false, placeable);
|
2014-06-23 18:43:24 +00:00
|
|
|
mEngine->createAndAdjustRigidBody(
|
2014-10-04 18:37:35 +00:00
|
|
|
mesh, node->getName(), ptr.getCellRef().getScale(), node->getPosition(), node->getOrientation(), 0, 0, true, placeable);
|
2015-04-25 13:19:17 +00:00
|
|
|
*/
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:29:56 +00:00
|
|
|
void PhysicsSystem::addActor (const Ptr& ptr, const std::string& mesh)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-04-25 13:19:17 +00:00
|
|
|
/*
|
2015-04-12 13:34:50 +00:00
|
|
|
Ogre::SceneNode* node = ptr.getRefData().getBaseNodeOld();
|
2011-08-01 13:55:36 +00:00
|
|
|
//TODO:optimize this. Searching the std::map isn't very efficient i think.
|
2012-11-05 18:40:02 +00:00
|
|
|
mEngine->addCharacter(node->getName(), mesh, node->getPosition(), node->getScale().x, node->getOrientation());
|
2015-04-25 13:19:17 +00:00
|
|
|
*/
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::removeObject (const std::string& handle)
|
|
|
|
{
|
|
|
|
mEngine->removeCharacter(handle);
|
|
|
|
mEngine->removeRigidBody(handle);
|
|
|
|
mEngine->deleteRigidBody(handle);
|
|
|
|
}
|
|
|
|
|
2012-11-05 18:40:02 +00:00
|
|
|
void PhysicsSystem::moveObject (const Ptr& ptr)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
Ogre::SceneNode *node = ptr.getRefData().getBaseNodeOld();
|
2013-02-19 12:59:38 +00:00
|
|
|
const std::string &handle = node->getName();
|
|
|
|
const Ogre::Vector3 &position = node->getPosition();
|
2013-03-08 22:46:25 +00:00
|
|
|
|
2013-02-19 12:59:38 +00:00
|
|
|
if(OEngine::Physic::RigidBody *body = mEngine->getRigidBody(handle))
|
2014-09-28 16:02:57 +00:00
|
|
|
{
|
2013-02-19 12:59:38 +00:00
|
|
|
body->getWorldTransform().setOrigin(btVector3(position.x,position.y,position.z));
|
2014-09-28 16:02:57 +00:00
|
|
|
mEngine->mDynamicsWorld->updateSingleAabb(body);
|
|
|
|
}
|
2013-03-08 22:46:25 +00:00
|
|
|
|
2014-09-28 16:02:57 +00:00
|
|
|
// Actors update their AABBs every frame (DISABLE_DEACTIVATION), so no need to do it manually
|
2013-03-08 22:46:25 +00:00
|
|
|
if(OEngine::Physic::PhysicActor *physact = mEngine->getCharacter(handle))
|
2013-02-19 12:18:15 +00:00
|
|
|
physact->setPosition(position);
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:40:02 +00:00
|
|
|
void PhysicsSystem::rotateObject (const Ptr& ptr)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
Ogre::SceneNode* node = ptr.getRefData().getBaseNodeOld();
|
2013-02-08 00:18:29 +00:00
|
|
|
const std::string &handle = node->getName();
|
|
|
|
const Ogre::Quaternion &rotation = node->getOrientation();
|
2014-06-21 21:32:58 +00:00
|
|
|
|
|
|
|
// TODO: map to MWWorld::Ptr for faster access
|
2012-07-10 10:10:50 +00:00
|
|
|
if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle))
|
2012-01-05 00:47:06 +00:00
|
|
|
{
|
2012-09-15 17:23:49 +00:00
|
|
|
act->setRotation(rotation);
|
2012-01-05 00:47:06 +00:00
|
|
|
}
|
2012-07-10 10:10:50 +00:00
|
|
|
if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle))
|
|
|
|
{
|
2012-08-15 00:29:48 +00:00
|
|
|
if(dynamic_cast<btBoxShape*>(body->getCollisionShape()) == NULL)
|
|
|
|
body->getWorldTransform().setRotation(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w));
|
|
|
|
else
|
|
|
|
mEngine->boxAdjustExternal(handleToMesh[handle], body, node->getScale().x, node->getPosition(), rotation);
|
2014-09-28 16:02:57 +00:00
|
|
|
mEngine->mDynamicsWorld->updateSingleAabb(body);
|
2013-03-08 22:46:25 +00:00
|
|
|
}
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:40:02 +00:00
|
|
|
void PhysicsSystem::scaleObject (const Ptr& ptr)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2015-04-12 13:34:50 +00:00
|
|
|
Ogre::SceneNode* node = ptr.getRefData().getBaseNodeOld();
|
2013-02-08 00:18:29 +00:00
|
|
|
const std::string &handle = node->getName();
|
2012-06-18 17:03:00 +00:00
|
|
|
if(handleToMesh.find(handle) != handleToMesh.end())
|
|
|
|
{
|
2015-01-12 10:29:56 +00:00
|
|
|
std::string model = ptr.getClass().getModel(ptr);
|
2015-04-01 15:02:15 +00:00
|
|
|
//model = Misc::ResourceHelpers::correctActorModelPath(model); // FIXME: scaling shouldn't require model
|
2015-01-12 10:29:56 +00:00
|
|
|
|
2013-03-14 02:04:02 +00:00
|
|
|
bool placeable = false;
|
2015-05-01 19:43:21 +00:00
|
|
|
if (OEngine::Physic::RigidBody* body = mEngine->getRigidBody(handle))
|
2013-03-14 02:04:02 +00:00
|
|
|
placeable = body->mPlaceable;
|
2012-06-18 17:03:00 +00:00
|
|
|
removeObject(handle);
|
2015-01-12 10:29:56 +00:00
|
|
|
addObject(ptr, model, placeable);
|
2012-06-20 17:14:27 +00:00
|
|
|
}
|
2012-09-12 22:30:32 +00:00
|
|
|
|
|
|
|
if (OEngine::Physic::PhysicActor* act = mEngine->getCharacter(handle))
|
2014-09-14 05:04:02 +00:00
|
|
|
{
|
2014-12-15 12:34:04 +00:00
|
|
|
float scale = ptr.getCellRef().getScale();
|
2015-04-24 12:49:20 +00:00
|
|
|
osg::Vec3f scaleVec (scale,scale,scale);
|
2014-12-15 12:34:04 +00:00
|
|
|
if (!ptr.getClass().isNpc())
|
|
|
|
// NOTE: Ignoring Npc::adjustScale (race height) on purpose. This is a bug in MW and must be replicated for compatibility reasons
|
2015-04-24 12:49:20 +00:00
|
|
|
ptr.getClass().adjustScale(ptr, scaleVec);
|
|
|
|
act->setScale(scaleVec.x());
|
2014-09-14 05:04:02 +00:00
|
|
|
}
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsSystem::toggleCollisionMode()
|
|
|
|
{
|
2013-07-31 16:46:32 +00:00
|
|
|
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = mEngine->mActorMap.begin(); it != mEngine->mActorMap.end();++it)
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2011-10-29 07:50:11 +00:00
|
|
|
if (it->first=="player")
|
2011-08-01 13:55:36 +00:00
|
|
|
{
|
2011-10-29 07:50:11 +00:00
|
|
|
OEngine::Physic::PhysicActor* act = it->second;
|
|
|
|
|
|
|
|
bool cmode = act->getCollisionMode();
|
|
|
|
if(cmode)
|
|
|
|
{
|
2014-05-12 23:43:52 +00:00
|
|
|
act->enableCollisionMode(false);
|
2011-10-29 07:50:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-12 23:43:52 +00:00
|
|
|
act->enableCollisionMode(true);
|
2011-10-29 07:50:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 07:50:11 +00:00
|
|
|
throw std::logic_error ("can't find player");
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 14:48:45 +00:00
|
|
|
void PhysicsSystem::queueObjectMovement(const Ptr &ptr, const Ogre::Vector3 &movement)
|
|
|
|
{
|
|
|
|
PtrVelocityList::iterator iter = mMovementQueue.begin();
|
2014-04-27 17:03:33 +00:00
|
|
|
for(;iter != mMovementQueue.end();++iter)
|
2013-08-17 14:48:45 +00:00
|
|
|
{
|
|
|
|
if(iter->first == ptr)
|
|
|
|
{
|
|
|
|
iter->second = movement;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mMovementQueue.push_back(std::make_pair(ptr, movement));
|
|
|
|
}
|
|
|
|
|
2014-08-13 14:23:34 +00:00
|
|
|
void PhysicsSystem::clearQueuedMovement()
|
|
|
|
{
|
|
|
|
mMovementQueue.clear();
|
|
|
|
mCollisions.clear();
|
|
|
|
mStandingCollisions.clear();
|
|
|
|
}
|
|
|
|
|
2013-08-17 14:48:45 +00:00
|
|
|
const PtrVelocityList& PhysicsSystem::applyQueuedMovement(float dt)
|
|
|
|
{
|
|
|
|
mMovementResults.clear();
|
|
|
|
|
2013-08-20 18:31:49 +00:00
|
|
|
mTimeAccum += dt;
|
|
|
|
if(mTimeAccum >= 1.0f/60.0f)
|
2013-08-17 14:48:45 +00:00
|
|
|
{
|
2014-10-11 22:13:24 +00:00
|
|
|
// Collision events should be available on every frame
|
|
|
|
mCollisions.clear();
|
|
|
|
mStandingCollisions.clear();
|
|
|
|
|
2013-08-20 18:31:49 +00:00
|
|
|
const MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
PtrVelocityList::iterator iter = mMovementQueue.begin();
|
2014-04-27 17:03:33 +00:00
|
|
|
for(;iter != mMovementQueue.end();++iter)
|
2013-08-20 18:31:49 +00:00
|
|
|
{
|
2013-08-21 14:06:07 +00:00
|
|
|
float waterlevel = -std::numeric_limits<float>::max();
|
2015-02-08 14:39:10 +00:00
|
|
|
const MWWorld::CellStore *cell = iter->first.getCell();
|
|
|
|
if(cell->getCell()->hasWater())
|
|
|
|
waterlevel = cell->getWaterLevel();
|
2013-08-23 20:38:26 +00:00
|
|
|
|
2013-12-27 20:21:18 +00:00
|
|
|
float oldHeight = iter->first.getRefData().getPosition().pos[2];
|
|
|
|
|
2014-01-17 14:47:34 +00:00
|
|
|
const MWMechanics::MagicEffects& effects = iter->first.getClass().getCreatureStats(iter->first).getMagicEffects();
|
|
|
|
|
2014-01-02 01:03:11 +00:00
|
|
|
bool waterCollision = false;
|
2014-08-16 20:38:22 +00:00
|
|
|
if (effects.get(ESM::MagicEffect::WaterWalking).getMagnitude()
|
2015-02-08 14:39:10 +00:00
|
|
|
&& cell->getCell()->hasWater()
|
2014-01-02 01:03:11 +00:00
|
|
|
&& !world->isUnderwater(iter->first.getCell(),
|
|
|
|
Ogre::Vector3(iter->first.getRefData().getPosition().pos)))
|
|
|
|
waterCollision = true;
|
|
|
|
|
2014-10-05 20:24:11 +00:00
|
|
|
OEngine::Physic::PhysicActor *physicActor = mEngine->getCharacter(iter->first.getRefData().getHandle());
|
2014-10-06 12:56:07 +00:00
|
|
|
if (!physicActor) // actor was already removed from the scene
|
|
|
|
continue;
|
2014-10-05 20:24:11 +00:00
|
|
|
physicActor->setCanWaterWalk(waterCollision);
|
2014-01-02 01:03:11 +00:00
|
|
|
|
2014-11-10 07:42:44 +00:00
|
|
|
// Slow fall reduces fall speed by a factor of (effect magnitude / 200)
|
|
|
|
float slowFall = 1.f - std::max(0.f, std::min(1.f, effects.get(ESM::MagicEffect::SlowFall).getMagnitude() * 0.005f));
|
2014-01-17 14:47:34 +00:00
|
|
|
|
2013-08-23 20:38:26 +00:00
|
|
|
Ogre::Vector3 newpos = MovementSolver::move(iter->first, iter->second, mTimeAccum,
|
|
|
|
world->isFlying(iter->first),
|
2014-07-29 17:01:40 +00:00
|
|
|
waterlevel, slowFall, mEngine, mCollisions, mStandingCollisions);
|
2013-12-27 20:21:18 +00:00
|
|
|
|
|
|
|
float heightDiff = newpos.z - oldHeight;
|
|
|
|
|
|
|
|
if (heightDiff < 0)
|
|
|
|
iter->first.getClass().getCreatureStats(iter->first).addToFallHeight(-heightDiff);
|
|
|
|
|
2013-08-20 18:31:49 +00:00
|
|
|
mMovementResults.push_back(std::make_pair(iter->first, newpos));
|
|
|
|
}
|
2013-08-17 14:48:45 +00:00
|
|
|
|
2013-08-20 18:31:49 +00:00
|
|
|
mTimeAccum = 0.0f;
|
|
|
|
}
|
2013-08-17 14:48:45 +00:00
|
|
|
mMovementQueue.clear();
|
|
|
|
|
|
|
|
return mMovementResults;
|
|
|
|
}
|
2014-06-23 18:43:24 +00:00
|
|
|
|
|
|
|
void PhysicsSystem::stepSimulation(float dt)
|
|
|
|
{
|
2014-09-28 16:02:57 +00:00
|
|
|
animateCollisionShapes(mEngine->mAnimatedShapes, mEngine->mDynamicsWorld);
|
2014-06-23 18:43:24 +00:00
|
|
|
|
|
|
|
mEngine->stepSimulation(dt);
|
2015-05-02 22:39:01 +00:00
|
|
|
|
|
|
|
if (mDebugDrawer.get())
|
|
|
|
mDebugDrawer->step();
|
2014-06-23 18:43:24 +00:00
|
|
|
}
|
2014-07-29 17:01:40 +00:00
|
|
|
|
|
|
|
bool PhysicsSystem::isActorStandingOn(const Ptr &actor, const Ptr &object) const
|
|
|
|
{
|
|
|
|
const std::string& actorHandle = actor.getRefData().getHandle();
|
|
|
|
const std::string& objectHandle = object.getRefData().getHandle();
|
|
|
|
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it = mStandingCollisions.begin();
|
|
|
|
it != mStandingCollisions.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == actorHandle && it->second == objectHandle)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::getActorsStandingOn(const Ptr &object, std::vector<std::string> &out) const
|
|
|
|
{
|
|
|
|
const std::string& objectHandle = object.getRefData().getHandle();
|
|
|
|
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it = mStandingCollisions.begin();
|
|
|
|
it != mStandingCollisions.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->second == objectHandle)
|
|
|
|
out.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsSystem::isActorCollidingWith(const Ptr &actor, const Ptr &object) const
|
|
|
|
{
|
|
|
|
const std::string& actorHandle = actor.getRefData().getHandle();
|
|
|
|
const std::string& objectHandle = object.getRefData().getHandle();
|
|
|
|
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it = mCollisions.begin();
|
|
|
|
it != mCollisions.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->first == actorHandle && it->second == objectHandle)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::getActorsCollidingWith(const Ptr &object, std::vector<std::string> &out) const
|
|
|
|
{
|
|
|
|
const std::string& objectHandle = object.getRefData().getHandle();
|
|
|
|
|
|
|
|
for (std::map<std::string, std::string>::const_iterator it = mCollisions.begin();
|
|
|
|
it != mCollisions.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->second == objectHandle)
|
|
|
|
out.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-05 20:24:11 +00:00
|
|
|
void PhysicsSystem::disableWater()
|
|
|
|
{
|
|
|
|
if (mWaterEnabled)
|
|
|
|
{
|
|
|
|
mWaterEnabled = false;
|
|
|
|
updateWater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::enableWater(float height)
|
|
|
|
{
|
|
|
|
if (!mWaterEnabled || mWaterHeight != height)
|
|
|
|
{
|
|
|
|
mWaterEnabled = true;
|
|
|
|
mWaterHeight = height;
|
|
|
|
updateWater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::setWaterHeight(float height)
|
|
|
|
{
|
|
|
|
if (mWaterHeight != height)
|
|
|
|
{
|
|
|
|
mWaterHeight = height;
|
|
|
|
updateWater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsSystem::updateWater()
|
|
|
|
{
|
|
|
|
if (mWaterCollisionObject.get())
|
|
|
|
{
|
|
|
|
mEngine->mDynamicsWorld->removeCollisionObject(mWaterCollisionObject.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mWaterEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mWaterCollisionObject.reset(new btCollisionObject());
|
|
|
|
mWaterCollisionShape.reset(new btStaticPlaneShape(btVector3(0,0,1), mWaterHeight));
|
|
|
|
mWaterCollisionObject->setCollisionShape(mWaterCollisionShape.get());
|
|
|
|
mEngine->mDynamicsWorld->addCollisionObject(mWaterCollisionObject.get(), OEngine::Physic::CollisionType_Water,
|
|
|
|
OEngine::Physic::CollisionType_Actor);
|
|
|
|
}
|
2011-08-01 13:55:36 +00:00
|
|
|
}
|