You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
834 lines
35 KiB
C++
834 lines
35 KiB
C++
13 years ago
|
#include "physicssystem.hpp"
|
||
|
|
||
13 years ago
|
#include <stdexcept>
|
||
|
|
||
10 years ago
|
#include <osg/Group>
|
||
|
|
||
12 years ago
|
#include <components/nifbullet/bulletnifloader.hpp>
|
||
10 years ago
|
#include <components/misc/resourcehelpers.hpp>
|
||
14 years ago
|
|
||
11 years ago
|
#include <components/esm/loadgmst.hpp>
|
||
|
|
||
12 years ago
|
#include "../mwbase/world.hpp" // FIXME
|
||
12 years ago
|
#include "../mwbase/environment.hpp"
|
||
14 years ago
|
|
||
11 years ago
|
#include "../mwmechanics/creaturestats.hpp"
|
||
10 years ago
|
#include "../mwmechanics/movement.hpp"
|
||
11 years ago
|
|
||
12 years ago
|
#include "../mwworld/esmstore.hpp"
|
||
11 years ago
|
#include "../mwworld/cellstore.hpp"
|
||
12 years ago
|
|
||
10 years ago
|
#include "../mwrender/bulletdebugdraw.hpp"
|
||
|
|
||
10 years ago
|
//#include "../apps/openmw/mwrender/animation.hpp"
|
||
10 years ago
|
#include "../mwbase/world.hpp"
|
||
|
#include "../mwbase/environment.hpp"
|
||
11 years ago
|
|
||
10 years ago
|
#include "../mwworld/class.hpp"
|
||
13 years ago
|
|
||
11 years ago
|
namespace
|
||
|
{
|
||
|
|
||
10 years ago
|
/*
|
||
10 years ago
|
void animateCollisionShapes (std::map<OEngine::Physic::RigidBody*, OEngine::Physic::AnimatedShapeInstance>& map, btDynamicsWorld* dynamicsWorld)
|
||
11 years ago
|
{
|
||
|
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);
|
||
11 years ago
|
if (ptr.isEmpty()) // Shouldn't happen
|
||
|
throw std::runtime_error("can't find Ptr");
|
||
|
|
||
11 years ago
|
MWRender::Animation* animation = MWBase::Environment::get().getWorld()->getAnimation(ptr);
|
||
10 years ago
|
if (!animation)
|
||
|
continue;
|
||
11 years ago
|
|
||
|
OEngine::Physic::AnimatedShapeInstance& instance = it->second;
|
||
|
|
||
10 years ago
|
std::map<int, int>& shapes = instance.mAnimatedShapes;
|
||
|
for (std::map<int, int>::iterator shapeIt = shapes.begin();
|
||
11 years ago
|
shapeIt != shapes.end(); ++shapeIt)
|
||
|
{
|
||
|
|
||
10 years ago
|
const std::string& mesh = animation->getObjectRootName();
|
||
|
int boneHandle = NifOgre::NIFSkeletonLoader::lookupOgreBoneHandle(mesh, shapeIt->first);
|
||
|
Ogre::Node* bone = animation->getNode(boneHandle);
|
||
11 years ago
|
|
||
11 years ago
|
if (bone == NULL)
|
||
10 years ago
|
continue;
|
||
11 years ago
|
|
||
10 years ago
|
btCompoundShape* compound = static_cast<btCompoundShape*>(instance.mCompound);
|
||
11 years ago
|
|
||
|
btTransform trans;
|
||
10 years ago
|
trans.setOrigin(BtOgre::Convert::toBullet(bone->_getDerivedPosition()) * compound->getLocalScaling());
|
||
11 years ago
|
trans.setRotation(BtOgre::Convert::toBullet(bone->_getDerivedOrientation()));
|
||
|
|
||
10 years ago
|
compound->getChildShape(shapeIt->second)->setLocalScaling(
|
||
|
compound->getLocalScaling() *
|
||
|
BtOgre::Convert::toBullet(bone->_getDerivedScale()));
|
||
11 years ago
|
compound->updateChildTransform(shapeIt->second, trans);
|
||
|
}
|
||
10 years ago
|
|
||
|
// needed because we used btDynamicsWorld::setForceUpdateAllAabbs(false)
|
||
|
dynamicsWorld->updateSingleAabb(it->first);
|
||
11 years ago
|
}
|
||
|
}
|
||
10 years ago
|
*/
|
||
11 years ago
|
|
||
|
}
|
||
|
|
||
10 years ago
|
namespace MWPhysics
|
||
14 years ago
|
{
|
||
|
|
||
11 years ago
|
static const float sMaxSlope = 49.0f;
|
||
10 years ago
|
static const float sStepSizeUp = 34.0f;
|
||
|
static const float sStepSizeDown = 62.0f;
|
||
|
|
||
12 years ago
|
// Arbitrary number. To prevent infinite loops. They shouldn't happen but it's good to be prepared.
|
||
12 years ago
|
static const int sMaxIterations = 8;
|
||
12 years ago
|
|
||
|
class MovementSolver
|
||
|
{
|
||
|
private:
|
||
12 years ago
|
static float getSlope(const Ogre::Vector3 &normal)
|
||
|
{
|
||
|
return normal.angleBetween(Ogre::Vector3(0.0f,0.0f,1.0f)).valueDegrees();
|
||
|
}
|
||
|
|
||
12 years ago
|
static bool stepMove(btCollisionObject *colobj, Ogre::Vector3 &position,
|
||
10 years ago
|
const Ogre::Vector3 &toMove, float &remainingTime /*, OEngine::Physic::PhysicEngine *engine*/)
|
||
12 years ago
|
{
|
||
11 years ago
|
/*
|
||
|
* 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
|
||
10 years ago
|
* - having moved forward by between epsilon() and toMove,
|
||
11 years ago
|
* = 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
|
||
10 years ago
|
* | | -------> toMove | | +--+XX
|
||
11 years ago
|
* | | | | |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
|
||
|
* +--+ +--------
|
||
|
* ==============================================
|
||
|
*/
|
||
10 years ago
|
return false;
|
||
|
#if 0
|
||
12 years ago
|
OEngine::Physic::ActorTracer tracer, stepper;
|
||
|
|
||
10 years ago
|
stepper.doTrace(colobj, position, position+Ogre::Vector3(0.0f,0.0f,sStepSizeUp), engine);
|
||
12 years ago
|
if(stepper.mFraction < std::numeric_limits<float>::epsilon())
|
||
11 years ago
|
return false; // didn't even move the smallest representable amount
|
||
|
// (TODO: shouldn't this be larger? Why bother with such a small amount?)
|
||
12 years ago
|
|
||
11 years ago
|
/*
|
||
|
* Try moving from the elevated position using tracer.
|
||
|
*
|
||
|
* +--+ +--+
|
||
|
* | | |YY| FIXME: collision with object YY
|
||
|
* | | +--+
|
||
|
* | |
|
||
|
* <------------------->| |
|
||
|
* +--+ +--+
|
||
10 years ago
|
* |XX| the moved amount is toMove*tracer.mFraction
|
||
11 years ago
|
* +--+
|
||
|
* ==============================================
|
||
|
*/
|
||
10 years ago
|
tracer.doTrace(colobj, stepper.mEndPos, stepper.mEndPos + toMove, engine);
|
||
12 years ago
|
if(tracer.mFraction < std::numeric_limits<float>::epsilon())
|
||
11 years ago
|
return false; // didn't even move the smallest representable amount
|
||
12 years ago
|
|
||
11 years ago
|
/*
|
||
10 years ago
|
* Try moving back down sStepSizeDown using stepper.
|
||
11 years ago
|
* 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| | |
|
||
|
* +--+ +--+
|
||
|
* ==============================================
|
||
|
*/
|
||
10 years ago
|
stepper.doTrace(colobj, tracer.mEndPos, tracer.mEndPos-Ogre::Vector3(0.0f,0.0f,sStepSizeDown), engine);
|
||
12 years ago
|
if(stepper.mFraction < 1.0f && getSlope(stepper.mPlaneNormal) <= sMaxSlope)
|
||
12 years ago
|
{
|
||
10 years ago
|
// don't allow stepping up other actors
|
||
|
if (stepper.mHitObject->getBroadphaseHandle()->m_collisionFilterGroup == OEngine::Physic::CollisionType_Actor)
|
||
|
return false;
|
||
12 years ago
|
// only step down onto semi-horizontal surfaces. don't step down onto the side of a house or a wall.
|
||
11 years ago
|
// TODO: stepper.mPlaneNormal does not appear to be reliable - needs more testing
|
||
|
// NOTE: caller's variables 'position' & 'remainingTime' are modified here
|
||
12 years ago
|
position = stepper.mEndPos;
|
||
11 years ago
|
remainingTime *= (1.0f-tracer.mFraction); // remaining time is proportional to remaining distance
|
||
12 years ago
|
return true;
|
||
|
}
|
||
|
|
||
11 years ago
|
// moved between 0 and just under sStepSize distance but slope was too great,
|
||
|
// or moved full sStepSize distance (FIXME: is this a bug?)
|
||
10 years ago
|
#endif
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
12 years ago
|
///Project a vector u on another vector v
|
||
|
static inline Ogre::Vector3 project(const Ogre::Vector3 u, const Ogre::Vector3 &v)
|
||
12 years ago
|
{
|
||
12 years ago
|
return v * u.dotProduct(v);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
///Helper for computing the character sliding
|
||
|
static inline Ogre::Vector3 slide(Ogre::Vector3 direction, const Ogre::Vector3 &planeNormal)
|
||
12 years ago
|
{
|
||
12 years ago
|
return direction - project(direction, planeNormal);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
|
||
12 years ago
|
public:
|
||
10 years ago
|
static Ogre::Vector3 traceDown(const MWWorld::Ptr &ptr, /*OEngine::Physic::PhysicEngine *engine,*/ float maxHeight)
|
||
12 years ago
|
{
|
||
|
const ESM::Position &refpos = ptr.getRefData().getPosition();
|
||
|
Ogre::Vector3 position(refpos.pos);
|
||
|
|
||
10 years ago
|
return position;
|
||
|
#if 0
|
||
12 years ago
|
OEngine::Physic::PhysicActor *physicActor = engine->getCharacter(ptr.getRefData().getHandle());
|
||
|
if (!physicActor)
|
||
|
return position;
|
||
|
|
||
12 years ago
|
OEngine::Physic::ActorTracer tracer;
|
||
11 years ago
|
tracer.findGround(physicActor, position, position-Ogre::Vector3(0,0,maxHeight), engine);
|
||
12 years ago
|
if(tracer.mFraction >= 1.0f)
|
||
12 years ago
|
{
|
||
|
physicActor->setOnGround(false);
|
||
12 years ago
|
return position;
|
||
12 years ago
|
}
|
||
10 years ago
|
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;
|
||
|
}
|
||
10 years ago
|
#endif
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
static Ogre::Vector3 move(const MWWorld::Ptr &ptr, const Ogre::Vector3 &movement, float time,
|
||
10 years ago
|
bool isFlying, float waterlevel, float slowFall /*, OEngine::Physic::PhysicEngine *engine*/
|
||
11 years ago
|
, std::map<std::string, std::string>& collisionTracker
|
||
|
, std::map<std::string, std::string>& standingCollisionTracker)
|
||
12 years ago
|
{
|
||
12 years ago
|
const ESM::Position &refpos = ptr.getRefData().getPosition();
|
||
|
Ogre::Vector3 position(refpos.pos);
|
||
10 years ago
|
return position;
|
||
|
#if 0
|
||
11 years ago
|
// Early-out for totally static creatures
|
||
|
// (Not sure if gravity should still apply?)
|
||
10 years ago
|
if (!ptr.getClass().isMobile(ptr))
|
||
11 years ago
|
return position;
|
||
|
|
||
12 years ago
|
OEngine::Physic::PhysicActor *physicActor = engine->getCharacter(ptr.getRefData().getHandle());
|
||
10 years ago
|
if (!physicActor)
|
||
|
return position;
|
||
|
|
||
10 years ago
|
// Reset per-frame data
|
||
|
physicActor->setWalkingOnWater(false);
|
||
10 years ago
|
// Anything to collide with?
|
||
10 years ago
|
if(!physicActor->getCollisionMode())
|
||
12 years ago
|
{
|
||
11 years ago
|
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;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
btCollisionObject *colobj = physicActor->getCollisionBody();
|
||
|
Ogre::Vector3 halfExtents = physicActor->getHalfExtents();
|
||
11 years ago
|
position.z += halfExtents.z;
|
||
11 years ago
|
|
||
10 years ago
|
static const float fSwimHeightScale = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
||
|
.find("fSwimHeightScale")->getFloat();
|
||
|
float swimlevel = waterlevel + halfExtents.z - (halfExtents.z * 2 * fSwimHeightScale);
|
||
12 years ago
|
|
||
12 years ago
|
OEngine::Physic::ActorTracer tracer;
|
||
10 years ago
|
Ogre::Vector3 inertia = physicActor->getInertialForce();
|
||
12 years ago
|
Ogre::Vector3 velocity;
|
||
11 years ago
|
|
||
10 years ago
|
if(position.z < swimlevel || isFlying)
|
||
12 years ago
|
{
|
||
11 years ago
|
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;
|
||
12 years ago
|
}
|
||
|
else
|
||
|
{
|
||
11 years ago
|
velocity = Ogre::Quaternion(Ogre::Radian(refpos.rot[2]), Ogre::Vector3::NEGATIVE_UNIT_Z) * movement;
|
||
10 years ago
|
|
||
10 years ago
|
if (velocity.z > 0.f)
|
||
|
inertia = velocity;
|
||
|
if(!physicActor->getOnGround())
|
||
12 years ago
|
{
|
||
10 years ago
|
velocity = velocity + physicActor->getInertialForce();
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
10 years ago
|
ptr.getClass().getMovementSettings(ptr).mPosition[2] = 0;
|
||
12 years ago
|
|
||
11 years ago
|
// Now that we have the effective movement vector, apply wind forces to it
|
||
|
if (MWBase::Environment::get().getWorld()->isInStorm())
|
||
12 years ago
|
{
|
||
11 years ago
|
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));
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
Ogre::Vector3 origVelocity = velocity;
|
||
|
|
||
11 years ago
|
Ogre::Vector3 newPosition = position;
|
||
11 years ago
|
/*
|
||
|
* 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).
|
||
|
*/
|
||
12 years ago
|
float remainingTime = time;
|
||
11 years ago
|
for(int iterations = 0; iterations < sMaxIterations && remainingTime > 0.01f; ++iterations)
|
||
12 years ago
|
{
|
||
11 years ago
|
Ogre::Vector3 nextpos = newPosition + velocity * remainingTime;
|
||
|
|
||
11 years ago
|
// If not able to fly, don't allow to swim up into the air
|
||
10 years ago
|
if(newPosition.z < swimlevel &&
|
||
11 years ago
|
!isFlying && // can't fly
|
||
10 years ago
|
nextpos.z > swimlevel && // but about to go above water
|
||
|
newPosition.z <= swimlevel)
|
||
12 years ago
|
{
|
||
|
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;
|
||
11 years ago
|
// NOTE: remainingTime is unchanged before the loop continues
|
||
11 years ago
|
continue; // velocity updated, calculate nextpos again
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
if(newPosition.squaredDistance(nextpos) > 0.0001)
|
||
11 years ago
|
{
|
||
|
// trace to where character would go if there were no obstructions
|
||
|
tracer.doTrace(colobj, newPosition, nextpos, engine);
|
||
12 years ago
|
|
||
11 years ago
|
// check for obstructions
|
||
|
if(tracer.mFraction >= 1.0f)
|
||
|
{
|
||
|
newPosition = tracer.mEndPos; // ok to move, so set newPosition
|
||
|
break;
|
||
|
}
|
||
11 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
else
|
||
12 years ago
|
{
|
||
11 years ago
|
// 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.
|
||
12 years ago
|
break;
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
Ogre::Vector3 oldPosition = newPosition;
|
||
11 years ago
|
// We hit something. Try to step up onto it. (NOTE: stepMove does not allow stepping over)
|
||
11 years ago
|
// NOTE: stepMove modifies newPosition if successful
|
||
10 years ago
|
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);
|
||
10 years ago
|
if(result)
|
||
11 years ago
|
{
|
||
11 years ago
|
// don't let pure water creatures move out of water after stepMove
|
||
10 years ago
|
if (ptr.getClass().isPureWaterCreature(ptr)
|
||
10 years ago
|
&& newPosition.z + halfExtents.z > waterlevel)
|
||
11 years ago
|
newPosition = oldPosition;
|
||
|
}
|
||
12 years ago
|
else
|
||
|
{
|
||
|
// Can't move this way, try to find another spot along the plane
|
||
10 years ago
|
Ogre::Vector3 direction = velocity;
|
||
|
Ogre::Real movelen = direction.normalise();
|
||
12 years ago
|
Ogre::Vector3 reflectdir = velocity.reflect(tracer.mPlaneNormal);
|
||
12 years ago
|
reflectdir.normalise();
|
||
10 years ago
|
|
||
|
Ogre::Vector3 newVelocity = slide(reflectdir, tracer.mPlaneNormal)*movelen;
|
||
|
if ((newVelocity-velocity).squaredLength() < 0.01)
|
||
|
break;
|
||
|
if (velocity.dotProduct(origVelocity) <= 0.f)
|
||
|
break;
|
||
|
|
||
|
velocity = newVelocity;
|
||
12 years ago
|
|
||
|
// Do not allow sliding upward if there is gravity. Stepping will have taken
|
||
|
// care of that.
|
||
10 years ago
|
if(!(newPosition.z < swimlevel || isFlying))
|
||
12 years ago
|
velocity.z = std::min(velocity.z, 0.0f);
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
bool isOnGround = false;
|
||
10 years ago
|
if (!(inertia.z > 0.f) && !(newPosition.z < swimlevel))
|
||
12 years ago
|
{
|
||
10 years ago
|
Ogre::Vector3 from = newPosition;
|
||
10 years ago
|
Ogre::Vector3 to = newPosition - (physicActor->getOnGround() ?
|
||
10 years ago
|
Ogre::Vector3(0,0,sStepSizeDown+2.f) : Ogre::Vector3(0,0,2.f));
|
||
10 years ago
|
tracer.doTrace(colobj, from, to, engine);
|
||
10 years ago
|
if(tracer.mFraction < 1.0f && getSlope(tracer.mPlaneNormal) <= sMaxSlope
|
||
|
&& tracer.mHitObject->getBroadphaseHandle()->m_collisionFilterGroup != OEngine::Physic::CollisionType_Actor)
|
||
12 years ago
|
{
|
||
10 years ago
|
const btCollisionObject* standingOn = tracer.mHitObject;
|
||
|
if (const OEngine::Physic::RigidBody* body = dynamic_cast<const OEngine::Physic::RigidBody*>(standingOn))
|
||
|
{
|
||
|
standingCollisionTracker[ptr.getRefData().getHandle()] = body->mName;
|
||
|
}
|
||
10 years ago
|
if (standingOn->getBroadphaseHandle()->m_collisionFilterGroup == OEngine::Physic::CollisionType_Water)
|
||
|
physicActor->setWalkingOnWater(true);
|
||
10 years ago
|
|
||
10 years ago
|
if (!isFlying)
|
||
|
newPosition.z = tracer.mEndPos.z + 1.0f;
|
||
10 years ago
|
|
||
12 years ago
|
isOnGround = true;
|
||
|
}
|
||
12 years ago
|
else
|
||
10 years ago
|
{
|
||
|
// 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)
|
||
|
{
|
||
10 years ago
|
if (Ogre::Vector3(velocity.x, velocity.y, 0).squaredLength() < 100.f*100.f)
|
||
10 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
isOnGround = false;
|
||
10 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
10 years ago
|
if(isOnGround || newPosition.z < swimlevel || isFlying)
|
||
12 years ago
|
physicActor->setInertialForce(Ogre::Vector3(0.0f));
|
||
|
else
|
||
|
{
|
||
10 years ago
|
inertia.z += time * -627.2f;
|
||
11 years ago
|
if (inertia.z < 0)
|
||
10 years ago
|
inertia.z *= slowFall;
|
||
12 years ago
|
physicActor->setInertialForce(inertia);
|
||
|
}
|
||
12 years ago
|
physicActor->setOnGround(isOnGround);
|
||
12 years ago
|
|
||
10 years ago
|
newPosition.z -= halfExtents.z; // remove what was added at the beginning
|
||
12 years ago
|
return newPosition;
|
||
10 years ago
|
#endif
|
||
12 years ago
|
}
|
||
|
};
|
||
|
|
||
|
|
||
10 years ago
|
PhysicsSystem::PhysicsSystem(osg::ref_ptr<osg::Group> parentNode) :
|
||
10 years ago
|
mTimeAccum(0.0f), mWaterEnabled(false), mWaterHeight(0), mDebugDrawEnabled(false), mParentNode(parentNode)
|
||
14 years ago
|
{
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
PhysicsSystem::~PhysicsSystem()
|
||
|
{
|
||
10 years ago
|
//if (mWaterCollisionObject.get())
|
||
|
// mEngine->mDynamicsWorld->removeCollisionObject(mWaterCollisionObject.get());
|
||
13 years ago
|
}
|
||
12 years ago
|
|
||
10 years ago
|
bool PhysicsSystem::toggleDebugRendering()
|
||
|
{
|
||
|
mDebugDrawEnabled = !mDebugDrawEnabled;
|
||
|
|
||
|
if (mDebugDrawEnabled && !mDebugDrawer.get())
|
||
|
{
|
||
10 years ago
|
//mDebugDrawer.reset(new MWRender::DebugDrawer(mParentNode, mEngine->mDynamicsWorld));
|
||
|
//mEngine->mDynamicsWorld->setDebugDrawer(mDebugDrawer.get());
|
||
|
//mDebugDrawer->setDebugMode(mDebugDrawEnabled);
|
||
10 years ago
|
}
|
||
|
else if (mDebugDrawer.get())
|
||
|
mDebugDrawer->setDebugMode(mDebugDrawEnabled);
|
||
|
return mDebugDrawEnabled;
|
||
|
}
|
||
|
|
||
12 years ago
|
std::pair<std::string,Ogre::Vector3> PhysicsSystem::getHitContact(const std::string &name,
|
||
12 years ago
|
const Ogre::Vector3 &origin,
|
||
|
const Ogre::Quaternion &orient,
|
||
12 years ago
|
float queryDistance)
|
||
12 years ago
|
{
|
||
10 years ago
|
return std::make_pair(std::string(), Ogre::Vector3());
|
||
|
/*
|
||
12 years ago
|
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]));
|
||
10 years ago
|
*/
|
||
12 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
bool PhysicsSystem::castRay(const Ogre::Vector3& from, const Ogre::Vector3& to, bool ignoreHeightMap)
|
||
13 years ago
|
{
|
||
10 years ago
|
return false;
|
||
|
/*
|
||
13 years ago
|
btVector3 _from, _to;
|
||
|
_from = btVector3(from.x, from.y, from.z);
|
||
|
_to = btVector3(to.x, to.y, to.z);
|
||
13 years ago
|
|
||
10 years ago
|
std::pair<std::string, float> result = mEngine->rayTest(_from, _to,ignoreHeightMap);
|
||
13 years ago
|
return !(result.first == "");
|
||
10 years ago
|
*/
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
std::pair<bool, Ogre::Vector3>
|
||
|
PhysicsSystem::castRay(const Ogre::Vector3 &orig, const Ogre::Vector3 &dir, float len)
|
||
|
{
|
||
10 years ago
|
return std::make_pair(false, Ogre::Vector3());
|
||
|
/*
|
||
13 years ago
|
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);
|
||
12 years ago
|
if (test.second == -1) {
|
||
13 years ago
|
return std::make_pair(false, Ogre::Vector3());
|
||
|
}
|
||
|
return std::make_pair(true, ray.getPoint(len * test.second));
|
||
10 years ago
|
*/
|
||
13 years ago
|
}
|
||
|
|
||
10 years ago
|
std::vector<std::string> PhysicsSystem::getCollisions(const MWWorld::Ptr &ptr, int collisionGroup, int collisionMask)
|
||
12 years ago
|
{
|
||
10 years ago
|
return std::vector<std::string>();//mEngine->getCollisions(ptr.getRefData().getBaseNodeOld()->getName(), collisionGroup, collisionMask);
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
Ogre::Vector3 PhysicsSystem::traceDown(const MWWorld::Ptr &ptr, float maxHeight)
|
||
12 years ago
|
{
|
||
10 years ago
|
return Ogre::Vector3();//MovementSolver::traceDown(ptr, mEngine, maxHeight);
|
||
12 years ago
|
}
|
||
14 years ago
|
|
||
13 years ago
|
void PhysicsSystem::addHeightField (float* heights,
|
||
|
int x, int y, float yoffset,
|
||
|
float triSize, float sqrtVerts)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void PhysicsSystem::removeHeightField (int x, int y)
|
||
|
{
|
||
|
}
|
||
|
|
||
10 years ago
|
void PhysicsSystem::addObject (const MWWorld::Ptr& ptr, const std::string& mesh, bool placeable)
|
||
14 years ago
|
{
|
||
11 years ago
|
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
void PhysicsSystem::addActor (const MWWorld::Ptr& ptr, const std::string& mesh)
|
||
14 years ago
|
{
|
||
12 years ago
|
|
||
14 years ago
|
}
|
||
|
|
||
|
bool PhysicsSystem::toggleCollisionMode()
|
||
|
{
|
||
10 years ago
|
/*
|
||
12 years ago
|
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = mEngine->mActorMap.begin(); it != mEngine->mActorMap.end();++it)
|
||
14 years ago
|
{
|
||
13 years ago
|
if (it->first=="player")
|
||
14 years ago
|
{
|
||
13 years ago
|
OEngine::Physic::PhysicActor* act = it->second;
|
||
|
|
||
|
bool cmode = act->getCollisionMode();
|
||
|
if(cmode)
|
||
|
{
|
||
11 years ago
|
act->enableCollisionMode(false);
|
||
13 years ago
|
return false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
11 years ago
|
act->enableCollisionMode(true);
|
||
13 years ago
|
return true;
|
||
|
}
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
throw std::logic_error ("can't find player");
|
||
10 years ago
|
*/
|
||
|
return false;
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
void PhysicsSystem::queueObjectMovement(const MWWorld::Ptr &ptr, const Ogre::Vector3 &movement)
|
||
12 years ago
|
{
|
||
|
PtrVelocityList::iterator iter = mMovementQueue.begin();
|
||
11 years ago
|
for(;iter != mMovementQueue.end();++iter)
|
||
12 years ago
|
{
|
||
|
if(iter->first == ptr)
|
||
|
{
|
||
|
iter->second = movement;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mMovementQueue.push_back(std::make_pair(ptr, movement));
|
||
|
}
|
||
|
|
||
11 years ago
|
void PhysicsSystem::clearQueuedMovement()
|
||
|
{
|
||
|
mMovementQueue.clear();
|
||
|
mCollisions.clear();
|
||
|
mStandingCollisions.clear();
|
||
|
}
|
||
|
|
||
12 years ago
|
const PtrVelocityList& PhysicsSystem::applyQueuedMovement(float dt)
|
||
|
{
|
||
|
mMovementResults.clear();
|
||
|
|
||
12 years ago
|
mTimeAccum += dt;
|
||
|
if(mTimeAccum >= 1.0f/60.0f)
|
||
12 years ago
|
{
|
||
10 years ago
|
// Collision events should be available on every frame
|
||
|
mCollisions.clear();
|
||
|
mStandingCollisions.clear();
|
||
|
|
||
10 years ago
|
/*
|
||
|
|
||
12 years ago
|
const MWBase::World *world = MWBase::Environment::get().getWorld();
|
||
|
PtrVelocityList::iterator iter = mMovementQueue.begin();
|
||
11 years ago
|
for(;iter != mMovementQueue.end();++iter)
|
||
12 years ago
|
{
|
||
12 years ago
|
float waterlevel = -std::numeric_limits<float>::max();
|
||
10 years ago
|
const MWWorld::CellStore *cell = iter->first.getCell();
|
||
|
if(cell->getCell()->hasWater())
|
||
|
waterlevel = cell->getWaterLevel();
|
||
12 years ago
|
|
||
11 years ago
|
float oldHeight = iter->first.getRefData().getPosition().pos[2];
|
||
|
|
||
11 years ago
|
const MWMechanics::MagicEffects& effects = iter->first.getClass().getCreatureStats(iter->first).getMagicEffects();
|
||
|
|
||
11 years ago
|
bool waterCollision = false;
|
||
11 years ago
|
if (effects.get(ESM::MagicEffect::WaterWalking).getMagnitude()
|
||
10 years ago
|
&& cell->getCell()->hasWater()
|
||
11 years ago
|
&& !world->isUnderwater(iter->first.getCell(),
|
||
|
Ogre::Vector3(iter->first.getRefData().getPosition().pos)))
|
||
|
waterCollision = true;
|
||
|
|
||
10 years ago
|
OEngine::Physic::PhysicActor *physicActor = mEngine->getCharacter(iter->first.getRefData().getHandle());
|
||
10 years ago
|
if (!physicActor) // actor was already removed from the scene
|
||
|
continue;
|
||
10 years ago
|
physicActor->setCanWaterWalk(waterCollision);
|
||
11 years ago
|
|
||
10 years ago
|
// 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));
|
||
11 years ago
|
|
||
12 years ago
|
Ogre::Vector3 newpos = MovementSolver::move(iter->first, iter->second, mTimeAccum,
|
||
|
world->isFlying(iter->first),
|
||
11 years ago
|
waterlevel, slowFall, mEngine, mCollisions, mStandingCollisions);
|
||
11 years ago
|
|
||
|
float heightDiff = newpos.z - oldHeight;
|
||
|
|
||
|
if (heightDiff < 0)
|
||
|
iter->first.getClass().getCreatureStats(iter->first).addToFallHeight(-heightDiff);
|
||
|
|
||
12 years ago
|
mMovementResults.push_back(std::make_pair(iter->first, newpos));
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
*/
|
||
|
|
||
12 years ago
|
mTimeAccum = 0.0f;
|
||
|
}
|
||
12 years ago
|
mMovementQueue.clear();
|
||
|
|
||
|
return mMovementResults;
|
||
|
}
|
||
11 years ago
|
|
||
|
void PhysicsSystem::stepSimulation(float dt)
|
||
|
{
|
||
10 years ago
|
//animateCollisionShapes(mEngine->mAnimatedShapes, mEngine->mDynamicsWorld);
|
||
11 years ago
|
|
||
10 years ago
|
//mEngine->stepSimulation(dt);
|
||
10 years ago
|
|
||
|
if (mDebugDrawer.get())
|
||
|
mDebugDrawer->step();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
bool PhysicsSystem::isActorStandingOn(const MWWorld::Ptr &actor, const MWWorld::Ptr &object) const
|
||
11 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
|
|
||
10 years ago
|
void PhysicsSystem::getActorsStandingOn(const MWWorld::Ptr &object, std::vector<std::string> &out) const
|
||
11 years ago
|
{
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
bool PhysicsSystem::isActorCollidingWith(const MWWorld::Ptr &actor, const MWWorld::Ptr &object) const
|
||
11 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
|
|
||
10 years ago
|
void PhysicsSystem::getActorsCollidingWith(const MWWorld::Ptr &object, std::vector<std::string> &out) const
|
||
11 years ago
|
{
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
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())
|
||
|
{
|
||
10 years ago
|
//mEngine->mDynamicsWorld->removeCollisionObject(mWaterCollisionObject.get());
|
||
10 years ago
|
}
|
||
|
|
||
|
if (!mWaterEnabled)
|
||
|
return;
|
||
|
|
||
|
mWaterCollisionObject.reset(new btCollisionObject());
|
||
|
mWaterCollisionShape.reset(new btStaticPlaneShape(btVector3(0,0,1), mWaterHeight));
|
||
|
mWaterCollisionObject->setCollisionShape(mWaterCollisionShape.get());
|
||
10 years ago
|
//mEngine->mDynamicsWorld->addCollisionObject(mWaterCollisionObject.get(), OEngine::Physic::CollisionType_Water,
|
||
|
// OEngine::Physic::CollisionType_Actor);
|
||
10 years ago
|
}
|
||
14 years ago
|
}
|