raymath.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /**********************************************************************************************
  2. *
  3. * raymath v1.2 - Math functions to work with Vector3, Matrix and Quaternions
  4. *
  5. * CONFIGURATION:
  6. *
  7. * #define RAYMATH_IMPLEMENTATION
  8. * Generates the implementation of the library into the included file.
  9. * If not defined, the library is in header only mode and can be included in other headers
  10. * or source files without problems. But only ONE file should hold the implementation.
  11. *
  12. * #define RAYMATH_HEADER_ONLY
  13. * Define static inline functions code, so #include header suffices for use.
  14. * This may use up lots of memory.
  15. *
  16. * #define RAYMATH_STANDALONE
  17. * Avoid raylib.h header inclusion in this file.
  18. * Vector3 and Matrix data types are defined internally in raymath module.
  19. *
  20. *
  21. * LICENSE: zlib/libpng
  22. *
  23. * Copyright (c) 2015-2017 Ramon Santamaria (@raysan5)
  24. *
  25. * This software is provided "as-is", without any express or implied warranty. In no event
  26. * will the authors be held liable for any damages arising from the use of this software.
  27. *
  28. * Permission is granted to anyone to use this software for any purpose, including commercial
  29. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  30. *
  31. * 1. The origin of this software must not be misrepresented; you must not claim that you
  32. * wrote the original software. If you use this software in a product, an acknowledgment
  33. * in the product documentation would be appreciated but is not required.
  34. *
  35. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  36. * as being the original software.
  37. *
  38. * 3. This notice may not be removed or altered from any source distribution.
  39. *
  40. **********************************************************************************************/
  41. #ifndef RAYMATH_H
  42. #define RAYMATH_H
  43. //#define RAYMATH_STANDALONE // NOTE: To use raymath as standalone lib, just uncomment this line
  44. //#define RAYMATH_HEADER_ONLY // NOTE: To compile functions as static inline, uncomment this line
  45. #ifndef RAYMATH_STANDALONE
  46. #include "raylib.h" // Required for structs: Vector3, Matrix
  47. #endif
  48. #ifdef __cplusplus
  49. #define RMEXTERN extern "C" // Functions visible from other files (no name mangling of functions in C++)
  50. #else
  51. #define RMEXTERN // Functions visible from other files
  52. #endif
  53. #if defined RAYMATH_IMPLEMENTATION && defined RAYMATH_HEADER_ONLY
  54. #error "Specifying both RAYMATH_IMPLEMENTATION and RAYMATH_HEADER_ONLY is contradictory"
  55. #endif
  56. #ifdef RAYMATH_IMPLEMENTATION
  57. #define RMDEF extern inline // Provide external definition
  58. #elif defined RAYMATH_HEADER_ONLY
  59. #define RMDEF static inline // Functions may be inlined, no external out-of-line definition
  60. #else
  61. #ifdef __TINYC__
  62. #define RMDEF static inline // plain inline not supported by tinycc (See issue #435)
  63. #else
  64. #define RMDEF inline // Functions may be inlined or external definition used
  65. #endif
  66. #endif
  67. //----------------------------------------------------------------------------------
  68. // Defines and Macros
  69. //----------------------------------------------------------------------------------
  70. #ifndef PI
  71. #define PI 3.14159265358979323846
  72. #endif
  73. #ifndef DEG2RAD
  74. #define DEG2RAD (PI/180.0f)
  75. #endif
  76. #ifndef RAD2DEG
  77. #define RAD2DEG (180.0f/PI)
  78. #endif
  79. // Return float vector for Matrix
  80. #ifndef MatrixToFloat
  81. #define MatrixToFloat(mat) (MatrixToFloatV(mat).v)
  82. #endif
  83. // Return float vector for Vector3
  84. #ifndef Vector3ToFloat
  85. #define Vector3ToFloat(vec) (Vector3ToFloatV(vec).v)
  86. #endif
  87. //----------------------------------------------------------------------------------
  88. // Types and Structures Definition
  89. //----------------------------------------------------------------------------------
  90. #if defined(RAYMATH_STANDALONE)
  91. // Vector2 type
  92. typedef struct Vector2 {
  93. float x;
  94. float y;
  95. } Vector2;
  96. // Vector3 type
  97. typedef struct Vector3 {
  98. float x;
  99. float y;
  100. float z;
  101. } Vector3;
  102. // Matrix type (OpenGL style 4x4 - right handed, column major)
  103. typedef struct Matrix {
  104. float m0, m4, m8, m12;
  105. float m1, m5, m9, m13;
  106. float m2, m6, m10, m14;
  107. float m3, m7, m11, m15;
  108. } Matrix;
  109. // Quaternion type
  110. typedef struct Quaternion {
  111. float x;
  112. float y;
  113. float z;
  114. float w;
  115. } Quaternion;
  116. #endif
  117. // NOTE: Helper types to be used instead of array return types for *ToFloat functions
  118. typedef struct float3 { float v[3]; } float3;
  119. typedef struct float16 { float v[16]; } float16;
  120. #include <math.h> // Required for: sinf(), cosf(), tan(), fabs()
  121. //----------------------------------------------------------------------------------
  122. // Module Functions Definition - Utils math
  123. //----------------------------------------------------------------------------------
  124. // Clamp float value
  125. RMDEF float Clamp(float value, float min, float max)
  126. {
  127. const float res = value < min ? min : value;
  128. return res > max ? max : res;
  129. }
  130. //----------------------------------------------------------------------------------
  131. // Module Functions Definition - Vector2 math
  132. //----------------------------------------------------------------------------------
  133. // Vector with components value 0.0f
  134. RMDEF Vector2 Vector2Zero(void)
  135. {
  136. Vector2 result = { 0.0f, 0.0f };
  137. return result;
  138. }
  139. // Vector with components value 1.0f
  140. RMDEF Vector2 Vector2One(void)
  141. {
  142. Vector2 result = { 1.0f, 1.0f };
  143. return result;
  144. }
  145. // Add two vectors (v1 + v2)
  146. RMDEF Vector2 Vector2Add(Vector2 v1, Vector2 v2)
  147. {
  148. Vector2 result = { v1.x + v2.x, v1.y + v2.y };
  149. return result;
  150. }
  151. // Subtract two vectors (v1 - v2)
  152. RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2)
  153. {
  154. Vector2 result = { v1.x - v2.x, v1.y - v2.y };
  155. return result;
  156. }
  157. // Calculate vector length
  158. RMDEF float Vector2Length(Vector2 v)
  159. {
  160. float result = sqrtf((v.x*v.x) + (v.y*v.y));
  161. return result;
  162. }
  163. // Calculate two vectors dot product
  164. RMDEF float Vector2DotProduct(Vector2 v1, Vector2 v2)
  165. {
  166. float result = (v1.x*v2.x + v1.y*v2.y);
  167. return result;
  168. }
  169. // Calculate distance between two vectors
  170. RMDEF float Vector2Distance(Vector2 v1, Vector2 v2)
  171. {
  172. float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
  173. return result;
  174. }
  175. // Calculate angle from two vectors in X-axis
  176. RMDEF float Vector2Angle(Vector2 v1, Vector2 v2)
  177. {
  178. float result = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI);
  179. if (result < 0) result += 360.0f;
  180. return result;
  181. }
  182. // Scale vector (multiply by value)
  183. RMDEF Vector2 Vector2Scale(Vector2 v, float scale)
  184. {
  185. Vector2 result = { v.x*scale, v.y*scale };
  186. return result;
  187. }
  188. // Negate vector
  189. RMDEF Vector2 Vector2Negate(Vector2 v)
  190. {
  191. Vector2 result = { -v.x, -v.y };
  192. return result;
  193. }
  194. // Divide vector by a float value
  195. RMDEF Vector2 Vector2Divide(Vector2 v, float div)
  196. {
  197. Vector2 result = { v.x/div, v.y/div };
  198. return result;
  199. }
  200. // Normalize provided vector
  201. RMDEF Vector2 Vector2Normalize(Vector2 v)
  202. {
  203. Vector2 result = Vector2Divide(v, Vector2Length(v));
  204. return result;
  205. }
  206. //----------------------------------------------------------------------------------
  207. // Module Functions Definition - Vector3 math
  208. //----------------------------------------------------------------------------------
  209. // Vector with components value 0.0f
  210. RMDEF Vector3 Vector3Zero(void)
  211. {
  212. Vector3 result = { 0.0f, 0.0f, 0.0f };
  213. return result;
  214. }
  215. // Vector with components value 1.0f
  216. RMDEF Vector3 Vector3One(void)
  217. {
  218. Vector3 result = { 1.0f, 1.0f, 1.0f };
  219. return result;
  220. }
  221. // Add two vectors
  222. RMDEF Vector3 Vector3Add(Vector3 v1, Vector3 v2)
  223. {
  224. Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
  225. return result;
  226. }
  227. // Substract two vectors
  228. RMDEF Vector3 Vector3Subtract(Vector3 v1, Vector3 v2)
  229. {
  230. Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
  231. return result;
  232. }
  233. // Multiply vector by scalar
  234. RMDEF Vector3 Vector3Multiply(Vector3 v, float scalar)
  235. {
  236. Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };
  237. return result;
  238. }
  239. // Multiply vector by vector
  240. RMDEF Vector3 Vector3MultiplyV(Vector3 v1, Vector3 v2)
  241. {
  242. Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };
  243. return result;
  244. }
  245. // Calculate two vectors cross product
  246. RMDEF Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2)
  247. {
  248. Vector3 result = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
  249. return result;
  250. }
  251. // Calculate one vector perpendicular vector
  252. RMDEF Vector3 Vector3Perpendicular(Vector3 v)
  253. {
  254. Vector3 result = { 0 };
  255. float min = fabs(v.x);
  256. Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
  257. if (fabs(v.y) < min)
  258. {
  259. min = fabs(v.y);
  260. Vector3 tmp = {0.0f, 1.0f, 0.0f};
  261. cardinalAxis = tmp;
  262. }
  263. if (fabs(v.z) < min)
  264. {
  265. Vector3 tmp = {0.0f, 0.0f, 1.0f};
  266. cardinalAxis = tmp;
  267. }
  268. result = Vector3CrossProduct(v, cardinalAxis);
  269. return result;
  270. }
  271. // Calculate vector length
  272. RMDEF float Vector3Length(const Vector3 v)
  273. {
  274. float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
  275. return result;
  276. }
  277. // Calculate two vectors dot product
  278. RMDEF float Vector3DotProduct(Vector3 v1, Vector3 v2)
  279. {
  280. float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
  281. return result;
  282. }
  283. // Calculate distance between two vectors
  284. RMDEF float Vector3Distance(Vector3 v1, Vector3 v2)
  285. {
  286. float dx = v2.x - v1.x;
  287. float dy = v2.y - v1.y;
  288. float dz = v2.z - v1.z;
  289. float result = sqrtf(dx*dx + dy*dy + dz*dz);
  290. return result;
  291. }
  292. // Scale provided vector
  293. RMDEF Vector3 Vector3Scale(Vector3 v, float scale)
  294. {
  295. Vector3 result = { v.x*scale, v.y*scale, v.z*scale };
  296. return result;
  297. }
  298. // Negate provided vector (invert direction)
  299. RMDEF Vector3 Vector3Negate(Vector3 v)
  300. {
  301. Vector3 result = { -v.x, -v.y, -v.z };
  302. return result;
  303. }
  304. // Normalize provided vector
  305. RMDEF Vector3 Vector3Normalize(Vector3 v)
  306. {
  307. Vector3 result = v;
  308. float length, ilength;
  309. length = Vector3Length(v);
  310. if (length == 0.0f) length = 1.0f;
  311. ilength = 1.0f/length;
  312. result.x *= ilength;
  313. result.y *= ilength;
  314. result.z *= ilength;
  315. return result;
  316. }
  317. // Orthonormalize provided vectors
  318. // Makes vectors normalized and orthogonal to each other
  319. // Gram-Schmidt function implementation
  320. RMDEF void Vector3OrthoNormalize(Vector3 *v1, Vector3 *v2)
  321. {
  322. *v1 = Vector3Normalize(*v1);
  323. Vector3 vn = Vector3CrossProduct(*v1, *v2);
  324. vn = Vector3Normalize(vn);
  325. *v2 = Vector3CrossProduct(vn, *v1);
  326. }
  327. // Transforms a Vector3 by a given Matrix
  328. RMDEF Vector3 Vector3Transform(Vector3 v, Matrix mat)
  329. {
  330. Vector3 result = { 0 };
  331. float x = v.x;
  332. float y = v.y;
  333. float z = v.z;
  334. result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
  335. result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
  336. result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;
  337. return result;
  338. };
  339. // Transform a vector by quaternion rotation
  340. RMDEF Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q)
  341. {
  342. Vector3 result = { 0 };
  343. result.x = v.x*(q.x*q.x + q.w*q.w - q.y*q.y - q.z*q.z) + v.y*(2*q.x*q.y - 2*q.w*q.z) + v.z*(2*q.x*q.z + 2*q.w*q.y);
  344. result.y = v.x*(2*q.w*q.z + 2*q.x*q.y) + v.y*(q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z) + v.z*(-2*q.w*q.x + 2*q.y*q.z);
  345. result.z = v.x*(-2*q.w*q.y + 2*q.x*q.z) + v.y*(2*q.w*q.x + 2*q.y*q.z)+ v.z*(q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z);
  346. return result;
  347. }
  348. // Calculate linear interpolation between two vectors
  349. RMDEF Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount)
  350. {
  351. Vector3 result = { 0 };
  352. result.x = v1.x + amount*(v2.x - v1.x);
  353. result.y = v1.y + amount*(v2.y - v1.y);
  354. result.z = v1.z + amount*(v2.z - v1.z);
  355. return result;
  356. }
  357. // Calculate reflected vector to normal
  358. RMDEF Vector3 Vector3Reflect(Vector3 v, Vector3 normal)
  359. {
  360. // I is the original vector
  361. // N is the normal of the incident plane
  362. // R = I - (2*N*( DotProduct[ I,N] ))
  363. Vector3 result = { 0 };
  364. float dotProduct = Vector3DotProduct(v, normal);
  365. result.x = v.x - (2.0f*normal.x)*dotProduct;
  366. result.y = v.y - (2.0f*normal.y)*dotProduct;
  367. result.z = v.z - (2.0f*normal.z)*dotProduct;
  368. return result;
  369. }
  370. // Return min value for each pair of components
  371. RMDEF Vector3 Vector3Min(Vector3 v1, Vector3 v2)
  372. {
  373. Vector3 result = { 0 };
  374. result.x = fminf(v1.x, v2.x);
  375. result.y = fminf(v1.y, v2.y);
  376. result.z = fminf(v1.z, v2.z);
  377. return result;
  378. }
  379. // Return max value for each pair of components
  380. RMDEF Vector3 Vector3Max(Vector3 v1, Vector3 v2)
  381. {
  382. Vector3 result = { 0 };
  383. result.x = fmaxf(v1.x, v2.x);
  384. result.y = fmaxf(v1.y, v2.y);
  385. result.z = fmaxf(v1.z, v2.z);
  386. return result;
  387. }
  388. // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
  389. // NOTE: Assumes P is on the plane of the triangle
  390. RMDEF Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c)
  391. {
  392. //Vector v0 = b - a, v1 = c - a, v2 = p - a;
  393. Vector3 v0 = Vector3Subtract(b, a);
  394. Vector3 v1 = Vector3Subtract(c, a);
  395. Vector3 v2 = Vector3Subtract(p, a);
  396. float d00 = Vector3DotProduct(v0, v0);
  397. float d01 = Vector3DotProduct(v0, v1);
  398. float d11 = Vector3DotProduct(v1, v1);
  399. float d20 = Vector3DotProduct(v2, v0);
  400. float d21 = Vector3DotProduct(v2, v1);
  401. float denom = d00*d11 - d01*d01;
  402. Vector3 result = { 0 };
  403. result.y = (d11*d20 - d01*d21)/denom;
  404. result.z = (d00*d21 - d01*d20)/denom;
  405. result.x = 1.0f - (result.z + result.y);
  406. return result;
  407. }
  408. // Returns Vector3 as float array
  409. RMDEF float3 Vector3ToFloatV(Vector3 v)
  410. {
  411. float3 buffer = { 0 };
  412. buffer.v[0] = v.x;
  413. buffer.v[1] = v.y;
  414. buffer.v[2] = v.z;
  415. return buffer;
  416. }
  417. //----------------------------------------------------------------------------------
  418. // Module Functions Definition - Matrix math
  419. //----------------------------------------------------------------------------------
  420. // Compute matrix determinant
  421. RMDEF float MatrixDeterminant(Matrix mat)
  422. {
  423. float result = { 0 };
  424. // Cache the matrix values (speed optimization)
  425. float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
  426. float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
  427. float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
  428. float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
  429. result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
  430. a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
  431. a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
  432. a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
  433. a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
  434. a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
  435. return result;
  436. }
  437. // Returns the trace of the matrix (sum of the values along the diagonal)
  438. RMDEF float MatrixTrace(Matrix mat)
  439. {
  440. float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);
  441. return result;
  442. }
  443. // Transposes provided matrix
  444. RMDEF Matrix MatrixTranspose(Matrix mat)
  445. {
  446. Matrix result = { 0 };
  447. result.m0 = mat.m0;
  448. result.m1 = mat.m4;
  449. result.m2 = mat.m8;
  450. result.m3 = mat.m12;
  451. result.m4 = mat.m1;
  452. result.m5 = mat.m5;
  453. result.m6 = mat.m9;
  454. result.m7 = mat.m13;
  455. result.m8 = mat.m2;
  456. result.m9 = mat.m6;
  457. result.m10 = mat.m10;
  458. result.m11 = mat.m14;
  459. result.m12 = mat.m3;
  460. result.m13 = mat.m7;
  461. result.m14 = mat.m11;
  462. result.m15 = mat.m15;
  463. return result;
  464. }
  465. // Invert provided matrix
  466. RMDEF Matrix MatrixInvert(Matrix mat)
  467. {
  468. Matrix result = { 0 };
  469. // Cache the matrix values (speed optimization)
  470. float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
  471. float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
  472. float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
  473. float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
  474. float b00 = a00*a11 - a01*a10;
  475. float b01 = a00*a12 - a02*a10;
  476. float b02 = a00*a13 - a03*a10;
  477. float b03 = a01*a12 - a02*a11;
  478. float b04 = a01*a13 - a03*a11;
  479. float b05 = a02*a13 - a03*a12;
  480. float b06 = a20*a31 - a21*a30;
  481. float b07 = a20*a32 - a22*a30;
  482. float b08 = a20*a33 - a23*a30;
  483. float b09 = a21*a32 - a22*a31;
  484. float b10 = a21*a33 - a23*a31;
  485. float b11 = a22*a33 - a23*a32;
  486. // Calculate the invert determinant (inlined to avoid double-caching)
  487. float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
  488. result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
  489. result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
  490. result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
  491. result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
  492. result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
  493. result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
  494. result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
  495. result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
  496. result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
  497. result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
  498. result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
  499. result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
  500. result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
  501. result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
  502. result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
  503. result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;
  504. return result;
  505. }
  506. // Normalize provided matrix
  507. RMDEF Matrix MatrixNormalize(Matrix mat)
  508. {
  509. Matrix result = { 0 };
  510. float det = MatrixDeterminant(mat);
  511. result.m0 = mat.m0/det;
  512. result.m1 = mat.m1/det;
  513. result.m2 = mat.m2/det;
  514. result.m3 = mat.m3/det;
  515. result.m4 = mat.m4/det;
  516. result.m5 = mat.m5/det;
  517. result.m6 = mat.m6/det;
  518. result.m7 = mat.m7/det;
  519. result.m8 = mat.m8/det;
  520. result.m9 = mat.m9/det;
  521. result.m10 = mat.m10/det;
  522. result.m11 = mat.m11/det;
  523. result.m12 = mat.m12/det;
  524. result.m13 = mat.m13/det;
  525. result.m14 = mat.m14/det;
  526. result.m15 = mat.m15/det;
  527. return result;
  528. }
  529. // Returns identity matrix
  530. RMDEF Matrix MatrixIdentity(void)
  531. {
  532. Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
  533. 0.0f, 1.0f, 0.0f, 0.0f,
  534. 0.0f, 0.0f, 1.0f, 0.0f,
  535. 0.0f, 0.0f, 0.0f, 1.0f };
  536. return result;
  537. }
  538. // Add two matrices
  539. RMDEF Matrix MatrixAdd(Matrix left, Matrix right)
  540. {
  541. Matrix result = MatrixIdentity();
  542. result.m0 = left.m0 + right.m0;
  543. result.m1 = left.m1 + right.m1;
  544. result.m2 = left.m2 + right.m2;
  545. result.m3 = left.m3 + right.m3;
  546. result.m4 = left.m4 + right.m4;
  547. result.m5 = left.m5 + right.m5;
  548. result.m6 = left.m6 + right.m6;
  549. result.m7 = left.m7 + right.m7;
  550. result.m8 = left.m8 + right.m8;
  551. result.m9 = left.m9 + right.m9;
  552. result.m10 = left.m10 + right.m10;
  553. result.m11 = left.m11 + right.m11;
  554. result.m12 = left.m12 + right.m12;
  555. result.m13 = left.m13 + right.m13;
  556. result.m14 = left.m14 + right.m14;
  557. result.m15 = left.m15 + right.m15;
  558. return result;
  559. }
  560. // Substract two matrices (left - right)
  561. RMDEF Matrix MatrixSubstract(Matrix left, Matrix right)
  562. {
  563. Matrix result = MatrixIdentity();
  564. result.m0 = left.m0 - right.m0;
  565. result.m1 = left.m1 - right.m1;
  566. result.m2 = left.m2 - right.m2;
  567. result.m3 = left.m3 - right.m3;
  568. result.m4 = left.m4 - right.m4;
  569. result.m5 = left.m5 - right.m5;
  570. result.m6 = left.m6 - right.m6;
  571. result.m7 = left.m7 - right.m7;
  572. result.m8 = left.m8 - right.m8;
  573. result.m9 = left.m9 - right.m9;
  574. result.m10 = left.m10 - right.m10;
  575. result.m11 = left.m11 - right.m11;
  576. result.m12 = left.m12 - right.m12;
  577. result.m13 = left.m13 - right.m13;
  578. result.m14 = left.m14 - right.m14;
  579. result.m15 = left.m15 - right.m15;
  580. return result;
  581. }
  582. // Returns translation matrix
  583. RMDEF Matrix MatrixTranslate(float x, float y, float z)
  584. {
  585. Matrix result = { 1.0f, 0.0f, 0.0f, x,
  586. 0.0f, 1.0f, 0.0f, y,
  587. 0.0f, 0.0f, 1.0f, z,
  588. 0.0f, 0.0f, 0.0f, 1.0f };
  589. return result;
  590. }
  591. // Create rotation matrix from axis and angle
  592. // NOTE: Angle should be provided in radians
  593. RMDEF Matrix MatrixRotate(Vector3 axis, float angle)
  594. {
  595. Matrix result = { 0 };
  596. float x = axis.x, y = axis.y, z = axis.z;
  597. float length = sqrtf(x*x + y*y + z*z);
  598. if ((length != 1.0f) && (length != 0.0f))
  599. {
  600. length = 1.0f/length;
  601. x *= length;
  602. y *= length;
  603. z *= length;
  604. }
  605. float sinres = sinf(angle);
  606. float cosres = cosf(angle);
  607. float t = 1.0f - cosres;
  608. result.m0 = x*x*t + cosres;
  609. result.m1 = y*x*t + z*sinres;
  610. result.m2 = z*x*t - y*sinres;
  611. result.m3 = 0.0f;
  612. result.m4 = x*y*t - z*sinres;
  613. result.m5 = y*y*t + cosres;
  614. result.m6 = z*y*t + x*sinres;
  615. result.m7 = 0.0f;
  616. result.m8 = x*z*t + y*sinres;
  617. result.m9 = y*z*t - x*sinres;
  618. result.m10 = z*z*t + cosres;
  619. result.m11 = 0.0f;
  620. result.m12 = 0.0f;
  621. result.m13 = 0.0f;
  622. result.m14 = 0.0f;
  623. result.m15 = 1.0f;
  624. return result;
  625. }
  626. // Returns x-rotation matrix (angle in radians)
  627. RMDEF Matrix MatrixRotateX(float angle)
  628. {
  629. Matrix result = MatrixIdentity();
  630. float cosres = cosf(angle);
  631. float sinres = sinf(angle);
  632. result.m5 = cosres;
  633. result.m6 = -sinres;
  634. result.m9 = sinres;
  635. result.m10 = cosres;
  636. return result;
  637. }
  638. // Returns y-rotation matrix (angle in radians)
  639. RMDEF Matrix MatrixRotateY(float angle)
  640. {
  641. Matrix result = MatrixIdentity();
  642. float cosres = cosf(angle);
  643. float sinres = sinf(angle);
  644. result.m0 = cosres;
  645. result.m2 = sinres;
  646. result.m8 = -sinres;
  647. result.m10 = cosres;
  648. return result;
  649. }
  650. // Returns z-rotation matrix (angle in radians)
  651. RMDEF Matrix MatrixRotateZ(float angle)
  652. {
  653. Matrix result = MatrixIdentity();
  654. float cosres = cosf(angle);
  655. float sinres = sinf(angle);
  656. result.m0 = cosres;
  657. result.m1 = -sinres;
  658. result.m4 = sinres;
  659. result.m5 = cosres;
  660. return result;
  661. }
  662. // Returns scaling matrix
  663. RMDEF Matrix MatrixScale(float x, float y, float z)
  664. {
  665. Matrix result = { x, 0.0f, 0.0f, 0.0f,
  666. 0.0f, y, 0.0f, 0.0f,
  667. 0.0f, 0.0f, z, 0.0f,
  668. 0.0f, 0.0f, 0.0f, 1.0f };
  669. return result;
  670. }
  671. // Returns two matrix multiplication
  672. // NOTE: When multiplying matrices... the order matters!
  673. RMDEF Matrix MatrixMultiply(Matrix left, Matrix right)
  674. {
  675. Matrix result = { 0 };
  676. result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
  677. result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
  678. result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
  679. result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
  680. result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
  681. result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
  682. result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
  683. result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
  684. result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
  685. result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
  686. result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
  687. result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
  688. result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
  689. result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
  690. result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
  691. result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
  692. return result;
  693. }
  694. // Returns perspective projection matrix
  695. RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far)
  696. {
  697. Matrix result = { 0 };
  698. float rl = (right - left);
  699. float tb = (top - bottom);
  700. float fn = (far - near);
  701. result.m0 = (near*2.0f)/rl;
  702. result.m1 = 0.0f;
  703. result.m2 = 0.0f;
  704. result.m3 = 0.0f;
  705. result.m4 = 0.0f;
  706. result.m5 = (near*2.0f)/tb;
  707. result.m6 = 0.0f;
  708. result.m7 = 0.0f;
  709. result.m8 = (right + left)/rl;
  710. result.m9 = (top + bottom)/tb;
  711. result.m10 = -(far + near)/fn;
  712. result.m11 = -1.0f;
  713. result.m12 = 0.0f;
  714. result.m13 = 0.0f;
  715. result.m14 = -(far*near*2.0f)/fn;
  716. result.m15 = 0.0f;
  717. return result;
  718. }
  719. // Returns perspective projection matrix
  720. // NOTE: Angle should be provided in radians
  721. RMDEF Matrix MatrixPerspective(double fovy, double aspect, double near, double far)
  722. {
  723. double top = near*tan(fovy*0.5);
  724. double right = top*aspect;
  725. Matrix result = MatrixFrustum(-right, right, -top, top, near, far);
  726. return result;
  727. }
  728. // Returns orthographic projection matrix
  729. RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far)
  730. {
  731. Matrix result = { 0 };
  732. float rl = (right - left);
  733. float tb = (top - bottom);
  734. float fn = (far - near);
  735. result.m0 = 2.0f/rl;
  736. result.m1 = 0.0f;
  737. result.m2 = 0.0f;
  738. result.m3 = 0.0f;
  739. result.m4 = 0.0f;
  740. result.m5 = 2.0f/tb;
  741. result.m6 = 0.0f;
  742. result.m7 = 0.0f;
  743. result.m8 = 0.0f;
  744. result.m9 = 0.0f;
  745. result.m10 = -2.0f/fn;
  746. result.m11 = 0.0f;
  747. result.m12 = -(left + right)/rl;
  748. result.m13 = -(top + bottom)/tb;
  749. result.m14 = -(far + near)/fn;
  750. result.m15 = 1.0f;
  751. return result;
  752. }
  753. // Returns camera look-at matrix (view matrix)
  754. RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
  755. {
  756. Matrix result = { 0 };
  757. Vector3 z = Vector3Subtract(eye, target);
  758. z = Vector3Normalize(z);
  759. Vector3 x = Vector3CrossProduct(up, z);
  760. x = Vector3Normalize(x);
  761. Vector3 y = Vector3CrossProduct(z, x);
  762. y = Vector3Normalize(y);
  763. result.m0 = x.x;
  764. result.m1 = x.y;
  765. result.m2 = x.z;
  766. result.m3 = 0.0f;
  767. result.m4 = y.x;
  768. result.m5 = y.y;
  769. result.m6 = y.z;
  770. result.m7 = 0.0f;
  771. result.m8 = z.x;
  772. result.m9 = z.y;
  773. result.m10 = z.z;
  774. result.m11 = 0.0f;
  775. result.m12 = eye.x;
  776. result.m13 = eye.y;
  777. result.m14 = eye.z;
  778. result.m15 = 1.0f;
  779. result = MatrixInvert(result);
  780. return result;
  781. }
  782. // Returns float array of matrix data
  783. RMDEF float16 MatrixToFloatV(Matrix mat)
  784. {
  785. float16 buffer = { 0 };
  786. buffer.v[0] = mat.m0;
  787. buffer.v[1] = mat.m1;
  788. buffer.v[2] = mat.m2;
  789. buffer.v[3] = mat.m3;
  790. buffer.v[4] = mat.m4;
  791. buffer.v[5] = mat.m5;
  792. buffer.v[6] = mat.m6;
  793. buffer.v[7] = mat.m7;
  794. buffer.v[8] = mat.m8;
  795. buffer.v[9] = mat.m9;
  796. buffer.v[10] = mat.m10;
  797. buffer.v[11] = mat.m11;
  798. buffer.v[12] = mat.m12;
  799. buffer.v[13] = mat.m13;
  800. buffer.v[14] = mat.m14;
  801. buffer.v[15] = mat.m15;
  802. return buffer;
  803. }
  804. //----------------------------------------------------------------------------------
  805. // Module Functions Definition - Quaternion math
  806. //----------------------------------------------------------------------------------
  807. // Returns identity quaternion
  808. RMDEF Quaternion QuaternionIdentity(void)
  809. {
  810. Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
  811. return result;
  812. }
  813. // Computes the length of a quaternion
  814. RMDEF float QuaternionLength(Quaternion q)
  815. {
  816. float result = sqrt(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
  817. return result;
  818. }
  819. // Normalize provided quaternion
  820. RMDEF Quaternion QuaternionNormalize(Quaternion q)
  821. {
  822. Quaternion result = { 0 };
  823. float length, ilength;
  824. length = QuaternionLength(q);
  825. if (length == 0.0f) length = 1.0f;
  826. ilength = 1.0f/length;
  827. result.x = q.x*ilength;
  828. result.y = q.y*ilength;
  829. result.z = q.z*ilength;
  830. result.w = q.w*ilength;
  831. return result;
  832. }
  833. // Invert provided quaternion
  834. RMDEF Quaternion QuaternionInvert(Quaternion q)
  835. {
  836. Quaternion result = q;
  837. float length = QuaternionLength(q);
  838. float lengthSq = length*length;
  839. if (lengthSq != 0.0)
  840. {
  841. float i = 1.0f/lengthSq;
  842. result.x *= -i;
  843. result.y *= -i;
  844. result.z *= -i;
  845. result.w *= i;
  846. }
  847. return result;
  848. }
  849. // Calculate two quaternion multiplication
  850. RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2)
  851. {
  852. Quaternion result = { 0 };
  853. float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w;
  854. float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w;
  855. result.x = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
  856. result.y = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
  857. result.z = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
  858. result.w = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;
  859. return result;
  860. }
  861. // Calculate linear interpolation between two quaternions
  862. RMDEF Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount)
  863. {
  864. Quaternion result = { 0 };
  865. result.x = q1.x + amount*(q2.x - q1.x);
  866. result.y = q1.y + amount*(q2.y - q1.y);
  867. result.z = q1.z + amount*(q2.z - q1.z);
  868. result.w = q1.w + amount*(q2.w - q1.w);
  869. return result;
  870. }
  871. // Calculate slerp-optimized interpolation between two quaternions
  872. RMDEF Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount)
  873. {
  874. Quaternion result = QuaternionLerp(q1, q2, amount);
  875. result = QuaternionNormalize(result);
  876. return result;
  877. }
  878. // Calculates spherical linear interpolation between two quaternions
  879. RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount)
  880. {
  881. Quaternion result = { 0 };
  882. float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
  883. if (fabs(cosHalfTheta) >= 1.0f) result = q1;
  884. else if (cosHalfTheta > 0.95f) result = QuaternionNlerp(q1, q2, amount);
  885. else
  886. {
  887. float halfTheta = acos(cosHalfTheta);
  888. float sinHalfTheta = sqrt(1.0f - cosHalfTheta*cosHalfTheta);
  889. if (fabs(sinHalfTheta) < 0.001f)
  890. {
  891. result.x = (q1.x*0.5f + q2.x*0.5f);
  892. result.y = (q1.y*0.5f + q2.y*0.5f);
  893. result.z = (q1.z*0.5f + q2.z*0.5f);
  894. result.w = (q1.w*0.5f + q2.w*0.5f);
  895. }
  896. else
  897. {
  898. float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta;
  899. float ratioB = sinf(amount*halfTheta)/sinHalfTheta;
  900. result.x = (q1.x*ratioA + q2.x*ratioB);
  901. result.y = (q1.y*ratioA + q2.y*ratioB);
  902. result.z = (q1.z*ratioA + q2.z*ratioB);
  903. result.w = (q1.w*ratioA + q2.w*ratioB);
  904. }
  905. }
  906. return result;
  907. }
  908. // Calculate quaternion based on the rotation from one vector to another
  909. RMDEF Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
  910. {
  911. Quaternion result = { 0 };
  912. float cos2Theta = Vector3DotProduct(from, to);
  913. Vector3 cross = Vector3CrossProduct(from, to);
  914. result.x = cross.x;
  915. result.y = cross.y;
  916. result.z = cross.y;
  917. result.w = 1.0f + cos2Theta; // NOTE: Added QuaternioIdentity()
  918. // Normalize to essentially nlerp the original and identity to 0.5
  919. result = QuaternionNormalize(result);
  920. // Above lines are equivalent to:
  921. //Quaternion result = QuaternionNlerp(q, QuaternionIdentity(), 0.5f);
  922. return result;
  923. }
  924. // Returns a quaternion for a given rotation matrix
  925. RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
  926. {
  927. Quaternion result = { 0 };
  928. float trace = MatrixTrace(mat);
  929. if (trace > 0.0f)
  930. {
  931. float s = (float)sqrt(trace + 1)*2.0f;
  932. float invS = 1.0f/s;
  933. result.w = s*0.25f;
  934. result.x = (mat.m6 - mat.m9)*invS;
  935. result.y = (mat.m8 - mat.m2)*invS;
  936. result.z = (mat.m1 - mat.m4)*invS;
  937. }
  938. else
  939. {
  940. float m00 = mat.m0, m11 = mat.m5, m22 = mat.m10;
  941. if (m00 > m11 && m00 > m22)
  942. {
  943. float s = (float)sqrt(1.0f + m00 - m11 - m22)*2.0f;
  944. float invS = 1.0f/s;
  945. result.w = (mat.m6 - mat.m9)*invS;
  946. result.x = s*0.25f;
  947. result.y = (mat.m4 + mat.m1)*invS;
  948. result.z = (mat.m8 + mat.m2)*invS;
  949. }
  950. else if (m11 > m22)
  951. {
  952. float s = (float)sqrt(1.0f + m11 - m00 - m22)*2.0f;
  953. float invS = 1.0f/s;
  954. result.w = (mat.m8 - mat.m2)*invS;
  955. result.x = (mat.m4 + mat.m1)*invS;
  956. result.y = s*0.25f;
  957. result.z = (mat.m9 + mat.m6)*invS;
  958. }
  959. else
  960. {
  961. float s = (float)sqrt(1.0f + m22 - m00 - m11)*2.0f;
  962. float invS = 1.0f/s;
  963. result.w = (mat.m1 - mat.m4)*invS;
  964. result.x = (mat.m8 + mat.m2)*invS;
  965. result.y = (mat.m9 + mat.m6)*invS;
  966. result.z = s*0.25f;
  967. }
  968. }
  969. return result;
  970. }
  971. // Returns a matrix for a given quaternion
  972. RMDEF Matrix QuaternionToMatrix(Quaternion q)
  973. {
  974. Matrix result = { 0 };
  975. float x = q.x, y = q.y, z = q.z, w = q.w;
  976. float x2 = x + x;
  977. float y2 = y + y;
  978. float z2 = z + z;
  979. float length = QuaternionLength(q);
  980. float lengthSquared = length*length;
  981. float xx = x*x2/lengthSquared;
  982. float xy = x*y2/lengthSquared;
  983. float xz = x*z2/lengthSquared;
  984. float yy = y*y2/lengthSquared;
  985. float yz = y*z2/lengthSquared;
  986. float zz = z*z2/lengthSquared;
  987. float wx = w*x2/lengthSquared;
  988. float wy = w*y2/lengthSquared;
  989. float wz = w*z2/lengthSquared;
  990. result.m0 = 1.0f - (yy + zz);
  991. result.m1 = xy - wz;
  992. result.m2 = xz + wy;
  993. result.m3 = 0.0f;
  994. result.m4 = xy + wz;
  995. result.m5 = 1.0f - (xx + zz);
  996. result.m6 = yz - wx;
  997. result.m7 = 0.0f;
  998. result.m8 = xz - wy;
  999. result.m9 = yz + wx;
  1000. result.m10 = 1.0f - (xx + yy);
  1001. result.m11 = 0.0f;
  1002. result.m12 = 0.0f;
  1003. result.m13 = 0.0f;
  1004. result.m14 = 0.0f;
  1005. result.m15 = 1.0f;
  1006. return result;
  1007. }
  1008. // Returns rotation quaternion for an angle and axis
  1009. // NOTE: angle must be provided in radians
  1010. RMDEF Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle)
  1011. {
  1012. Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
  1013. if (Vector3Length(axis) != 0.0f)
  1014. angle *= 0.5f;
  1015. axis = Vector3Normalize(axis);
  1016. float sinres = sinf(angle);
  1017. float cosres = cosf(angle);
  1018. result.x = axis.x*sinres;
  1019. result.y = axis.y*sinres;
  1020. result.z = axis.z*sinres;
  1021. result.w = cosres;
  1022. result = QuaternionNormalize(result);
  1023. return result;
  1024. }
  1025. // Returns the rotation angle and axis for a given quaternion
  1026. RMDEF void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle)
  1027. {
  1028. if (fabs(q.w) > 1.0f) q = QuaternionNormalize(q);
  1029. Vector3 resAxis = { 0.0f, 0.0f, 0.0f };
  1030. float resAngle = 0.0f;
  1031. resAngle = 2.0f*(float)acos(q.w);
  1032. float den = (float)sqrt(1.0f - q.w*q.w);
  1033. if (den > 0.0001f)
  1034. {
  1035. resAxis.x = q.x/den;
  1036. resAxis.y = q.y/den;
  1037. resAxis.z = q.z/den;
  1038. }
  1039. else
  1040. {
  1041. // This occurs when the angle is zero.
  1042. // Not a problem: just set an arbitrary normalized axis.
  1043. resAxis.x = 1.0f;
  1044. }
  1045. *outAxis = resAxis;
  1046. *outAngle = resAngle;
  1047. }
  1048. // Returns he quaternion equivalent to Euler angles
  1049. RMDEF Quaternion QuaternionFromEuler(float roll, float pitch, float yaw)
  1050. {
  1051. Quaternion q = { 0 };
  1052. float x0 = cosf(roll*0.5f);
  1053. float x1 = sinf(roll*0.5f);
  1054. float y0 = cosf(pitch*0.5f);
  1055. float y1 = sinf(pitch*0.5f);
  1056. float z0 = cosf(yaw*0.5f);
  1057. float z1 = sinf(yaw*0.5f);
  1058. q.x = x1*y0*z0 - x0*y1*z1;
  1059. q.y = x0*y1*z0 + x1*y0*z1;
  1060. q.z = x0*y0*z1 - x1*y1*z0;
  1061. q.w = x0*y0*z0 + x1*y1*z1;
  1062. return q;
  1063. }
  1064. // Return the Euler angles equivalent to quaternion (roll, pitch, yaw)
  1065. // NOTE: Angles are returned in a Vector3 struct in degrees
  1066. RMDEF Vector3 QuaternionToEuler(Quaternion q)
  1067. {
  1068. Vector3 result = { 0 };
  1069. // roll (x-axis rotation)
  1070. float x0 = 2.0f*(q.w*q.x + q.y*q.z);
  1071. float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y);
  1072. result.x = atan2f(x0, x1)*RAD2DEG;
  1073. // pitch (y-axis rotation)
  1074. float y0 = 2.0f*(q.w*q.y - q.z*q.x);
  1075. y0 = y0 > 1.0f ? 1.0f : y0;
  1076. y0 = y0 < -1.0f ? -1.0f : y0;
  1077. result.y = asinf(y0)*RAD2DEG;
  1078. // yaw (z-axis rotation)
  1079. float z0 = 2.0f*(q.w*q.z + q.x*q.y);
  1080. float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z);
  1081. result.z = atan2f(z0, z1)*RAD2DEG;
  1082. return result;
  1083. }
  1084. // Transform a quaternion given a transformation matrix
  1085. RMDEF Quaternion QuaternionTransform(Quaternion q, Matrix mat)
  1086. {
  1087. Quaternion result = { 0 };
  1088. result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w;
  1089. result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w;
  1090. result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w;
  1091. result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w;
  1092. return result;
  1093. }
  1094. #endif // RAYMATH_H