2014-01-29 19:29:07 +00:00
|
|
|
#include "steering.hpp"
|
|
|
|
|
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/ptr.hpp"
|
|
|
|
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
|
|
|
|
#include "movement.hpp"
|
|
|
|
|
|
|
|
namespace MWMechanics
|
|
|
|
{
|
|
|
|
|
2015-06-03 17:41:19 +00:00
|
|
|
bool smoothTurn(const MWWorld::Ptr& actor, float targetAngleRadians, int axis, float epsilonRadians)
|
2014-04-25 20:20:55 +00:00
|
|
|
{
|
2015-06-03 17:41:19 +00:00
|
|
|
float currentAngle (actor.getRefData().getPosition().rot[axis]);
|
|
|
|
float diff (targetAngleRadians - currentAngle);
|
2015-09-06 05:39:48 +00:00
|
|
|
if (std::abs(diff) >= osg::DegreesToRadians(180.f))
|
2014-04-25 20:20:55 +00:00
|
|
|
{
|
2015-09-06 05:39:48 +00:00
|
|
|
if (diff >= 0)
|
|
|
|
{
|
|
|
|
diff = diff - osg::DegreesToRadians(360.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
diff = osg::DegreesToRadians(360.f) + diff;
|
|
|
|
}
|
2014-04-25 20:20:55 +00:00
|
|
|
}
|
2015-06-03 17:41:19 +00:00
|
|
|
float absDiff = std::abs(diff);
|
2014-04-25 20:20:55 +00:00
|
|
|
|
|
|
|
// The turning animation actually moves you slightly, so the angle will be wrong again.
|
|
|
|
// Use epsilon to prevent jerkiness.
|
2015-06-03 17:41:19 +00:00
|
|
|
if (absDiff < epsilonRadians)
|
2014-04-25 20:20:55 +00:00
|
|
|
return true;
|
|
|
|
|
2020-08-27 11:48:59 +00:00
|
|
|
float limit = getAngularVelocity(actor.getClass().getMaxSpeed(actor)) * MWBase::Environment::get().getFrameDuration();
|
2014-04-25 20:20:55 +00:00
|
|
|
if (absDiff > limit)
|
2015-06-03 17:41:19 +00:00
|
|
|
diff = osg::sign(diff) * limit;
|
2014-04-25 20:20:55 +00:00
|
|
|
|
2015-06-03 17:41:19 +00:00
|
|
|
actor.getClass().getMovementSettings(actor).mRotation[axis] = diff;
|
2014-04-25 20:20:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-03 17:41:19 +00:00
|
|
|
bool zTurn(const MWWorld::Ptr& actor, float targetAngleRadians, float epsilonRadians)
|
2014-01-29 19:29:07 +00:00
|
|
|
{
|
2015-06-03 17:41:19 +00:00
|
|
|
return smoothTurn(actor, targetAngleRadians, 2, epsilonRadians);
|
2014-01-29 19:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|