2015-05-12 01:02:15 +00:00
# ifndef OPENMW_MWPHYSICS_PHYSICSSYSTEM_H
# define OPENMW_MWPHYSICS_PHYSICSSYSTEM_H
2011-08-01 13:55:36 +00:00
2020-10-15 04:11:00 +00:00
# include <array>
2014-10-05 20:24:11 +00:00
# include <memory>
2015-06-03 21:04:35 +00:00
# include <map>
2015-11-19 22:33:08 +00:00
# include <set>
2018-05-26 14:44:25 +00:00
# include <algorithm>
2014-10-05 20:24:11 +00:00
2015-06-03 21:04:35 +00:00
# include <osg/Quat>
2020-05-11 13:37:00 +00:00
# include <osg/BoundingBox>
2015-05-02 22:39:01 +00:00
# include <osg/ref_ptr>
2020-11-20 12:11:53 +00:00
# include <osg/Timer>
2015-05-02 22:39:01 +00:00
2015-05-09 23:09:00 +00:00
# include "../mwworld/ptr.hpp"
2013-08-17 14:48:45 +00:00
2015-05-31 23:57:15 +00:00
# include "collisiontype.hpp"
2020-08-03 20:44:16 +00:00
# include "raycasting.hpp"
2015-05-31 23:57:15 +00:00
2015-05-02 22:39:01 +00:00
namespace osg
{
class Group ;
2017-03-06 18:04:17 +00:00
class Object ;
2020-05-21 22:11:23 +00:00
class Stats ;
2015-05-02 22:39:01 +00:00
}
2013-02-07 20:11:10 +00:00
2015-05-02 22:39:01 +00:00
namespace MWRender
{
class DebugDrawer ;
}
2015-11-16 22:30:10 +00:00
namespace Resource
2015-05-12 01:02:15 +00:00
{
class BulletShapeManager ;
2016-02-09 18:04:59 +00:00
class ResourceSystem ;
2015-05-12 01:02:15 +00:00
}
2016-02-09 18:04:59 +00:00
namespace SceneUtil
2015-05-12 01:02:15 +00:00
{
2016-02-09 18:04:59 +00:00
class UnrefQueue ;
2015-05-12 01:02:15 +00:00
}
2015-05-27 21:09:38 +00:00
class btCollisionWorld ;
2015-05-27 20:32:11 +00:00
class btBroadphaseInterface ;
class btDefaultCollisionConfiguration ;
class btCollisionDispatcher ;
class btCollisionObject ;
class btCollisionShape ;
2020-10-25 21:33:19 +00:00
class btVector3 ;
2015-05-10 00:08:25 +00:00
2015-05-09 23:09:00 +00:00
namespace MWPhysics
2011-08-01 13:55:36 +00:00
{
2015-05-10 00:08:25 +00:00
class HeightField ;
2015-05-12 01:02:15 +00:00
class Object ;
class Actor ;
2020-10-15 04:11:00 +00:00
class PhysicsTaskScheduler ;
2019-02-13 07:30:16 +00:00
class Projectile ;
2020-10-15 04:11:00 +00:00
2020-12-05 00:09:43 +00:00
using ActorMap = std : : map < MWWorld : : ConstPtr , std : : shared_ptr < Actor > > ;
2020-11-01 21:30:48 +00:00
struct ContactPoint
{
MWWorld : : Ptr mObject ;
osg : : Vec3f mPoint ;
osg : : Vec3f mNormal ;
} ;
2020-10-15 04:11:00 +00:00
struct LOSRequest
{
LOSRequest ( const std : : weak_ptr < Actor > & a1 , const std : : weak_ptr < Actor > & a2 ) ;
std : : array < std : : weak_ptr < Actor > , 2 > mActors ;
std : : array < const Actor * , 2 > mRawActors ;
bool mResult ;
bool mStale ;
int mAge ;
} ;
bool operator = = ( const LOSRequest & lhs , const LOSRequest & rhs ) noexcept ;
struct ActorFrameData
{
ActorFrameData ( const std : : shared_ptr < Actor > & actor , const MWWorld : : Ptr character , const MWWorld : : Ptr standingOn , bool moveToWaterSurface , osg : : Vec3f movement , float slowFall , float waterlevel ) ;
2020-11-28 19:45:23 +00:00
void updatePosition ( ) ;
2020-10-15 04:11:00 +00:00
std : : weak_ptr < Actor > mActor ;
Actor * mActorRaw ;
MWWorld : : Ptr mPtr ;
MWWorld : : Ptr mStandingOn ;
bool mFlying ;
bool mSwimming ;
bool mWasOnGround ;
bool mWantJump ;
bool mDidJump ;
bool mIsDead ;
bool mNeedLand ;
bool mMoveToWaterSurface ;
float mWaterlevel ;
float mSlowFall ;
float mOldHeight ;
float mFallHeight ;
osg : : Vec3f mMovement ;
osg : : Vec3f mPosition ;
ESM : : Position mRefpos ;
} ;
struct WorldFrameData
{
WorldFrameData ( ) ;
bool mIsInStorm ;
osg : : Vec3f mStormDirection ;
} ;
2015-05-10 00:08:25 +00:00
2020-08-03 20:44:16 +00:00
class PhysicsSystem : public RayCastingInterface
2011-08-01 13:55:36 +00:00
{
public :
2015-05-12 14:24:53 +00:00
PhysicsSystem ( Resource : : ResourceSystem * resourceSystem , osg : : ref_ptr < osg : : Group > parentNode ) ;
2020-08-26 08:34:27 +00:00
virtual ~ PhysicsSystem ( ) ;
2011-08-22 19:34:51 +00:00
2016-02-09 18:04:59 +00:00
void setUnrefQueue ( SceneUtil : : UnrefQueue * unrefQueue ) ;
2016-02-06 23:07:02 +00:00
Resource : : BulletShapeManager * getShapeManager ( ) ;
2014-10-05 20:24:11 +00:00
void enableWater ( float height ) ;
void setWaterHeight ( float height ) ;
void disableWater ( ) ;
2015-12-18 17:32:42 +00:00
void addObject ( const MWWorld : : Ptr & ptr , const std : : string & mesh , int collisionType = CollisionType_World ) ;
2015-05-12 17:02:56 +00:00
void addActor ( const MWWorld : : Ptr & ptr , const std : : string & mesh ) ;
2020-11-21 15:26:45 +00:00
int addProjectile ( const MWWorld : : Ptr & caster , const osg : : Vec3f & position ) ;
2019-02-13 07:30:16 +00:00
void updateProjectile ( const int projectileId , const osg : : Vec3f & position ) ;
void removeProjectile ( const int projectileId ) ;
2015-05-12 17:02:56 +00:00
void updatePtr ( const MWWorld : : Ptr & old , const MWWorld : : Ptr & updated ) ;
Actor * getActor ( const MWWorld : : Ptr & ptr ) ;
2015-12-18 16:36:14 +00:00
const Actor * getActor ( const MWWorld : : ConstPtr & ptr ) const ;
2016-03-05 14:56:19 +00:00
const Object * getObject ( const MWWorld : : ConstPtr & ptr ) const ;
2015-05-12 01:02:15 +00:00
2020-10-23 18:27:07 +00:00
Projectile * getProjectile ( int projectileId ) const ;
2015-05-12 01:02:15 +00:00
// Object or Actor
void remove ( const MWWorld : : Ptr & ptr ) ;
void updateScale ( const MWWorld : : Ptr & ptr ) ;
void updateRotation ( const MWWorld : : Ptr & ptr ) ;
void updatePosition ( const MWWorld : : Ptr & ptr ) ;
2020-12-18 08:22:02 +00:00
void resetPosition ( const MWWorld : : ConstPtr & ptr ) ;
2015-05-12 01:02:15 +00:00
2017-03-07 14:00:16 +00:00
void addHeightField ( const float * heights , int x , int y , float triSize , float sqrtVerts , float minH , float maxH , const osg : : Object * holdObject ) ;
2012-03-13 16:09:50 +00:00
void removeHeightField ( int x , int y ) ;
2018-03-13 22:49:08 +00:00
const HeightField * getHeightField ( int x , int y ) const ;
2011-08-01 13:55:36 +00:00
bool toggleCollisionMode ( ) ;
2013-08-20 18:31:49 +00:00
2020-10-14 09:32:12 +00:00
void stepSimulation ( ) ;
2015-06-07 15:00:00 +00:00
void debugDraw ( ) ;
2014-06-23 18:43:24 +00:00
2015-12-18 16:56:48 +00:00
std : : vector < MWWorld : : Ptr > getCollisions ( const MWWorld : : ConstPtr & ptr , int collisionGroup , int collisionMask ) const ; ///< get handles this object collides with
2020-11-01 21:30:48 +00:00
std : : vector < ContactPoint > getCollisionsPoints ( const MWWorld : : ConstPtr & ptr , int collisionGroup , int collisionMask ) const ;
2017-02-10 01:43:49 +00:00
osg : : Vec3f traceDown ( const MWWorld : : Ptr & ptr , const osg : : Vec3f & position , float maxHeight ) ;
2013-02-05 20:45:10 +00:00
2015-12-18 16:38:21 +00:00
std : : pair < MWWorld : : Ptr , osg : : Vec3f > getHitContact ( const MWWorld : : ConstPtr & actor ,
2015-05-22 02:36:17 +00:00
const osg : : Vec3f & origin ,
const osg : : Quat & orientation ,
2020-10-14 09:32:12 +00:00
float queryDistance , std : : vector < MWWorld : : Ptr > & targets ) ;
2012-03-25 18:52:56 +00:00
2015-11-18 18:00:43 +00:00
/// Get distance from \a point to the collision shape of \a target. Uses a raycast to find where the
/// target vector hits the collision shape and then calculates distance from the intersection point.
/// This can be used to find out how much nearer we need to move to the target for a "getHitContact" to be successful.
/// \note Only Actor targets are supported at the moment.
2020-10-22 21:57:53 +00:00
float getHitDistance ( const osg : : Vec3f & point , const MWWorld : : ConstPtr & target ) const override ;
2015-05-31 23:57:15 +00:00
2017-02-02 07:20:34 +00:00
/// @param me Optional, a Ptr to ignore in the list of results. targets are actors to filter for, ignoring all other actors.
2020-08-03 20:44:16 +00:00
RayCastingResult castRay ( const osg : : Vec3f & from , const osg : : Vec3f & to , const MWWorld : : ConstPtr & ignore = MWWorld : : ConstPtr ( ) ,
2017-02-02 07:20:34 +00:00
std : : vector < MWWorld : : Ptr > targets = std : : vector < MWWorld : : Ptr > ( ) ,
2019-02-13 07:30:16 +00:00
int mask = CollisionType_World | CollisionType_HeightMap | CollisionType_Actor | CollisionType_Door , int group = 0xff , int projId = - 1 ) const override ;
2011-08-22 19:34:51 +00:00
2020-10-22 21:57:53 +00:00
RayCastingResult castSphere ( const osg : : Vec3f & from , const osg : : Vec3f & to , float radius ) const override ;
2015-06-01 13:34:46 +00:00
2015-05-31 23:57:15 +00:00
/// Return true if actor1 can see actor2.
2020-10-22 21:57:53 +00:00
bool getLineOfSight ( const MWWorld : : ConstPtr & actor1 , const MWWorld : : ConstPtr & actor2 ) const override ;
2012-07-25 16:25:53 +00:00
2015-06-01 00:40:42 +00:00
bool isOnGround ( const MWWorld : : Ptr & actor ) ;
2016-11-14 15:20:17 +00:00
bool canMoveToWaterSurface ( const MWWorld : : ConstPtr & actor , const float waterlevel ) ;
2015-09-16 23:08:16 +00:00
/// Get physical half extents (scaled) of the given actor.
2015-12-18 16:36:14 +00:00
osg : : Vec3f getHalfExtents ( const MWWorld : : ConstPtr & actor ) const ;
2015-06-01 19:41:13 +00:00
2019-03-03 11:45:36 +00:00
/// Get physical half extents (not scaled) of the given actor.
osg : : Vec3f getOriginalHalfExtents ( const MWWorld : : ConstPtr & actor ) const ;
2015-11-01 20:45:58 +00:00
/// @see MWPhysics::Actor::getRenderingHalfExtents
2015-12-18 16:36:14 +00:00
osg : : Vec3f getRenderingHalfExtents ( const MWWorld : : ConstPtr & actor ) const ;
2015-11-03 17:15:47 +00:00
/// Get the position of the collision shape for the actor. Use together with getHalfExtents() to get the collision bounds in world space.
/// @note The collision shape's origin is in its center, so the position returned can be described as center of the actor collision box in world space.
2016-02-13 01:56:41 +00:00
osg : : Vec3f getCollisionObjectPosition ( const MWWorld : : ConstPtr & actor ) const ;
2015-11-01 20:45:58 +00:00
2020-05-11 13:37:00 +00:00
/// Get bounding box in world space of the given object.
osg : : BoundingBox getBoundingBox ( const MWWorld : : ConstPtr & object ) const ;
2013-08-17 14:48:45 +00:00
/// Queues velocity movement for a Ptr. If a Ptr is already queued, its velocity will
/// be overwritten. Valid until the next call to applyQueuedMovement.
2015-05-12 01:02:15 +00:00
void queueObjectMovement ( const MWWorld : : Ptr & ptr , const osg : : Vec3f & velocity ) ;
2013-08-17 14:48:45 +00:00
2014-08-13 14:23:34 +00:00
/// Apply all queued movements, then clear the list.
2020-12-18 08:22:02 +00:00
const std : : vector < MWWorld : : Ptr > & applyQueuedMovement ( float dt , bool skipSimulation , osg : : Timer_t frameStart , unsigned int frameNumber , osg : : Stats & stats ) ;
2013-08-17 14:48:45 +00:00
2014-08-13 14:23:34 +00:00
/// Clear the queued movements list without applying.
void clearQueuedMovement ( ) ;
2014-07-29 17:01:40 +00:00
/// Return true if \a actor has been standing on \a object in this frame
/// This will trigger whenever the object is directly below the actor.
/// It doesn't matter if the actor is stationary or moving.
2015-12-18 16:56:48 +00:00
bool isActorStandingOn ( const MWWorld : : Ptr & actor , const MWWorld : : ConstPtr & object ) const ;
2014-07-29 17:01:40 +00:00
/// Get the handle of all actors standing on \a object in this frame.
2015-12-18 16:56:48 +00:00
void getActorsStandingOn ( const MWWorld : : ConstPtr & object , std : : vector < MWWorld : : Ptr > & out ) const ;
2014-07-29 17:01:40 +00:00
/// Return true if \a actor has collided with \a object in this frame.
/// This will detect running into objects, but will not detect climbing stairs, stepping up a small object, etc.
2015-12-18 16:56:48 +00:00
bool isActorCollidingWith ( const MWWorld : : Ptr & actor , const MWWorld : : ConstPtr & object ) const ;
2014-07-29 17:01:40 +00:00
/// Get the handle of all actors colliding with \a object in this frame.
2015-12-18 16:56:48 +00:00
void getActorsCollidingWith ( const MWWorld : : ConstPtr & object , std : : vector < MWWorld : : Ptr > & out ) const ;
2014-07-29 17:01:40 +00:00
2015-05-02 22:39:01 +00:00
bool toggleDebugRendering ( ) ;
2015-11-20 18:22:31 +00:00
/// Mark the given object as a 'non-solid' object. A non-solid object means that
/// \a isOnSolidGround will return false for actors standing on that object.
2015-12-18 16:56:48 +00:00
void markAsNonSolid ( const MWWorld : : ConstPtr & ptr ) ;
2015-11-20 18:22:31 +00:00
bool isOnSolidGround ( const MWWorld : : Ptr & actor ) const ;
2018-05-26 14:44:25 +00:00
2018-09-22 08:57:50 +00:00
void updateAnimatedCollisionShape ( const MWWorld : : Ptr & object ) ;
2018-05-26 14:44:25 +00:00
template < class Function >
void forEachAnimatedObject ( Function & & function ) const
{
std : : for_each ( mAnimatedObjects . begin ( ) , mAnimatedObjects . end ( ) , function ) ;
}
2020-02-09 17:24:08 +00:00
bool isAreaOccupiedByOtherActor ( const osg : : Vec3f & position , const float radius , const MWWorld : : ConstPtr & ignore ) const ;
2020-05-21 22:11:23 +00:00
void reportStats ( unsigned int frameNumber , osg : : Stats & stats ) const ;
2020-10-25 21:33:19 +00:00
void reportCollision ( const btVector3 & position , const btVector3 & normal ) ;
2020-05-21 22:11:23 +00:00
2011-08-01 13:55:36 +00:00
private :
2012-08-17 06:10:37 +00:00
2014-10-05 20:24:11 +00:00
void updateWater ( ) ;
2020-10-21 19:28:01 +00:00
std : : vector < ActorFrameData > prepareFrameData ( int numSteps ) ;
2020-10-15 04:11:00 +00:00
2016-02-09 18:04:59 +00:00
osg : : ref_ptr < SceneUtil : : UnrefQueue > mUnrefQueue ;
2020-10-14 09:32:12 +00:00
std : : unique_ptr < btBroadphaseInterface > mBroadphase ;
std : : unique_ptr < btDefaultCollisionConfiguration > mCollisionConfiguration ;
std : : unique_ptr < btCollisionDispatcher > mDispatcher ;
std : : shared_ptr < btCollisionWorld > mCollisionWorld ;
Process movement queue in one or several background threads
Before movement calculation, the main thread prepare a
vector of ActorFrameData, which contains all data necessary to perform
the simulation, and feed it to the solver. At the same time it fetches
the result from the previous background simulation, which in turn is
used by the game mechanics.
Other functions of the physics system (weapon hit for instance)
interrupt the background simulation, with some exceptions described
below.
The number of threads is controlled by the numeric setting
[Physics]
async num threads
In case 'async num threads' > 1 and Bullet doesn't support multiple threads,
1 async thread will be used. 0 means synchronous solver.
Additional settings (will be silently switched off if async num threads = 0)
[Physics]
defer aabb update
Update AABBs of actors and objects in the background thread(s). It is not an especially
costly operation, but it needs exclusive access to the collision world, which blocks
other operations. Since AABB needs to be updated for collision detection, one can queue
them to defer update before start of the movement solver. Extensive tests on as much
as one installation (mine) show no drawback having that switched on.
[Physics]
lineofsight keep inactive cache
Control for how long (how many frames) the line of sight (LOS) request will be kept updated.
When a request for LOS is made for the first time, the background threads are stopped to
service it. From now on, the LOS will be refreshed preemptively as part of the background
routine until it is not required for lineofsight keep inactive cache frames. This mean
that subsequent request will not interrupt the background computation.
2020-10-15 04:11:44 +00:00
std : : unique_ptr < PhysicsTaskScheduler > mTaskScheduler ;
2015-05-10 00:08:25 +00:00
2017-04-28 15:30:26 +00:00
std : : unique_ptr < Resource : : BulletShapeManager > mShapeManager ;
2016-02-06 15:57:54 +00:00
Resource : : ResourceSystem * mResourceSystem ;
2015-05-12 01:02:15 +00:00
2020-10-14 09:32:12 +00:00
using ObjectMap = std : : map < MWWorld : : ConstPtr , std : : shared_ptr < Object > > ;
2015-05-12 01:02:15 +00:00
ObjectMap mObjects ;
2015-11-19 22:33:08 +00:00
std : : set < Object * > mAnimatedObjects ; // stores pointers to elements in mObjects
2015-05-12 01:02:15 +00:00
ActorMap mActors ;
2020-10-23 18:27:07 +00:00
using ProjectileMap = std : : map < int , std : : shared_ptr < Projectile > > ;
2019-02-13 07:30:16 +00:00
ProjectileMap mProjectiles ;
2020-10-14 09:32:12 +00:00
using HeightFieldMap = std : : map < std : : pair < int , int > , HeightField * > ;
2015-05-10 00:08:25 +00:00
HeightFieldMap mHeightFields ;
2015-05-02 22:39:01 +00:00
bool mDebugDrawEnabled ;
2020-10-14 09:32:12 +00:00
using PtrVelocityList = std : : vector < std : : pair < MWWorld : : Ptr , osg : : Vec3f > > ;
2013-08-17 14:48:45 +00:00
PtrVelocityList mMovementQueue ;
2013-08-20 18:31:49 +00:00
float mTimeAccum ;
2020-10-23 18:27:07 +00:00
unsigned int mProjectileId ;
2019-02-13 07:30:16 +00:00
2014-10-05 20:24:11 +00:00
float mWaterHeight ;
2017-04-19 15:28:27 +00:00
bool mWaterEnabled ;
2014-10-05 20:24:11 +00:00
2017-04-28 15:30:26 +00:00
std : : unique_ptr < btCollisionObject > mWaterCollisionObject ;
std : : unique_ptr < btCollisionShape > mWaterCollisionShape ;
2014-10-05 20:24:11 +00:00
2017-04-28 15:30:26 +00:00
std : : unique_ptr < MWRender : : DebugDrawer > mDebugDrawer ;
2015-05-02 22:39:01 +00:00
osg : : ref_ptr < osg : : Group > mParentNode ;
2017-09-26 17:19:53 +00:00
float mPhysicsDt ;
2011-08-22 19:34:51 +00:00
PhysicsSystem ( const PhysicsSystem & ) ;
PhysicsSystem & operator = ( const PhysicsSystem & ) ;
2011-08-01 13:55:36 +00:00
} ;
}
# endif