Quaternion.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. sincosf(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 = 1.0f / sqrtf(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. }
  37. void Core::Quaternion::toString(BufferString& s) const {
  38. s.append("(");
  39. s.append(xyz[0]).append(" i + ");
  40. s.append(xyz[1]).append(" j + ");
  41. s.append(xyz[2]).append(" k + ");
  42. s.append(w);
  43. s.append(')');
  44. }