Vector3.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "Base.h"
  2. #include "Vector3.h"
  3. #include "MathUtil.h"
  4. namespace gameplay
  5. {
  6. Vector3::Vector3()
  7. : x(0.0f), y(0.0f), z(0.0f)
  8. {
  9. }
  10. Vector3::Vector3(float x, float y, float z)
  11. : x(x), y(y), z(z)
  12. {
  13. }
  14. Vector3::Vector3(const float* array)
  15. {
  16. set(array);
  17. }
  18. Vector3::Vector3(const Vector3& p1, const Vector3& p2)
  19. {
  20. set(p1, p2);
  21. }
  22. Vector3::Vector3(const Vector3& copy)
  23. {
  24. set(copy);
  25. }
  26. Vector3 Vector3::fromColor(unsigned int color)
  27. {
  28. float components[3];
  29. int componentIndex = 0;
  30. for (int i = 2; i >= 0; --i)
  31. {
  32. int component = (color >> i*8) & 0x0000ff;
  33. components[componentIndex++] = static_cast<float>(component) / 255.0f;
  34. }
  35. Vector3 value(components);
  36. return value;
  37. }
  38. Vector3::~Vector3()
  39. {
  40. }
  41. const Vector3& Vector3::zero()
  42. {
  43. static Vector3 value(0.0f, 0.0f, 0.0f);
  44. return value;
  45. }
  46. const Vector3& Vector3::one()
  47. {
  48. static Vector3 value(1.0f, 1.0f, 1.0f);
  49. return value;
  50. }
  51. const Vector3& Vector3::unitX()
  52. {
  53. static Vector3 value(1.0f, 0.0f, 0.0f);
  54. return value;
  55. }
  56. const Vector3& Vector3::unitY()
  57. {
  58. static Vector3 value(0.0f, 1.0f, 0.0f);
  59. return value;
  60. }
  61. const Vector3& Vector3::unitZ()
  62. {
  63. static Vector3 value(0.0f, 0.0f, 1.0f);
  64. return value;
  65. }
  66. bool Vector3::isZero() const
  67. {
  68. return x == 0.0f && y == 0.0f && z == 0.0f;
  69. }
  70. bool Vector3::isOne() const
  71. {
  72. return x == 1.0f && y == 1.0f && z == 1.0f;
  73. }
  74. float Vector3::angle(const Vector3& v1, const Vector3& v2)
  75. {
  76. float dx = v1.y * v2.z - v1.z * v2.y;
  77. float dy = v1.z * v2.x - v1.x * v2.z;
  78. float dz = v1.x * v2.y - v1.y * v2.x;
  79. return atan2f(sqrt(dx * dx + dy * dy + dz * dz) + MATH_FLOAT_SMALL, dot(v1, v2));
  80. }
  81. void Vector3::add(const Vector3& v)
  82. {
  83. x += v.x;
  84. y += v.y;
  85. z += v.z;
  86. }
  87. void Vector3::add(const Vector3& v1, const Vector3& v2, Vector3* dst)
  88. {
  89. GP_ASSERT(dst);
  90. dst->x = v1.x + v2.x;
  91. dst->y = v1.y + v2.y;
  92. dst->z = v1.z + v2.z;
  93. }
  94. void Vector3::clamp(const Vector3& min, const Vector3& max)
  95. {
  96. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
  97. // Clamp the x value.
  98. if (x < min.x)
  99. x = min.x;
  100. if (x > max.x)
  101. x = max.x;
  102. // Clamp the y value.
  103. if (y < min.y)
  104. y = min.y;
  105. if (y > max.y)
  106. y = max.y;
  107. // Clamp the z value.
  108. if (z < min.z)
  109. z = min.z;
  110. if (z > max.z)
  111. z = max.z;
  112. }
  113. void Vector3::clamp(const Vector3& v, const Vector3& min, const Vector3& max, Vector3* dst)
  114. {
  115. GP_ASSERT(dst);
  116. GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z));
  117. // Clamp the x value.
  118. dst->x = v.x;
  119. if (dst->x < min.x)
  120. dst->x = min.x;
  121. if (dst->x > max.x)
  122. dst->x = max.x;
  123. // Clamp the y value.
  124. dst->y = v.y;
  125. if (dst->y < min.y)
  126. dst->y = min.y;
  127. if (dst->y > max.y)
  128. dst->y = max.y;
  129. // Clamp the z value.
  130. dst->z = v.z;
  131. if (dst->z < min.z)
  132. dst->z = min.z;
  133. if (dst->z > max.z)
  134. dst->z = max.z;
  135. }
  136. void Vector3::cross(const Vector3& v)
  137. {
  138. cross(*this, v, this);
  139. }
  140. void Vector3::cross(const Vector3& v1, const Vector3& v2, Vector3* dst)
  141. {
  142. GP_ASSERT(dst);
  143. MathUtil::crossVector3((const float*)&v1, (const float*)&v2, (float*)dst);
  144. }
  145. float Vector3::distance(const Vector3& v) const
  146. {
  147. float dx = v.x - x;
  148. float dy = v.y - y;
  149. float dz = v.z - z;
  150. return sqrt(dx * dx + dy * dy + dz * dz);
  151. }
  152. float Vector3::distanceSquared(const Vector3& v) const
  153. {
  154. float dx = v.x - x;
  155. float dy = v.y - y;
  156. float dz = v.z - z;
  157. return (dx * dx + dy * dy + dz * dz);
  158. }
  159. float Vector3::dot(const Vector3& v) const
  160. {
  161. return (x * v.x + y * v.y + z * v.z);
  162. }
  163. float Vector3::dot(const Vector3& v1, const Vector3& v2)
  164. {
  165. return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
  166. }
  167. float Vector3::length() const
  168. {
  169. return sqrt(x * x + y * y + z * z);
  170. }
  171. float Vector3::lengthSquared() const
  172. {
  173. return (x * x + y * y + z * z);
  174. }
  175. void Vector3::negate()
  176. {
  177. x = -x;
  178. y = -y;
  179. z = -z;
  180. }
  181. Vector3& Vector3::normalize()
  182. {
  183. normalize(this);
  184. return *this;
  185. }
  186. void Vector3::normalize(Vector3* dst) const
  187. {
  188. GP_ASSERT(dst);
  189. if (dst != this)
  190. {
  191. dst->x = x;
  192. dst->y = y;
  193. dst->z = z;
  194. }
  195. float n = x * x + y * y + z * z;
  196. // Already normalized.
  197. if (n == 1.0f)
  198. return;
  199. n = sqrt(n);
  200. // Too close to zero.
  201. if (n < MATH_TOLERANCE)
  202. return;
  203. n = 1.0f / n;
  204. dst->x *= n;
  205. dst->y *= n;
  206. dst->z *= n;
  207. }
  208. void Vector3::scale(float scalar)
  209. {
  210. x *= scalar;
  211. y *= scalar;
  212. z *= scalar;
  213. }
  214. void Vector3::set(float x, float y, float z)
  215. {
  216. this->x = x;
  217. this->y = y;
  218. this->z = z;
  219. }
  220. void Vector3::set(const float* array)
  221. {
  222. GP_ASSERT(array);
  223. x = array[0];
  224. y = array[1];
  225. z = array[2];
  226. }
  227. void Vector3::set(const Vector3& v)
  228. {
  229. this->x = v.x;
  230. this->y = v.y;
  231. this->z = v.z;
  232. }
  233. void Vector3::set(const Vector3& p1, const Vector3& p2)
  234. {
  235. x = p2.x - p1.x;
  236. y = p2.y - p1.y;
  237. z = p2.z - p1.z;
  238. }
  239. void Vector3::subtract(const Vector3& v)
  240. {
  241. x -= v.x;
  242. y -= v.y;
  243. z -= v.z;
  244. }
  245. void Vector3::subtract(const Vector3& v1, const Vector3& v2, Vector3* dst)
  246. {
  247. GP_ASSERT(dst);
  248. dst->x = v1.x - v2.x;
  249. dst->y = v1.y - v2.y;
  250. dst->z = v1.z - v2.z;
  251. }
  252. void Vector3::smooth(const Vector3& target, float elapsedTime, float responseTime)
  253. {
  254. if (elapsedTime > 0)
  255. {
  256. *this += (target - *this) * (elapsedTime / (elapsedTime + responseTime));
  257. }
  258. }
  259. }