1 module raylib.raymath; 2 3 import raylib; 4 /********************************************************************************************** 5 * 6 * raymath v1.5 - Math functions to work with Vector2, Vector3, Matrix and Quaternions 7 * 8 * CONVENTIONS: 9 * - Matrix structure is defined as row-major (memory layout) but parameters naming AND all 10 * math operations performed by the library consider the structure as it was column-major 11 * It is like transposed versions of the matrices are used for all the maths 12 * It benefits some functions making them cache-friendly and also avoids matrix 13 * transpositions sometimes required by OpenGL 14 * Example: In memory order, row0 is [m0 m4 m8 m12] but in semantic math row0 is [m0 m1 m2 m3] 15 * - Functions are always self-contained, no function use another raymath function inside, 16 * required code is directly re-implemented inside 17 * - Functions input parameters are always received by value (2 unavoidable exceptions) 18 * - Functions use always a "result" variable for return 19 * - Functions are always defined inline 20 * - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience) 21 * - No compound literals used to make sure libray is compatible with C++ 22 * 23 * CONFIGURATION: 24 * #define RAYMATH_IMPLEMENTATION 25 * Generates the implementation of the library into the included file. 26 * If not defined, the library is in header only mode and can be included in other headers 27 * or source files without problems. But only ONE file should hold the implementation. 28 * 29 * #define RAYMATH_STATIC_INLINE 30 * Define static inline functions code, so #include header suffices for use. 31 * This may use up lots of memory. 32 * 33 * 34 * LICENSE: zlib/libpng 35 * 36 * Copyright (c) 2015-2023 Ramon Santamaria (@raysan5) 37 * 38 * This software is provided "as-is", without any express or implied warranty. In no event 39 * will the authors be held liable for any damages arising from the use of this software. 40 * 41 * Permission is granted to anyone to use this software for any purpose, including commercial 42 * applications, and to alter it and redistribute it freely, subject to the following restrictions: 43 * 44 * 1. The origin of this software must not be misrepresented; you must not claim that you 45 * wrote the original software. If you use this software in a product, an acknowledgment 46 * in the product documentation would be appreciated but is not required. 47 * 48 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented 49 * as being the original software. 50 * 51 * 3. This notice may not be removed or altered from any source distribution. 52 * 53 **********************************************************************************************/ 54 55 extern (C) @nogc nothrow: 56 57 // Function specifiers definition 58 59 // We are building raylib as a Win32 shared library (.dll). 60 61 // We are using raylib as a Win32 shared library (.dll) 62 63 // Provide external definition 64 65 // Functions may be inlined, no external out-of-line definition 66 67 // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used 68 69 //---------------------------------------------------------------------------------- 70 // Defines and Macros 71 //---------------------------------------------------------------------------------- 72 73 enum PI = 3.14159265358979323846f; 74 75 enum EPSILON = 0.000001f; 76 77 enum DEG2RAD = PI / 180.0f; 78 79 enum RAD2DEG = 180.0f / PI; 80 81 // Get float vector for Matrix 82 83 extern (D) auto MatrixToFloat(T)(auto ref T mat) 84 { 85 return MatrixToFloatV(mat).v; 86 } 87 88 // Get float vector for Vector3 89 90 extern (D) auto Vector3ToFloat(T)(auto ref T vec) 91 { 92 return Vector3ToFloatV(vec).v; 93 } 94 95 //---------------------------------------------------------------------------------- 96 // Types and Structures Definition 97 //---------------------------------------------------------------------------------- 98 99 // Vector2 type 100 101 // Vector3 type 102 103 // Vector4 type 104 105 // Quaternion type 106 107 // Matrix type (OpenGL style 4x4 - right handed, column major) 108 109 // Matrix first row (4 components) 110 // Matrix second row (4 components) 111 // Matrix third row (4 components) 112 // Matrix fourth row (4 components) 113 114 // NOTE: Helper types to be used instead of array return types for *ToFloat functions 115 struct float3 116 { 117 float[3] v; 118 } 119 120 struct float16 121 { 122 float[16] v; 123 } 124 125 // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabs() 126 127 //---------------------------------------------------------------------------------- 128 // Module Functions Definition - Utils math 129 //---------------------------------------------------------------------------------- 130 131 // Clamp float value 132 float Clamp(float value, float min, float max); 133 134 // Calculate linear interpolation between two floats 135 float Lerp(float start, float end, float amount); 136 137 // Normalize input value within input range 138 float Normalize(float value, float start, float end); 139 140 // Remap input value within input range to output range 141 float Remap( 142 float value, 143 float inputStart, 144 float inputEnd, 145 float outputStart, 146 float outputEnd); 147 148 // Wrap input value from min to max 149 float Wrap(float value, float min, float max); 150 151 // Check whether two given floats are almost equal 152 int FloatEquals(float x, float y); 153 154 //---------------------------------------------------------------------------------- 155 // Module Functions Definition - Vector2 math 156 //---------------------------------------------------------------------------------- 157 158 // Vector with components value 0.0f 159 Vector2 Vector2Zero(); 160 161 // Vector with components value 1.0f 162 Vector2 Vector2One(); 163 164 // Add two vectors (v1 + v2) 165 Vector2 Vector2Add(Vector2 v1, Vector2 v2); 166 167 // Add vector and float value 168 Vector2 Vector2AddValue(Vector2 v, float add); 169 170 // Subtract two vectors (v1 - v2) 171 Vector2 Vector2Subtract(Vector2 v1, Vector2 v2); 172 173 // Subtract vector by float value 174 Vector2 Vector2SubtractValue(Vector2 v, float sub); 175 176 // Calculate vector length 177 float Vector2Length(Vector2 v); 178 179 // Calculate vector square length 180 float Vector2LengthSqr(Vector2 v); 181 182 // Calculate two vectors dot product 183 float Vector2DotProduct(Vector2 v1, Vector2 v2); 184 185 // Calculate distance between two vectors 186 float Vector2Distance(Vector2 v1, Vector2 v2); 187 188 // Calculate square distance between two vectors 189 float Vector2DistanceSqr(Vector2 v1, Vector2 v2); 190 191 // Calculate angle between two vectors 192 // NOTE: Angle is calculated from origin point (0, 0) 193 float Vector2Angle(Vector2 v1, Vector2 v2); 194 195 // Calculate angle defined by a two vectors line 196 // NOTE: Parameters need to be normalized 197 // Current implementation should be aligned with glm::angle 198 199 // TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior 200 float Vector2LineAngle(Vector2 start, Vector2 end); 201 202 // Scale vector (multiply by value) 203 Vector2 Vector2Scale(Vector2 v, float scale); 204 205 // Multiply vector by vector 206 Vector2 Vector2Multiply(Vector2 v1, Vector2 v2); 207 208 // Negate vector 209 Vector2 Vector2Negate(Vector2 v); 210 211 // Divide vector by vector 212 Vector2 Vector2Divide(Vector2 v1, Vector2 v2); 213 214 // Normalize provided vector 215 Vector2 Vector2Normalize(Vector2 v); 216 217 // Transforms a Vector2 by a given Matrix 218 Vector2 Vector2Transform(Vector2 v, Matrix mat); 219 220 // Calculate linear interpolation between two vectors 221 Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount); 222 223 // Calculate reflected vector to normal 224 225 // Dot product 226 Vector2 Vector2Reflect(Vector2 v, Vector2 normal); 227 228 // Rotate vector by angle 229 Vector2 Vector2Rotate(Vector2 v, float angle); 230 231 // Move Vector towards target 232 Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance); 233 234 // Invert the given vector 235 Vector2 Vector2Invert(Vector2 v); 236 237 // Clamp the components of the vector between 238 // min and max values specified by the given vectors 239 Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max); 240 241 // Clamp the magnitude of the vector between two min and max values 242 Vector2 Vector2ClampValue(Vector2 v, float min, float max); 243 244 // Check whether two given vectors are almost equal 245 int Vector2Equals(Vector2 p, Vector2 q); 246 247 //---------------------------------------------------------------------------------- 248 // Module Functions Definition - Vector3 math 249 //---------------------------------------------------------------------------------- 250 251 // Vector with components value 0.0f 252 Vector3 Vector3Zero(); 253 254 // Vector with components value 1.0f 255 Vector3 Vector3One(); 256 257 // Add two vectors 258 Vector3 Vector3Add(Vector3 v1, Vector3 v2); 259 260 // Add vector and float value 261 Vector3 Vector3AddValue(Vector3 v, float add); 262 263 // Subtract two vectors 264 Vector3 Vector3Subtract(Vector3 v1, Vector3 v2); 265 266 // Subtract vector by float value 267 Vector3 Vector3SubtractValue(Vector3 v, float sub); 268 269 // Multiply vector by scalar 270 Vector3 Vector3Scale(Vector3 v, float scalar); 271 272 // Multiply vector by vector 273 Vector3 Vector3Multiply(Vector3 v1, Vector3 v2); 274 275 // Calculate two vectors cross product 276 Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2); 277 278 // Calculate one vector perpendicular vector 279 280 // Cross product between vectors 281 Vector3 Vector3Perpendicular(Vector3 v); 282 283 // Calculate vector length 284 float Vector3Length(const Vector3 v); 285 286 // Calculate vector square length 287 float Vector3LengthSqr(const Vector3 v); 288 289 // Calculate two vectors dot product 290 float Vector3DotProduct(Vector3 v1, Vector3 v2); 291 292 // Calculate distance between two vectors 293 float Vector3Distance(Vector3 v1, Vector3 v2); 294 295 // Calculate square distance between two vectors 296 float Vector3DistanceSqr(Vector3 v1, Vector3 v2); 297 298 // Calculate angle between two vectors 299 float Vector3Angle(Vector3 v1, Vector3 v2); 300 301 // Negate provided vector (invert direction) 302 Vector3 Vector3Negate(Vector3 v); 303 304 // Divide vector by vector 305 Vector3 Vector3Divide(Vector3 v1, Vector3 v2); 306 307 // Normalize provided vector 308 Vector3 Vector3Normalize(Vector3 v); 309 310 //Calculate the projection of the vector v1 on to v2 311 Vector3 Vector3Project(Vector3 v1, Vector3 v2); 312 313 //Calculate the rejection of the vector v1 on to v2 314 Vector3 Vector3Reject(Vector3 v1, Vector3 v2); 315 316 // Orthonormalize provided vectors 317 // Makes vectors normalized and orthogonal to each other 318 // Gram-Schmidt function implementation 319 320 // Vector3Normalize(*v1); 321 322 // Vector3CrossProduct(*v1, *v2) 323 324 // Vector3Normalize(vn1); 325 326 // Vector3CrossProduct(vn1, *v1) 327 void Vector3OrthoNormalize(Vector3* v1, Vector3* v2); 328 329 // Transforms a Vector3 by a given Matrix 330 Vector3 Vector3Transform(Vector3 v, Matrix mat); 331 332 // Transform a vector by quaternion rotation 333 Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q); 334 335 // Rotates a vector around an axis 336 337 // Using Euler-Rodrigues Formula 338 // Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula 339 340 // Vector3Normalize(axis); 341 342 // Vector3CrossProduct(w, v) 343 344 // Vector3CrossProduct(w, wv) 345 346 // Vector3Scale(wv, 2*a) 347 348 // Vector3Scale(wwv, 2) 349 Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle); 350 351 // Calculate linear interpolation between two vectors 352 Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount); 353 354 // Calculate reflected vector to normal 355 356 // I is the original vector 357 // N is the normal of the incident plane 358 // R = I - (2*N*(DotProduct[I, N])) 359 Vector3 Vector3Reflect(Vector3 v, Vector3 normal); 360 361 // Get min value for each pair of components 362 Vector3 Vector3Min(Vector3 v1, Vector3 v2); 363 364 // Get max value for each pair of components 365 Vector3 Vector3Max(Vector3 v1, Vector3 v2); 366 367 // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c) 368 // NOTE: Assumes P is on the plane of the triangle 369 370 // Vector3Subtract(b, a) 371 // Vector3Subtract(c, a) 372 // Vector3Subtract(p, a) 373 // Vector3DotProduct(v0, v0) 374 // Vector3DotProduct(v0, v1) 375 // Vector3DotProduct(v1, v1) 376 // Vector3DotProduct(v2, v0) 377 // Vector3DotProduct(v2, v1) 378 Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c); 379 380 // Projects a Vector3 from screen space into object space 381 // NOTE: We are avoiding calling other raymath functions despite available 382 383 // Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it 384 // MatrixMultiply(view, projection); 385 386 // Calculate inverted matrix -> MatrixInvert(matViewProj); 387 // Cache the matrix values (speed optimization) 388 389 // Calculate the invert determinant (inlined to avoid double-caching) 390 391 // Create quaternion from source point 392 393 // Multiply quat point by unprojecte matrix 394 // QuaternionTransform(quat, matViewProjInv) 395 396 // Normalized world points in vectors 397 Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view); 398 399 // Get Vector3 as float array 400 float3 Vector3ToFloatV(Vector3 v); 401 402 // Invert the given vector 403 Vector3 Vector3Invert(Vector3 v); 404 405 // Clamp the components of the vector between 406 // min and max values specified by the given vectors 407 Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max); 408 409 // Clamp the magnitude of the vector between two values 410 Vector3 Vector3ClampValue(Vector3 v, float min, float max); 411 412 // Check whether two given vectors are almost equal 413 int Vector3Equals(Vector3 p, Vector3 q); 414 415 // Compute the direction of a refracted ray 416 // v: normalized direction of the incoming ray 417 // n: normalized normal vector of the interface of two optical media 418 // r: ratio of the refractive index of the medium from where the ray comes 419 // to the refractive index of the medium on the other side of the surface 420 Vector3 Vector3Refract(Vector3 v, Vector3 n, float r); 421 422 //---------------------------------------------------------------------------------- 423 // Module Functions Definition - Matrix math 424 //---------------------------------------------------------------------------------- 425 426 // Compute matrix determinant 427 428 // Cache the matrix values (speed optimization) 429 float MatrixDeterminant(Matrix mat); 430 431 // Get the trace of the matrix (sum of the values along the diagonal) 432 float MatrixTrace(Matrix mat); 433 434 // Transposes provided matrix 435 Matrix MatrixTranspose(Matrix mat); 436 437 // Invert provided matrix 438 439 // Cache the matrix values (speed optimization) 440 441 // Calculate the invert determinant (inlined to avoid double-caching) 442 Matrix MatrixInvert(Matrix mat); 443 444 // Get identity matrix 445 Matrix MatrixIdentity(); 446 447 // Add two matrices 448 Matrix MatrixAdd(Matrix left, Matrix right); 449 450 // Subtract two matrices (left - right) 451 Matrix MatrixSubtract(Matrix left, Matrix right); 452 453 // Get two matrix multiplication 454 // NOTE: When multiplying matrices... the order matters! 455 Matrix MatrixMultiply(Matrix left, Matrix right); 456 457 // Get translation matrix 458 Matrix MatrixTranslate(float x, float y, float z); 459 460 // Create rotation matrix from axis and angle 461 // NOTE: Angle should be provided in radians 462 Matrix MatrixRotate(Vector3 axis, float angle); 463 464 // Get x-rotation matrix 465 // NOTE: Angle must be provided in radians 466 467 // MatrixIdentity() 468 Matrix MatrixRotateX(float angle); 469 470 // Get y-rotation matrix 471 // NOTE: Angle must be provided in radians 472 473 // MatrixIdentity() 474 Matrix MatrixRotateY(float angle); 475 476 // Get z-rotation matrix 477 // NOTE: Angle must be provided in radians 478 479 // MatrixIdentity() 480 Matrix MatrixRotateZ(float angle); 481 482 // Get xyz-rotation matrix 483 // NOTE: Angle must be provided in radians 484 485 // MatrixIdentity() 486 Matrix MatrixRotateXYZ(Vector3 angle); 487 488 // Get zyx-rotation matrix 489 // NOTE: Angle must be provided in radians 490 Matrix MatrixRotateZYX(Vector3 angle); 491 492 // Get scaling matrix 493 Matrix MatrixScale(float x, float y, float z); 494 495 // Get perspective projection matrix 496 Matrix MatrixFrustum( 497 double left, 498 double right, 499 double bottom, 500 double top, 501 double near, 502 double far); 503 504 // Get perspective projection matrix 505 // NOTE: Fovy angle must be provided in radians 506 507 // MatrixFrustum(-right, right, -top, top, near, far); 508 Matrix MatrixPerspective( 509 double fovY, 510 double aspect, 511 double nearPlane, 512 double farPlane); 513 514 // Get orthographic projection matrix 515 Matrix MatrixOrtho( 516 double left, 517 double right, 518 double bottom, 519 double top, 520 double nearPlane, 521 double farPlane); 522 523 // Get camera look-at matrix (view matrix) 524 525 // Vector3Subtract(eye, target) 526 527 // Vector3Normalize(vz) 528 529 // Vector3CrossProduct(up, vz) 530 531 // Vector3Normalize(x) 532 533 // Vector3CrossProduct(vz, vx) 534 535 // Vector3DotProduct(vx, eye) 536 // Vector3DotProduct(vy, eye) 537 // Vector3DotProduct(vz, eye) 538 Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up); 539 540 // Get float array of matrix data 541 float16 MatrixToFloatV(Matrix mat); 542 543 //---------------------------------------------------------------------------------- 544 // Module Functions Definition - Quaternion math 545 //---------------------------------------------------------------------------------- 546 547 // Add two quaternions 548 Quaternion QuaternionAdd(Quaternion q1, Quaternion q2); 549 550 // Add quaternion and float value 551 Quaternion QuaternionAddValue(Quaternion q, float add); 552 553 // Subtract two quaternions 554 Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2); 555 556 // Subtract quaternion and float value 557 Quaternion QuaternionSubtractValue(Quaternion q, float sub); 558 559 // Get identity quaternion 560 Quaternion QuaternionIdentity(); 561 562 // Computes the length of a quaternion 563 float QuaternionLength(Quaternion q); 564 565 // Normalize provided quaternion 566 Quaternion QuaternionNormalize(Quaternion q); 567 568 // Invert provided quaternion 569 Quaternion QuaternionInvert(Quaternion q); 570 571 // Calculate two quaternion multiplication 572 Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); 573 574 // Scale quaternion by float value 575 Quaternion QuaternionScale(Quaternion q, float mul); 576 577 // Divide two quaternions 578 Quaternion QuaternionDivide(Quaternion q1, Quaternion q2); 579 580 // Calculate linear interpolation between two quaternions 581 Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount); 582 583 // Calculate slerp-optimized interpolation between two quaternions 584 585 // QuaternionLerp(q1, q2, amount) 586 587 // QuaternionNormalize(q); 588 Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount); 589 590 // Calculates spherical linear interpolation between two quaternions 591 Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount); 592 593 // Calculate quaternion based on the rotation from one vector to another 594 595 // Vector3DotProduct(from, to) 596 // Vector3CrossProduct(from, to) 597 598 // QuaternionNormalize(q); 599 // NOTE: Normalize to essentially nlerp the original and identity to 0.5 600 Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to); 601 602 // Get a quaternion for a given rotation matrix 603 Quaternion QuaternionFromMatrix(Matrix mat); 604 605 // Get a matrix for a given quaternion 606 607 // MatrixIdentity() 608 Matrix QuaternionToMatrix(Quaternion q); 609 610 // Get rotation quaternion for an angle and axis 611 // NOTE: Angle must be provided in radians 612 613 // Vector3Normalize(axis) 614 615 // QuaternionNormalize(q); 616 Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle); 617 618 // Get the rotation angle and axis for a given quaternion 619 620 // QuaternionNormalize(q); 621 622 // This occurs when the angle is zero. 623 // Not a problem: just set an arbitrary normalized axis. 624 void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle); 625 626 // Get the quaternion equivalent to Euler angles 627 // NOTE: Rotation order is ZYX 628 Quaternion QuaternionFromEuler(float pitch, float yaw, float roll); 629 630 // Get the Euler angles equivalent to quaternion (roll, pitch, yaw) 631 // NOTE: Angles are returned in a Vector3 struct in radians 632 633 // Roll (x-axis rotation) 634 635 // Pitch (y-axis rotation) 636 637 // Yaw (z-axis rotation) 638 Vector3 QuaternionToEuler(Quaternion q); 639 640 // Transform a quaternion given a transformation matrix 641 Quaternion QuaternionTransform(Quaternion q, Matrix mat); 642 643 // Check whether two given quaternions are almost equal 644 int QuaternionEquals(Quaternion p, Quaternion q); 645 646 // RAYMATH_H