BsVector3.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. float Vector3::length() const
  13. {
  14. return Math::sqrt(x * x + y * y + z * z);
  15. }
  16. float Vector3::normalize()
  17. {
  18. float len = Math::sqrt(x * x + y * y + z * z);
  19. // Will also work for zero-sized vectors, but will change nothing
  20. if (len > 1e-08)
  21. {
  22. float invLen = 1.0f / len;
  23. x *= invLen;
  24. y *= invLen;
  25. z *= invLen;
  26. }
  27. return len;
  28. }
  29. Radian Vector3::angleBetween(const Vector3& dest) const
  30. {
  31. float lenProduct = length() * dest.length();
  32. // Divide by zero check
  33. if (lenProduct < 1e-6f)
  34. lenProduct = 1e-6f;
  35. float f = dot(dest) / lenProduct;
  36. f = Math::clamp(f, -1.0f, 1.0f);
  37. return Math::acos(f);
  38. }
  39. Vector3 Vector3::normalize(const Vector3& val)
  40. {
  41. float len = Math::sqrt(val.x * val.x + val.y * val.y + val.z * val.z);
  42. // Will also work for zero-sized vectors, but will change nothing
  43. if (len > 1e-08)
  44. {
  45. float invLen = 1.0f / len;
  46. Vector3 normalizedVec;
  47. normalizedVec.x = val.x * invLen;
  48. normalizedVec.y = val.y * invLen;
  49. normalizedVec.z = val.z * invLen;
  50. return normalizedVec;
  51. }
  52. else
  53. return val;
  54. }
  55. bool Vector3::isNaN() const
  56. {
  57. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
  58. }
  59. const Vector3 Vector3::ZERO(0, 0, 0);
  60. const Vector3 Vector3::ONE(1, 1, 1);
  61. const Vector3 Vector3::INF =
  62. Vector3(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  63. const Vector3 Vector3::UNIT_X(1, 0, 0);
  64. const Vector3 Vector3::UNIT_Y(0, 1, 0);
  65. const Vector3 Vector3::UNIT_Z(0, 0, 1);
  66. }