metaforce/Runtime/Character/CTimeScaleFunctions.cpp

108 lines
3.7 KiB
C++
Raw Normal View History

2016-04-11 03:59:54 +00:00
#include "CTimeScaleFunctions.hpp"
2018-01-30 01:04:01 +00:00
#include "zeus/Math.hpp"
2016-04-11 03:59:54 +00:00
namespace urde
{
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale> IVaryingAnimationTimeScale::Clone() const
2016-04-11 03:59:54 +00:00
{
return VClone();
}
2018-01-30 01:04:01 +00:00
float CConstantAnimationTimeScale::VTimeScaleIntegral(float lowerLimit, float upperLimit) const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
return (upperLimit - lowerLimit) * x4_scale;
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
float CConstantAnimationTimeScale::VFindUpperLimit(float lowerLimit, float root) const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
return (root / x4_scale) + lowerLimit;
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale> CConstantAnimationTimeScale::VClone() const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
return std::make_unique<CConstantAnimationTimeScale>(x4_scale);
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale> CConstantAnimationTimeScale::VGetFunctionMirrored(float) const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
return Clone();
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
CLinearAnimationTimeScale::CLinearAnimationTimeScale(const CCharAnimTime& t1, float y1,
const CCharAnimTime& t2, float y2)
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
float y2my1 = y2 - y1;
float t2mt1 = (t2 - t1).GetSeconds();
x4_desc.x4_slope = y2my1 / t2mt1;
x4_desc.x8_yIntercept = y1 - y2my1 / t2mt1 * t1.GetSeconds();
x4_desc.xc_t1 = t1.GetSeconds();
x4_desc.x10_t2 = t2.GetSeconds();
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale>
CLinearAnimationTimeScale::CFunctionDescription::FunctionMirroredAround(float value) const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
float slope = -x4_slope;
float t1 = 2.f * value - x10_t2;
float t2 = 2.f * value - xc_t1;
float newYInt = x8_yIntercept - x4_slope * 2.f * value;
float y1 = slope * t1 + newYInt;
float y2 = slope * t2 + newYInt;
return std::make_unique<CLinearAnimationTimeScale>(t1, y1, t2, y2);
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
float CLinearAnimationTimeScale::VTimeScaleIntegral(float lowerLimit, float upperLimit) const
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
if (lowerLimit <= upperLimit)
return TimeScaleIntegralWithSortedLimits(x4_desc, lowerLimit, upperLimit);
else
return -TimeScaleIntegralWithSortedLimits(x4_desc, upperLimit, lowerLimit);
}
2016-04-11 03:59:54 +00:00
2018-01-30 01:04:01 +00:00
float CLinearAnimationTimeScale::TimeScaleIntegralWithSortedLimits(const CFunctionDescription& desc,
float lowerLimit, float upperLimit)
{
float lowerEval = desc.x4_slope * lowerLimit + desc.x8_yIntercept;
float upperEval = desc.x4_slope * upperLimit + desc.x8_yIntercept;
return (upperLimit - lowerLimit) * 0.5f * (lowerEval + upperEval);
}
2016-04-11 03:59:54 +00:00
2018-01-30 01:04:01 +00:00
float CLinearAnimationTimeScale::VFindUpperLimit(float lowerLimit, float root) const
{
return FindUpperLimitFromRoot(x4_desc, lowerLimit, root);
2016-04-11 03:59:54 +00:00
}
2018-01-30 01:04:01 +00:00
float CLinearAnimationTimeScale::FindUpperLimitFromRoot(const CFunctionDescription& desc,
float lowerLimit, float root)
2016-04-11 03:59:54 +00:00
{
2018-01-30 01:04:01 +00:00
float M = 0.5f * desc.x4_slope;
float upperLimit = lowerLimit;
float m = 2.f * M;
float lowerIntegration = M * lowerLimit * lowerLimit + desc.x8_yIntercept * lowerLimit;
for (int i=0 ; i<16 ; ++i)
{
float factor = (M * upperLimit * upperLimit + desc.x8_yIntercept * upperLimit - lowerIntegration - root) /
(m * upperLimit + desc.x8_yIntercept);
upperLimit -= factor;
if (zeus::close_enough(factor, 0.f))
return upperLimit;
}
return -1.f;
}
2016-04-11 03:59:54 +00:00
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale> CLinearAnimationTimeScale::VClone() const
{
float y1 = x4_desc.x4_slope * x4_desc.xc_t1 + x4_desc.x8_yIntercept;
float y2 = x4_desc.x4_slope * x4_desc.x10_t2 + x4_desc.x8_yIntercept;
return std::make_unique<CLinearAnimationTimeScale>(x4_desc.xc_t1, y1, x4_desc.x10_t2, y2);
}
2016-04-11 03:59:54 +00:00
2018-01-30 01:04:01 +00:00
std::unique_ptr<IVaryingAnimationTimeScale>
CLinearAnimationTimeScale::VGetFunctionMirrored(float value) const
{
return x4_desc.FunctionMirroredAround(value);
2016-04-11 03:59:54 +00:00
}
}