Quaternion.cpp 1.2 KB

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