1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-08 09:37:53 +00:00
OpenMW/apps/openmw/mwmechanics/steering.cpp
dteviot 96e3933ee9 Fixed bug in smoothTurn()
Now correctly handles changing direction from 178 to -178 degrees.
2015-09-06 17:39:48 +12:00

49 lines
1.3 KiB
C++

#include "steering.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwbase/environment.hpp"
#include "movement.hpp"
namespace MWMechanics
{
bool smoothTurn(const MWWorld::Ptr& actor, float targetAngleRadians, int axis, float epsilonRadians)
{
float currentAngle (actor.getRefData().getPosition().rot[axis]);
float diff (targetAngleRadians - currentAngle);
if (std::abs(diff) >= osg::DegreesToRadians(180.f))
{
if (diff >= 0)
{
diff = diff - osg::DegreesToRadians(360.f);
}
else
{
diff = osg::DegreesToRadians(360.f) + diff;
}
}
float absDiff = std::abs(diff);
// The turning animation actually moves you slightly, so the angle will be wrong again.
// Use epsilon to prevent jerkiness.
if (absDiff < epsilonRadians)
return true;
float limit = MAX_VEL_ANGULAR_RADIANS * MWBase::Environment::get().getFrameDuration();
if (absDiff > limit)
diff = osg::sign(diff) * limit;
actor.getClass().getMovementSettings(actor).mRotation[axis] = diff;
return false;
}
bool zTurn(const MWWorld::Ptr& actor, float targetAngleRadians, float epsilonRadians)
{
return smoothTurn(actor, targetAngleRadians, 2, epsilonRadians);
}
}