BsVector3.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVector3.h"
  4. #include "BsVector4.h"
  5. #include "BsMath.h"
  6. namespace BansheeEngine
  7. {
  8. Vector3::Vector3(const Vector4& vec)
  9. :x(vec.x), y(vec.y), z(vec.z)
  10. {
  11. }
  12. Radian Vector3::angleBetween(const Vector3& dest) const
  13. {
  14. float lenProduct = length() * dest.length();
  15. // Divide by zero check
  16. if (lenProduct < 1e-6f)
  17. lenProduct = 1e-6f;
  18. float f = dot(dest) / lenProduct;
  19. f = Math::clamp(f, -1.0f, 1.0f);
  20. return Math::acos(f);
  21. }
  22. Vector3 Vector3::normalize(const Vector3& val)
  23. {
  24. float len = Math::sqrt(val.x * val.x + val.y * val.y + val.z * val.z);
  25. // Will also work for zero-sized vectors, but will change nothing
  26. if (len > 1e-08)
  27. {
  28. float invLen = 1.0f / len;
  29. Vector3 normalizedVec;
  30. normalizedVec.x = val.x * invLen;
  31. normalizedVec.y = val.y * invLen;
  32. normalizedVec.z = val.z * invLen;
  33. return normalizedVec;
  34. }
  35. else
  36. return val;
  37. }
  38. bool Vector3::isNaN() const
  39. {
  40. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
  41. }
  42. const Vector3 Vector3::ZERO(0, 0, 0);
  43. const Vector3 Vector3::ONE(1, 1, 1);
  44. const Vector3 Vector3::INF =
  45. Vector3(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  46. const Vector3 Vector3::UNIT_X(1, 0, 0);
  47. const Vector3 Vector3::UNIT_Y(0, 1, 0);
  48. const Vector3 Vector3::UNIT_Z(0, 0, 1);
  49. }