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

Merge branch 'fix_circling_fargoth' into 'master'

Remove unnecessary loops from path (#6510)

Closes #6510

See merge request OpenMW/openmw!1584
This commit is contained in:
psi29a 2022-01-29 19:48:02 +00:00
commit 7bab714825
2 changed files with 35 additions and 4 deletions

View File

@ -6,6 +6,7 @@
#include <components/detournavigator/navigatorutils.hpp>
#include <components/debug/debuglog.hpp>
#include <components/misc/coordinateconverter.hpp>
#include <components/misc/math.hpp>
#include "../mwbase/world.hpp"
#include "../mwbase/environment.hpp"
@ -315,12 +316,13 @@ namespace MWMechanics
while (mPath.size() > 1 && sqrDistanceIgnoreZ(mPath.front(), position) < pointTolerance * pointTolerance)
mPath.pop_front();
const IsValidShortcut isValidShortcut {
MWBase::Environment::get().getWorld()->getNavigator(),
halfExtents, flags
};
if (shortenIfAlmostStraight)
{
const IsValidShortcut isValidShortcut {
MWBase::Environment::get().getWorld()->getNavigator(),
halfExtents, flags
};
while (mPath.size() > 2 && isAlmostStraight(mPath[0], mPath[1], mPath[2], pointTolerance)
&& isValidShortcut(mPath[0], mPath[2]))
mPath.erase(mPath.begin() + 1);
@ -329,6 +331,19 @@ namespace MWMechanics
mPath.pop_front();
}
if (mPath.size() > 1)
{
std::size_t begin = 0;
for (std::size_t i = 1; i < mPath.size(); ++i)
{
const float sqrDistance = Misc::getVectorToLine(position, mPath[i - 1], mPath[i]).length2();
if (sqrDistance < pointTolerance * pointTolerance && isValidShortcut(position, mPath[i]))
begin = i;
}
for (std::size_t i = 0; i < begin; ++i)
mPath.pop_front();
}
if (mPath.size() == 1)
{
float distSqr;

16
components/misc/math.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef OPENMW_COMPONENTS_MISC_MATH_H
#define OPENMW_COMPONENTS_MISC_MATH_H
#include <osg/Vec3f>
namespace Misc
{
inline osg::Vec3f getVectorToLine(const osg::Vec3f& position, const osg::Vec3f& a, const osg::Vec3f& b)
{
osg::Vec3f direction = b - a;
direction.normalize();
return (position - a) ^ direction;
}
}
#endif