From 2a7b1054840dd0934383bdccf900c7cf8ce98e3a Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 14 May 2023 14:06:53 +0200 Subject: [PATCH] Use different object id for avoid shape Otherwise addObject will ignore it as a duplicate and resulting recastmesh will not match generated by the engine causing navmeshdb cache miss. --- apps/navmeshtool/worldspacedata.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/navmeshtool/worldspacedata.cpp b/apps/navmeshtool/worldspacedata.cpp index 19a85d60c6..9712b80b6f 100644 --- a/apps/navmeshtool/worldspacedata.cpp +++ b/apps/navmeshtool/worldspacedata.cpp @@ -3,11 +3,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -17,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -29,8 +32,8 @@ #include #include -#include #include +#include #include #include #include @@ -219,6 +222,16 @@ namespace NavMeshTool const std::vector data = serialize(value); getRawStderr().write(reinterpret_cast(data.data()), static_cast(data.size())); } + + std::string makeAddObjectErrorMessage( + ObjectId objectId, DetourNavigator::AreaType areaType, const CollisionShape& shape) + { + std::ostringstream stream; + stream << "Failed to add object to recast mesh objectId=" << objectId.value() << " areaType=" << areaType + << " fileName=" << shape.getInstance()->mFileName + << " fileHash=" << Misc::StringUtils::toHex(shape.getInstance()->mFileHash); + return stream.str(); + } } WorldspaceNavMeshInput::WorldspaceNavMeshInput( @@ -318,14 +331,19 @@ namespace NavMeshTool const CollisionShape shape(object.getShapeInstance(), *object.getCollisionObject().getCollisionShape(), object.getObjectTransform()); - navMeshInput.mTileCachedRecastMeshManager.addObject( - objectId, shape, transform, DetourNavigator::AreaType_ground, guard.get()); + if (!navMeshInput.mTileCachedRecastMeshManager.addObject( + objectId, shape, transform, DetourNavigator::AreaType_ground, guard.get())) + throw std::logic_error( + makeAddObjectErrorMessage(objectId, DetourNavigator::AreaType_ground, shape)); if (const btCollisionShape* avoid = object.getShapeInstance()->mAvoidCollisionShape.get()) { + const ObjectId avoidObjectId(++objectsCounter); const CollisionShape avoidShape(object.getShapeInstance(), *avoid, object.getObjectTransform()); - navMeshInput.mTileCachedRecastMeshManager.addObject( - objectId, avoidShape, transform, DetourNavigator::AreaType_null, guard.get()); + if (!navMeshInput.mTileCachedRecastMeshManager.addObject( + avoidObjectId, avoidShape, transform, DetourNavigator::AreaType_null, guard.get())) + throw std::logic_error( + makeAddObjectErrorMessage(avoidObjectId, DetourNavigator::AreaType_null, avoidShape)); } data.mObjects.emplace_back(std::move(object));