1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-29 04:06:40 +00:00

Support btCompoundShape for RecastMesh

This commit is contained in:
elsid 2018-04-03 00:04:19 +03:00
parent 373adc6ec4
commit dd5e6a61a3
No known key found for this signature in database
GPG key ID: B845CB9FEE18AB40
11 changed files with 59 additions and 42 deletions

View file

@ -3,6 +3,7 @@
#include <limits> #include <limits>
#include <BulletCollision/CollisionDispatch/btCollisionObject.h> #include <BulletCollision/CollisionDispatch/btCollisionObject.h>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/loadinglistener/loadinglistener.hpp> #include <components/loadinglistener/loadinglistener.hpp>
@ -82,14 +83,11 @@ namespace
ptr.getClass().insertObject (ptr, model, physics); ptr.getClass().insertObject (ptr, model, physics);
if (const auto object = physics.getObject(ptr)) if (const auto object = physics.getObject(ptr))
{
if (const auto concaveShape = dynamic_cast<const btConcaveShape*>(object->getShapeInstance()->mCollisionShape))
{ {
const auto navigator = MWBase::Environment::get().getWorld()->getNavigator(); const auto navigator = MWBase::Environment::get().getWorld()->getNavigator();
navigator->addObject(reinterpret_cast<std::size_t>(object), *concaveShape, navigator->addObject(reinterpret_cast<std::size_t>(object), *object->getCollisionObject()->getCollisionShape(),
object->getCollisionObject()->getWorldTransform()); object->getCollisionObject()->getWorldTransform());
} }
}
else if (const auto actor = physics.getActor(ptr)) else if (const auto actor = physics.getActor(ptr))
{ {
const auto navigator = MWBase::Environment::get().getWorld()->getNavigator(); const auto navigator = MWBase::Environment::get().getWorld()->getNavigator();

View file

@ -5,7 +5,14 @@ namespace DetourNavigator
{ {
CachedRecastMeshManager::CachedRecastMeshManager(const Settings& settings) CachedRecastMeshManager::CachedRecastMeshManager(const Settings& settings)
: mImpl(settings) : mImpl(settings)
{}
bool CachedRecastMeshManager::addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform)
{ {
if (!mImpl.addObject(id, shape, transform))
return false;
mCached.reset();
return true;
} }
boost::optional<RecastMeshManager::Object> CachedRecastMeshManager::removeObject(std::size_t id) boost::optional<RecastMeshManager::Object> CachedRecastMeshManager::removeObject(std::size_t id)
@ -21,5 +28,5 @@ namespace DetourNavigator
if (!mCached) if (!mCached)
mCached = mImpl.getMesh(); mCached = mImpl.getMesh();
return mCached; return mCached;
} }
} }

View file

@ -12,14 +12,7 @@ namespace DetourNavigator
public: public:
CachedRecastMeshManager(const Settings& settings); CachedRecastMeshManager(const Settings& settings);
template <class T> bool addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform);
bool addObject(std::size_t id, const T& shape, const btTransform& transform)
{
if (!mImpl.addObject(id, shape, transform))
return false;
mCached.reset();
return true;
}
boost::optional<RecastMeshManager::Object> removeObject(std::size_t id); boost::optional<RecastMeshManager::Object> removeObject(std::size_t id);

View file

@ -25,6 +25,11 @@ namespace DetourNavigator
mNavMeshManager.reset(agentHalfExtents); mNavMeshManager.reset(agentHalfExtents);
} }
bool Navigator::addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform)
{
return mNavMeshManager.addObject(id, shape, transform);
}
bool Navigator::removeObject(std::size_t id) bool Navigator::removeObject(std::size_t id)
{ {
return mNavMeshManager.removeObject(id); return mNavMeshManager.removeObject(id);

View file

@ -17,11 +17,7 @@ namespace DetourNavigator
void removeAgent(const osg::Vec3f& agentHalfExtents); void removeAgent(const osg::Vec3f& agentHalfExtents);
template <class T> bool addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform);
bool addObject(std::size_t id, const T& shape, const btTransform& transform)
{
return mNavMeshManager.addObject(id, shape, transform);
}
bool removeObject(std::size_t id); bool removeObject(std::size_t id);

View file

@ -19,6 +19,15 @@ namespace DetourNavigator
, mAsyncNavMeshUpdater(settings) , mAsyncNavMeshUpdater(settings)
{} {}
bool NavMeshManager::addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform)
{
if (!mRecastMeshManager.addObject(id, shape, transform))
return false;
++mRevision;
addChangedTiles(shape, transform);
return true;
}
bool NavMeshManager::removeObject(std::size_t id) bool NavMeshManager::removeObject(std::size_t id)
{ {
const auto object = mRecastMeshManager.removeObject(id); const auto object = mRecastMeshManager.removeObject(id);

View file

@ -23,15 +23,7 @@ namespace DetourNavigator
public: public:
NavMeshManager(const Settings& settings); NavMeshManager(const Settings& settings);
template <class T> bool addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform);
bool addObject(std::size_t id, const T& shape, const btTransform& transform)
{
if (!mRecastMeshManager.addObject(id, shape, transform))
return false;
++mRevision;
addChangedTiles(shape, transform);
return true;
}
bool removeObject(std::size_t id); bool removeObject(std::size_t id);

View file

@ -1,12 +1,15 @@
#include "recastmeshbuilder.hpp" #include "recastmeshbuilder.hpp"
#include "chunkytrimesh.hpp" #include "chunkytrimesh.hpp"
#include "debug.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "settingsutils.hpp" #include "settingsutils.hpp"
#include "exceptions.hpp"
#include <components/bullethelpers/processtrianglecallback.hpp> #include <components/bullethelpers/processtrianglecallback.hpp>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h> #include <BulletCollision/CollisionShapes/btCompoundShape.h>
#include <BulletCollision/CollisionShapes/btConcaveShape.h> #include <BulletCollision/CollisionShapes/btConcaveShape.h>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
namespace namespace
{ {
@ -22,7 +25,25 @@ namespace DetourNavigator
RecastMeshBuilder::RecastMeshBuilder(const Settings& settings) RecastMeshBuilder::RecastMeshBuilder(const Settings& settings)
: mSettings(settings) : mSettings(settings)
{}
void RecastMeshBuilder::addObject(const btCollisionShape& shape, const btTransform& transform)
{ {
if (shape.isCompound())
return addObject(static_cast<const btCompoundShape&>(shape), transform);
else if (shape.getShapeType() == TERRAIN_SHAPE_PROXYTYPE)
return addObject(static_cast<const btHeightfieldTerrainShape&>(shape), transform);
else if (shape.isConcave())
return addObject(static_cast<const btConcaveShape&>(shape), transform);
std::ostringstream message;
message << "Unsupported shape type: " << BroadphaseNativeTypes(shape.getShapeType());
throw InvalidArgument(message.str());
}
void RecastMeshBuilder::addObject(const btCompoundShape& shape, const btTransform& transform)
{
for (int i = 0, num = shape.getNumChildShapes(); i < num; ++i)
addObject(*shape.getChildShape(i), transform * shape.getChildTransform(i));
} }
void RecastMeshBuilder::addObject(const btConcaveShape& shape, const btTransform& transform) void RecastMeshBuilder::addObject(const btConcaveShape& shape, const btTransform& transform)

View file

@ -3,6 +3,8 @@
#include "recastmesh.hpp" #include "recastmesh.hpp"
class btCollisionShape;
class btCompoundShape;
class btConcaveShape; class btConcaveShape;
class btHeightfieldTerrainShape; class btHeightfieldTerrainShape;
class btTransform; class btTransform;
@ -16,6 +18,10 @@ namespace DetourNavigator
public: public:
RecastMeshBuilder(const Settings& settings); RecastMeshBuilder(const Settings& settings);
void addObject(const btCollisionShape& shape, const btTransform& transform);
void addObject(const btCompoundShape& shape, const btTransform& transform);
void addObject(const btConcaveShape& shape, const btTransform& transform); void addObject(const btConcaveShape& shape, const btTransform& transform);
void addObject(const btHeightfieldTerrainShape& shape, const btTransform& transform); void addObject(const btHeightfieldTerrainShape& shape, const btTransform& transform);

View file

@ -9,15 +9,7 @@ namespace DetourNavigator
, mMeshBuilder(settings) , mMeshBuilder(settings)
{} {}
bool RecastMeshManager::addObject(std::size_t id, const btHeightfieldTerrainShape& shape, const btTransform& transform) bool RecastMeshManager::addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform)
{
if (!mObjects.insert(std::make_pair(id, Object {&shape, transform})).second)
return false;
mShouldRebuild = true;
return true;
}
bool RecastMeshManager::addObject(std::size_t id, const btConcaveShape& shape, const btTransform& transform)
{ {
if (!mObjects.insert(std::make_pair(id, Object {&shape, transform})).second) if (!mObjects.insert(std::make_pair(id, Object {&shape, transform})).second)
return false; return false;

View file

@ -24,9 +24,7 @@ namespace DetourNavigator
RecastMeshManager(const Settings& settings); RecastMeshManager(const Settings& settings);
bool addObject(std::size_t id, const btHeightfieldTerrainShape& shape, const btTransform& transform); bool addObject(std::size_t id, const btCollisionShape& shape, const btTransform& transform);
bool addObject(std::size_t id, const btConcaveShape& shape, const btTransform& transform);
boost::optional<Object> removeObject(std::size_t id); boost::optional<Object> removeObject(std::size_t id);