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