1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-04-02 02:06:42 +00:00

Add RayCastingInterface

This commit is contained in:
Petr Mikheev 2020-08-03 22:44:16 +02:00
parent e9b2e9b474
commit 0de6650add
9 changed files with 74 additions and 35 deletions

View file

@ -73,7 +73,7 @@ add_openmw_dir (mwworld
add_openmw_dir (mwphysics add_openmw_dir (mwphysics
physicssystem trace collisiontype actor convert object heightfield closestnotmerayresultcallback physicssystem trace collisiontype actor convert object heightfield closestnotmerayresultcallback
contacttestresultcallback deepestnotmecontacttestresultcallback stepper movementsolver contacttestresultcallback deepestnotmecontacttestresultcallback stepper movementsolver
closestnotmeconvexresultcallback closestnotmeconvexresultcallback raycasting
) )
add_openmw_dir (mwclass add_openmw_dir (mwclass

View file

@ -53,7 +53,7 @@ namespace ESM
namespace MWPhysics namespace MWPhysics
{ {
class PhysicsSystem; class RayCastingInterface;
} }
namespace MWRender namespace MWRender
@ -120,8 +120,6 @@ namespace MWBase
virtual void readRecord (ESM::ESMReader& reader, uint32_t type, virtual void readRecord (ESM::ESMReader& reader, uint32_t type,
const std::map<int, int>& contentFileMap) = 0; const std::map<int, int>& contentFileMap) = 0;
virtual const MWPhysics::PhysicsSystem* getPhysics() const = 0;
virtual MWWorld::CellStore *getExterior (int x, int y) = 0; virtual MWWorld::CellStore *getExterior (int x, int y) = 0;
virtual MWWorld::CellStore *getInterior (const std::string& name) = 0; virtual MWWorld::CellStore *getInterior (const std::string& name) = 0;
@ -310,6 +308,8 @@ namespace MWBase
virtual void updateAnimatedCollisionShape(const MWWorld::Ptr &ptr) = 0; virtual void updateAnimatedCollisionShape(const MWWorld::Ptr &ptr) = 0;
virtual const MWPhysics::RayCastingInterface* getRayCasting() const = 0;
virtual bool castRay (float x1, float y1, float z1, float x2, float y2, float z2, int mask) = 0; virtual bool castRay (float x1, float y1, float z1, float x2, float y2, float z2, int mask) = 0;
///< cast a Ray and return true if there is an object in the ray path. ///< cast a Ray and return true if there is an object in the ray path.

View file

@ -179,7 +179,7 @@ namespace MWPhysics
{ {
// First of all, try to hit where you aim to // First of all, try to hit where you aim to
int hitmask = CollisionType_World | CollisionType_Door | CollisionType_HeightMap | CollisionType_Actor; int hitmask = CollisionType_World | CollisionType_Door | CollisionType_HeightMap | CollisionType_Actor;
RayResult result = castRay(origin, origin + (orient * osg::Vec3f(0.0f, queryDistance, 0.0f)), actor, targets, hitmask, CollisionType_Actor); RayCastingResult result = castRay(origin, origin + (orient * osg::Vec3f(0.0f, queryDistance, 0.0f)), actor, targets, hitmask, CollisionType_Actor);
if (result.mHit) if (result.mHit)
{ {
@ -262,7 +262,7 @@ namespace MWPhysics
return (point - Misc::Convert::toOsg(cb.m_hitPointWorld)).length(); return (point - Misc::Convert::toOsg(cb.m_hitPointWorld)).length();
} }
PhysicsSystem::RayResult PhysicsSystem::castRay(const osg::Vec3f &from, const osg::Vec3f &to, const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr> targets, int mask, int group) const RayCastingResult PhysicsSystem::castRay(const osg::Vec3f &from, const osg::Vec3f &to, const MWWorld::ConstPtr& ignore, std::vector<MWWorld::Ptr> targets, int mask, int group) const
{ {
btVector3 btFrom = Misc::Convert::toBullet(from); btVector3 btFrom = Misc::Convert::toBullet(from);
btVector3 btTo = Misc::Convert::toBullet(to); btVector3 btTo = Misc::Convert::toBullet(to);
@ -299,7 +299,7 @@ namespace MWPhysics
mCollisionWorld->rayTest(btFrom, btTo, resultCallback); mCollisionWorld->rayTest(btFrom, btTo, resultCallback);
RayResult result; RayCastingResult result;
result.mHit = resultCallback.hasHit(); result.mHit = resultCallback.hasHit();
if (resultCallback.hasHit()) if (resultCallback.hasHit())
{ {
@ -311,7 +311,7 @@ namespace MWPhysics
return result; return result;
} }
PhysicsSystem::RayResult PhysicsSystem::castSphere(const osg::Vec3f &from, const osg::Vec3f &to, float radius) const RayCastingResult PhysicsSystem::castSphere(const osg::Vec3f &from, const osg::Vec3f &to, float radius) const
{ {
btCollisionWorld::ClosestConvexResultCallback callback(Misc::Convert::toBullet(from), Misc::Convert::toBullet(to)); btCollisionWorld::ClosestConvexResultCallback callback(Misc::Convert::toBullet(from), Misc::Convert::toBullet(to));
callback.m_collisionFilterGroup = 0xff; callback.m_collisionFilterGroup = 0xff;
@ -325,7 +325,7 @@ namespace MWPhysics
mCollisionWorld->convexSweepTest(&shape, from_, to_, callback); mCollisionWorld->convexSweepTest(&shape, from_, to_, callback);
RayResult result; RayCastingResult result;
result.mHit = callback.hasHit(); result.mHit = callback.hasHit();
if (result.mHit) if (result.mHit)
{ {
@ -346,7 +346,7 @@ namespace MWPhysics
osg::Vec3f pos1 (physactor1->getCollisionObjectPosition() + osg::Vec3f(0,0,physactor1->getHalfExtents().z() * 0.9)); // eye level osg::Vec3f pos1 (physactor1->getCollisionObjectPosition() + osg::Vec3f(0,0,physactor1->getHalfExtents().z() * 0.9)); // eye level
osg::Vec3f pos2 (physactor2->getCollisionObjectPosition() + osg::Vec3f(0,0,physactor2->getHalfExtents().z() * 0.9)); osg::Vec3f pos2 (physactor2->getCollisionObjectPosition() + osg::Vec3f(0,0,physactor2->getHalfExtents().z() * 0.9));
RayResult result = castRay(pos1, pos2, MWWorld::ConstPtr(), std::vector<MWWorld::Ptr>(), CollisionType_World|CollisionType_HeightMap|CollisionType_Door); RayCastingResult result = castRay(pos1, pos2, MWWorld::ConstPtr(), std::vector<MWWorld::Ptr>(), CollisionType_World|CollisionType_HeightMap|CollisionType_Door);
return !result.mHit; return !result.mHit;
} }

View file

@ -13,6 +13,7 @@
#include "../mwworld/ptr.hpp" #include "../mwworld/ptr.hpp"
#include "collisiontype.hpp" #include "collisiontype.hpp"
#include "raycasting.hpp"
namespace osg namespace osg
{ {
@ -52,7 +53,7 @@ namespace MWPhysics
class Object; class Object;
class Actor; class Actor;
class PhysicsSystem class PhysicsSystem : public RayCastingInterface
{ {
public: public:
PhysicsSystem (Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Group> parentNode); PhysicsSystem (Resource::ResourceSystem* resourceSystem, osg::ref_ptr<osg::Group> parentNode);
@ -108,25 +109,17 @@ namespace MWPhysics
/// target vector hits the collision shape and then calculates distance from the intersection point. /// 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. /// 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. /// \note Only Actor targets are supported at the moment.
float getHitDistance(const osg::Vec3f& point, const MWWorld::ConstPtr& target) const; float getHitDistance(const osg::Vec3f& point, const MWWorld::ConstPtr& target) const final;
struct RayResult
{
bool mHit;
osg::Vec3f mHitPos;
osg::Vec3f mHitNormal;
MWWorld::Ptr mHitObject;
};
/// @param me Optional, a Ptr to ignore in the list of results. targets are actors to filter for, ignoring all other actors. /// @param me Optional, a Ptr to ignore in the list of results. targets are actors to filter for, ignoring all other actors.
RayResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, const MWWorld::ConstPtr& ignore = MWWorld::ConstPtr(), RayCastingResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, const MWWorld::ConstPtr& ignore = MWWorld::ConstPtr(),
std::vector<MWWorld::Ptr> targets = std::vector<MWWorld::Ptr>(), std::vector<MWWorld::Ptr> targets = std::vector<MWWorld::Ptr>(),
int mask = CollisionType_World|CollisionType_HeightMap|CollisionType_Actor|CollisionType_Door, int group=0xff) const; int mask = CollisionType_World|CollisionType_HeightMap|CollisionType_Actor|CollisionType_Door, int group=0xff) const final;
RayResult castSphere(const osg::Vec3f& from, const osg::Vec3f& to, float radius) const; RayCastingResult castSphere(const osg::Vec3f& from, const osg::Vec3f& to, float radius) const final;
/// Return true if actor1 can see actor2. /// Return true if actor1 can see actor2.
bool getLineOfSight(const MWWorld::ConstPtr& actor1, const MWWorld::ConstPtr& actor2) const; bool getLineOfSight(const MWWorld::ConstPtr& actor1, const MWWorld::ConstPtr& actor2) const final;
bool isOnGround (const MWWorld::Ptr& actor); bool isOnGround (const MWWorld::Ptr& actor);

View file

@ -0,0 +1,41 @@
#ifndef OPENMW_MWPHYSICS_RAYCASTING_H
#define OPENMW_MWPHYSICS_RAYCASTING_H
#include <osg/Vec3f>
#include "../mwworld/ptr.hpp"
#include "collisiontype.hpp"
namespace MWPhysics
{
struct RayCastingResult
{
bool mHit;
osg::Vec3f mHitPos;
osg::Vec3f mHitNormal;
MWWorld::Ptr mHitObject;
};
class RayCastingInterface
{
public:
/// 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.
virtual float getHitDistance(const osg::Vec3f& point, const MWWorld::ConstPtr& target) const = 0;
/// @param me Optional, a Ptr to ignore in the list of results. targets are actors to filter for, ignoring all other actors.
virtual RayCastingResult castRay(const osg::Vec3f &from, const osg::Vec3f &to, const MWWorld::ConstPtr& ignore = MWWorld::ConstPtr(),
std::vector<MWWorld::Ptr> targets = std::vector<MWWorld::Ptr>(),
int mask = CollisionType_World|CollisionType_HeightMap|CollisionType_Actor|CollisionType_Door, int group=0xff) const = 0;
virtual RayCastingResult castSphere(const osg::Vec3f& from, const osg::Vec3f& to, float radius) const = 0;
/// Return true if actor1 can see actor2.
virtual bool getLineOfSight(const MWWorld::ConstPtr& actor1, const MWWorld::ConstPtr& actor2) const = 0;
};
}
#endif

View file

@ -17,7 +17,7 @@
#include "../mwmechanics/movement.hpp" #include "../mwmechanics/movement.hpp"
#include "../mwmechanics/npcstats.hpp" #include "../mwmechanics/npcstats.hpp"
#include "../mwphysics/physicssystem.hpp" #include "../mwphysics/raycasting.hpp"
#include "npcanimation.hpp" #include "npcanimation.hpp"
@ -218,7 +218,7 @@ namespace MWRender
const float cameraObstacleLimit = 5.0f; const float cameraObstacleLimit = 5.0f;
const float focalObstacleLimit = 10.f; const float focalObstacleLimit = 10.f;
const MWPhysics::PhysicsSystem* physics = MWBase::Environment::get().getWorld()->getPhysics(); const auto* rayCasting = MWBase::Environment::get().getWorld()->getRayCasting();
// Adjust focal point to prevent clipping. // Adjust focal point to prevent clipping.
osg::Vec3d focal = getFocalPoint(); osg::Vec3d focal = getFocalPoint();
@ -226,7 +226,7 @@ namespace MWRender
float offsetLen = focalOffset.length(); float offsetLen = focalOffset.length();
if (offsetLen > 0) if (offsetLen > 0)
{ {
MWPhysics::PhysicsSystem::RayResult result = physics->castSphere(focal - focalOffset, focal, focalObstacleLimit); MWPhysics::RayCastingResult result = rayCasting->castSphere(focal - focalOffset, focal, focalObstacleLimit);
if (result.mHit) if (result.mHit)
{ {
double adjustmentCoef = -(result.mHitPos + result.mHitNormal * focalObstacleLimit - focal).length() / offsetLen; double adjustmentCoef = -(result.mHitPos + result.mHitNormal * focalObstacleLimit - focal).length() / offsetLen;
@ -240,7 +240,7 @@ namespace MWRender
mCameraDistance = std::min(mCameraDistance, mMaxNextCameraDistance); mCameraDistance = std::min(mCameraDistance, mMaxNextCameraDistance);
osg::Vec3d cameraPos; osg::Vec3d cameraPos;
getPosition(focal, cameraPos); getPosition(focal, cameraPos);
MWPhysics::PhysicsSystem::RayResult result = physics->castSphere(focal, cameraPos, cameraObstacleLimit); MWPhysics::RayCastingResult result = rayCasting->castSphere(focal, cameraPos, cameraObstacleLimit);
if (result.mHit) if (result.mHit)
mCameraDistance = (result.mHitPos + result.mHitNormal * cameraObstacleLimit - focal).length(); mCameraDistance = (result.mHitPos + result.mHitNormal * cameraObstacleLimit - focal).length();
} }

View file

@ -424,7 +424,7 @@ namespace MWWorld
// Check for impact // Check for impact
// TODO: use a proper btRigidBody / btGhostObject? // TODO: use a proper btRigidBody / btGhostObject?
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castRay(pos, newPos, caster, targetActors, 0xff, MWPhysics::CollisionType_Projectile); MWPhysics::RayCastingResult result = mPhysics->castRay(pos, newPos, caster, targetActors, 0xff, MWPhysics::CollisionType_Projectile);
bool hit = false; bool hit = false;
if (result.mHit) if (result.mHit)
@ -500,7 +500,7 @@ namespace MWWorld
// Check for impact // Check for impact
// TODO: use a proper btRigidBody / btGhostObject? // TODO: use a proper btRigidBody / btGhostObject?
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castRay(pos, newPos, caster, targetActors, 0xff, MWPhysics::CollisionType_Projectile); MWPhysics::RayCastingResult result = mPhysics->castRay(pos, newPos, caster, targetActors, 0xff, MWPhysics::CollisionType_Projectile);
bool underwater = MWBase::Environment::get().getWorld()->isUnderwater(MWMechanics::getPlayer().getCell(), newPos); bool underwater = MWBase::Environment::get().getWorld()->isUnderwater(MWMechanics::getPlayer().getCell(), newPos);

View file

@ -1542,6 +1542,11 @@ namespace MWWorld
return mNavigator->updateObject(DetourNavigator::ObjectId(object), shapes, object->getCollisionObject()->getWorldTransform()); return mNavigator->updateObject(DetourNavigator::ObjectId(object), shapes, object->getCollisionObject()->getWorldTransform());
} }
const MWPhysics::RayCastingInterface* World::getRayCasting() const
{
return mPhysics.get();
}
bool World::castRay (float x1, float y1, float z1, float x2, float y2, float z2) bool World::castRay (float x1, float y1, float z1, float x2, float y2, float z2)
{ {
int mask = MWPhysics::CollisionType_World | MWPhysics::CollisionType_Door; int mask = MWPhysics::CollisionType_World | MWPhysics::CollisionType_Door;
@ -1554,7 +1559,7 @@ namespace MWWorld
osg::Vec3f a(x1,y1,z1); osg::Vec3f a(x1,y1,z1);
osg::Vec3f b(x2,y2,z2); osg::Vec3f b(x2,y2,z2);
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castRay(a, b, MWWorld::Ptr(), std::vector<MWWorld::Ptr>(), mask); MWPhysics::RayCastingResult result = mPhysics->castRay(a, b, MWWorld::Ptr(), std::vector<MWWorld::Ptr>(), mask);
return result.mHit; return result.mHit;
} }
@ -2728,7 +2733,7 @@ namespace MWWorld
if (includeWater) { if (includeWater) {
collisionTypes |= MWPhysics::CollisionType_Water; collisionTypes |= MWPhysics::CollisionType_Water;
} }
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castRay(from, to, MWWorld::Ptr(), std::vector<MWWorld::Ptr>(), collisionTypes); MWPhysics::RayCastingResult result = mPhysics->castRay(from, to, MWWorld::Ptr(), std::vector<MWWorld::Ptr>(), collisionTypes);
if (!result.mHit) if (!result.mHit)
return maxDist; return maxDist;
@ -3091,7 +3096,7 @@ namespace MWWorld
actor.getClass().getCreatureStats(actor).getAiSequence().getCombatTargets(targetActors); actor.getClass().getCreatureStats(actor).getAiSequence().getCombatTargets(targetActors);
// Check for impact, if yes, handle hit, if not, launch projectile // Check for impact, if yes, handle hit, if not, launch projectile
MWPhysics::PhysicsSystem::RayResult result = mPhysics->castRay(sourcePos, worldPos, actor, targetActors, 0xff, MWPhysics::CollisionType_Projectile); MWPhysics::RayCastingResult result = mPhysics->castRay(sourcePos, worldPos, actor, targetActors, 0xff, MWPhysics::CollisionType_Projectile);
if (result.mHit) if (result.mHit)
MWMechanics::projectileHit(actor, result.mHitObject, bow, projectile, result.mHitPos, attackStrength); MWMechanics::projectileHit(actor, result.mHitObject, bow, projectile, result.mHitPos, attackStrength);
else else

View file

@ -214,8 +214,6 @@ namespace MWWorld
void readRecord (ESM::ESMReader& reader, uint32_t type, void readRecord (ESM::ESMReader& reader, uint32_t type,
const std::map<int, int>& contentFileMap) override; const std::map<int, int>& contentFileMap) override;
const MWPhysics::PhysicsSystem* getPhysics() const override { return mPhysics.get(); }
CellStore *getExterior (int x, int y) override; CellStore *getExterior (int x, int y) override;
CellStore *getInterior (const std::string& name) override; CellStore *getInterior (const std::string& name) override;
@ -412,6 +410,8 @@ namespace MWWorld
void updateAnimatedCollisionShape(const Ptr &ptr) override; void updateAnimatedCollisionShape(const Ptr &ptr) override;
const MWPhysics::RayCastingInterface* getRayCasting() const override;
bool castRay (float x1, float y1, float z1, float x2, float y2, float z2, int mask) override; bool castRay (float x1, float y1, float z1, float x2, float y2, float z2, int mask) override;
///< cast a Ray and return true if there is an object in the ray path. ///< cast a Ray and return true if there is an object in the ray path.