1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-29 06:45:34 +00:00
openmw/components/detournavigator/gettilespositions.cpp
2022-01-25 14:06:53 +00:00

66 lines
2.5 KiB
C++

#include "gettilespositions.hpp"
#include "settings.hpp"
#include "settingsutils.hpp"
#include "tileposition.hpp"
#include "tilebounds.hpp"
#include <components/misc/convert.hpp>
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
namespace DetourNavigator
{
TilesPositionsRange makeTilesPositionsRange(const osg::Vec3f& aabbMin, const osg::Vec3f& aabbMax,
const RecastSettings& settings)
{
osg::Vec3f min = toNavMeshCoordinates(settings, aabbMin);
osg::Vec3f max = toNavMeshCoordinates(settings, aabbMax);
const float border = getBorderSize(settings);
min -= osg::Vec3f(border, border, border);
max += osg::Vec3f(border, border, border);
TilePosition minTile = getTilePosition(settings, min);
TilePosition maxTile = getTilePosition(settings, max);
if (minTile.x() > maxTile.x())
std::swap(minTile.x(), maxTile.x());
if (minTile.y() > maxTile.y())
std::swap(minTile.y(), maxTile.y());
return {minTile, maxTile};
}
TilesPositionsRange makeTilesPositionsRange(const btCollisionShape& shape, const btTransform& transform,
const TileBounds& bounds, const RecastSettings& settings)
{
btVector3 aabbMin;
btVector3 aabbMax;
shape.getAabb(transform, aabbMin, aabbMax);
aabbMin.setX(std::max<btScalar>(aabbMin.x(), bounds.mMin.x()));
aabbMin.setY(std::max<btScalar>(aabbMin.y(), bounds.mMin.y()));
aabbMax.setX(std::min<btScalar>(aabbMax.x(), bounds.mMax.x()));
aabbMax.setY(std::min<btScalar>(aabbMax.y(), bounds.mMax.y()));
return makeTilesPositionsRange(Misc::Convert::toOsg(aabbMin), Misc::Convert::toOsg(aabbMax), settings);
}
TilesPositionsRange makeTilesPositionsRange(const int cellSize, const btVector3& shift,
const RecastSettings& settings)
{
using Misc::Convert::toOsg;
const int halfCellSize = cellSize / 2;
const btTransform transform(btMatrix3x3::getIdentity(), shift);
btVector3 aabbMin = transform(btVector3(-halfCellSize, -halfCellSize, 0));
btVector3 aabbMax = transform(btVector3(halfCellSize, halfCellSize, 0));
aabbMin.setX(std::min(aabbMin.x(), aabbMax.x()));
aabbMin.setY(std::min(aabbMin.y(), aabbMax.y()));
aabbMax.setX(std::max(aabbMin.x(), aabbMax.x()));
aabbMax.setY(std::max(aabbMin.y(), aabbMax.y()));
return makeTilesPositionsRange(toOsg(aabbMin), toOsg(aabbMax), settings);
}
}