Quaternion.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "math/Quaternion.hpp"
  2. Core::Quaternion::Quaternion() : w(1.0f) {
  3. }
  4. Core::Quaternion::Quaternion(const Vector3& axis, float angle) : xyz(axis) {
  5. xyz.normalize();
  6. float factor = 0.0f;
  7. Core::Math::sinCos(Core::Math::degreeToRadian(angle) * 0.5f, factor, w);
  8. xyz *= factor;
  9. }
  10. Core::Quaternion Core::Quaternion::lerp(float f,
  11. const Quaternion& other) const {
  12. Quaternion q;
  13. q.xyz = xyz * (1.0f - f) + other.xyz * f;
  14. q.w = w * (1.0f - f) + other.w * f;
  15. float iLength =
  16. 1.0f / Core::Math::squareRoot(q.xyz.squareLength() + q.w * q.w);
  17. q.xyz *= iLength;
  18. q.w *= iLength;
  19. return q;
  20. }
  21. Core::Quaternion& Core::Quaternion::operator*=(const Quaternion& other) {
  22. float dot = xyz.dot(other.xyz);
  23. xyz = other.xyz * w + xyz * other.w + xyz.cross(other.xyz);
  24. w = w * other.w - dot;
  25. return *this;
  26. }
  27. Core::Quaternion Core::Quaternion::operator*(const Quaternion& other) const {
  28. Quaternion q(*this);
  29. q *= other;
  30. return q;
  31. }
  32. Core::Vector3 Core::Quaternion::operator*(const Vector3& v) const {
  33. Vector3 qv = v * w + xyz.cross(v);
  34. Vector3 qvq = xyz * xyz.dot(v) + qv * w - qv.cross(xyz);
  35. return qvq;
  36. }