zeus/src/CVector2f.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2016-03-04 23:03:26 +00:00
#include "zeus/CVector2f.hpp"
2015-05-04 05:41:38 +00:00
#include <memory.h>
#include <cmath>
#include <assert.h>
2016-03-04 23:03:26 +00:00
#include "zeus/Math.hpp"
2015-05-04 05:41:38 +00:00
2016-03-04 23:03:26 +00:00
namespace zeus
{
2015-05-04 05:41:38 +00:00
const CVector2f CVector2f::skOne = CVector2f(1.0);
const CVector2f CVector2f::skNegOne = CVector2f(-1.0);
2017-03-17 23:30:14 +00:00
const CVector2f CVector2f::skZero(0.f, 0.f);
2015-05-04 05:41:38 +00:00
float CVector2f::getAngleDiff(const CVector2f& a, const CVector2f& b)
{
float mag1 = a.magnitude();
float mag2 = b.magnitude();
2015-05-04 05:41:38 +00:00
if (!mag1 || !mag2)
return 0;
2015-10-25 19:31:41 +00:00
float dot = a.dot(b);
2016-03-04 23:03:26 +00:00
float theta = std::acos(dot / (mag1 * mag2));
2015-05-04 05:41:38 +00:00
return theta;
}
CVector2f CVector2f::slerp(const CVector2f& a, const CVector2f& b, float t)
{
if (t <= 0.0f)
return a;
if (t >= 1.0f)
return b;
CVector2f ret;
2016-03-04 23:03:26 +00:00
float mag = std::sqrt(a.dot(a) * b.dot(b));
2015-05-04 05:41:38 +00:00
float prod = a.dot(b) / mag;
2016-03-04 23:03:26 +00:00
if (std::fabs(prod) < 1.0f)
2015-05-04 05:41:38 +00:00
{
2016-03-04 23:03:26 +00:00
const double sign = (prod < 0.0f) ? -1.0f : 1.0f;
2015-05-04 05:41:38 +00:00
2016-03-04 23:03:26 +00:00
const double theta = std::acos(sign * prod);
const double s1 = std::sin(sign * t * theta);
const double d = 1.0 / std::sin(theta);
const double s0 = std::sin((1.0f - t) * theta);
2015-05-04 05:41:38 +00:00
ret = (a * s0 + b * s1) * d;
2015-05-04 05:41:38 +00:00
return ret;
}
return a;
}
}