1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00

Merge branch 'pathfinding_fix' into 'master'

Pathfinding fixes

See merge request OpenMW/openmw!576
This commit is contained in:
Petr Mikheev 2021-01-29 23:07:53 +00:00
commit e83a9cdc89
3 changed files with 29 additions and 15 deletions

View File

@ -96,6 +96,7 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f&
const float distToTarget = distance(position, dest); const float distToTarget = distance(position, dest);
const bool isDestReached = (distToTarget <= destTolerance); const bool isDestReached = (distToTarget <= destTolerance);
const bool actorCanMoveByZ = canActorMoveByZAxis(actor);
if (!isDestReached && mTimer > AI_REACTION_TIME) if (!isDestReached && mTimer > AI_REACTION_TIME)
{ {
@ -104,7 +105,6 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f&
const bool wasShortcutting = mIsShortcutting; const bool wasShortcutting = mIsShortcutting;
bool destInLOS = false; bool destInLOS = false;
const bool actorCanMoveByZ = canActorMoveByZAxis(actor);
// Prohibit shortcuts for AiWander, if the actor can not move in 3 dimensions. // Prohibit shortcuts for AiWander, if the actor can not move in 3 dimensions.
mIsShortcutting = actorCanMoveByZ mIsShortcutting = actorCanMoveByZ
@ -150,7 +150,9 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f&
+ 1.2 * std::max(halfExtents.x(), halfExtents.y()); + 1.2 * std::max(halfExtents.x(), halfExtents.y());
const float pointTolerance = std::max(MIN_TOLERANCE, actorTolerance); const float pointTolerance = std::max(MIN_TOLERANCE, actorTolerance);
mPathFinder.update(position, pointTolerance, DEFAULT_TOLERANCE); static const bool smoothMovement = Settings::Manager::getBool("smooth movement", "Game");
mPathFinder.update(position, pointTolerance, DEFAULT_TOLERANCE,
/*shortenIfAlmostStraight=*/smoothMovement, actorCanMoveByZ);
if (isDestReached || mPathFinder.checkPathCompleted()) // if path is finished if (isDestReached || mPathFinder.checkPathCompleted()) // if path is finished
{ {
@ -180,7 +182,6 @@ bool MWMechanics::AiPackage::pathTo(const MWWorld::Ptr& actor, const osg::Vec3f&
const auto destination = mPathFinder.getPath().empty() ? dest : mPathFinder.getPath().front(); const auto destination = mPathFinder.getPath().empty() ? dest : mPathFinder.getPath().front();
mObstacleCheck.update(actor, destination, duration); mObstacleCheck.update(actor, destination, duration);
static const bool smoothMovement = Settings::Manager::getBool("smooth movement", "Game");
if (smoothMovement) if (smoothMovement)
{ {
const float smoothTurnReservedDist = 150; const float smoothTurnReservedDist = 150;

View File

@ -97,12 +97,12 @@ namespace
float dotProduct = v1.x() * v3.x() + v1.y() * v3.y(); float dotProduct = v1.x() * v3.x() + v1.y() * v3.y();
float crossProduct = v1.x() * v3.y() - v1.y() * v3.x(); float crossProduct = v1.x() * v3.y() - v1.y() * v3.x();
// Check that the angle between v1 and v3 is less or equal than 10 degrees. // Check that the angle between v1 and v3 is less or equal than 5 degrees.
static const float cos170 = std::cos(osg::PI / 180 * 170); static const float cos175 = std::cos(osg::PI * (175.0 / 180));
bool checkAngle = dotProduct <= cos170 * v1.length() * v3.length(); bool checkAngle = dotProduct <= cos175 * v1.length() * v3.length();
// Check that distance from p2 to the line (p1, p3) is less or equal than `pointTolerance`. // Check that distance from p2 to the line (p1, p3) is less or equal than `pointTolerance`.
bool checkDist = std::abs(crossProduct) <= pointTolerance * (p3 - p1).length() * 2; bool checkDist = std::abs(crossProduct) <= pointTolerance * (p3 - p1).length();
return checkAngle && checkDist; return checkAngle && checkDist;
} }
@ -296,7 +296,8 @@ namespace MWMechanics
return getXAngleToDir(dir); return getXAngleToDir(dir);
} }
void PathFinder::update(const osg::Vec3f& position, const float pointTolerance, const float destinationTolerance) void PathFinder::update(const osg::Vec3f& position, float pointTolerance, float destinationTolerance,
bool shortenIfAlmostStraight, bool canMoveByZ)
{ {
if (mPath.empty()) if (mPath.empty())
return; return;
@ -304,13 +305,24 @@ namespace MWMechanics
while (mPath.size() > 1 && sqrDistanceIgnoreZ(mPath.front(), position) < pointTolerance * pointTolerance) while (mPath.size() > 1 && sqrDistanceIgnoreZ(mPath.front(), position) < pointTolerance * pointTolerance)
mPath.pop_front(); mPath.pop_front();
while (mPath.size() > 2 && isAlmostStraight(mPath[0], mPath[1], mPath[2], pointTolerance)) if (shortenIfAlmostStraight)
mPath.erase(mPath.begin() + 1); {
if (mPath.size() > 1 && isAlmostStraight(position, mPath[0], mPath[1], pointTolerance)) while (mPath.size() > 2 && isAlmostStraight(mPath[0], mPath[1], mPath[2], pointTolerance))
mPath.pop_front(); mPath.erase(mPath.begin() + 1);
if (mPath.size() > 1 && isAlmostStraight(position, mPath[0], mPath[1], pointTolerance))
mPath.pop_front();
}
if (mPath.size() == 1 && sqrDistanceIgnoreZ(mPath.front(), position) < destinationTolerance * destinationTolerance) if (mPath.size() == 1)
mPath.pop_front(); {
float distSqr;
if (canMoveByZ)
distSqr = (mPath.front() - position).length2();
else
distSqr = sqrDistanceIgnoreZ(mPath.front(), position);
if (distSqr < destinationTolerance * destinationTolerance)
mPath.pop_front();
}
} }
void PathFinder::buildStraightPath(const osg::Vec3f& endPoint) void PathFinder::buildStraightPath(const osg::Vec3f& endPoint)

View File

@ -102,7 +102,8 @@ namespace MWMechanics
const DetourNavigator::Flags flags, const DetourNavigator::AreaCosts& areaCosts); const DetourNavigator::Flags flags, const DetourNavigator::AreaCosts& areaCosts);
/// Remove front point if exist and within tolerance /// Remove front point if exist and within tolerance
void update(const osg::Vec3f& position, const float pointTolerance, const float destinationTolerance); void update(const osg::Vec3f& position, float pointTolerance, float destinationTolerance,
bool shortenIfAlmostStraight, bool canMoveByZ);
bool checkPathCompleted() const bool checkPathCompleted() const
{ {