2023-10-24 03:00:28 -07:00
|
|
|
/**********************************************************************************************
|
2023-11-17 16:25:54 -08:00
|
|
|
*
|
|
|
|
* raymath v1.5 - Math functions to work with RL_Vector2, RL_Vector3, RL_Matrix and Quaternions
|
|
|
|
*
|
|
|
|
* CONVENTIONS:
|
|
|
|
* - RL_Matrix structure is defined as row-major (memory layout) but parameters naming AND all
|
|
|
|
* math operations performed by the library consider the structure as it was column-major
|
|
|
|
* It is like transposed versions of the matrices are used for all the maths
|
|
|
|
* It benefits some functions making them cache-friendly and also avoids matrix
|
|
|
|
* transpositions sometimes required by OpenGL
|
|
|
|
* Example: In memory order, row0 is [m0 m4 m8 m12] but in semantic math row0 is [m0 m1 m2 m3]
|
|
|
|
* - Functions are always self-contained, no function use another raymath function inside,
|
|
|
|
* required code is directly re-implemented inside
|
|
|
|
* - Functions input parameters are always received by value (2 unavoidable exceptions)
|
|
|
|
* - Functions use always a "result" variable for return
|
|
|
|
* - Functions are always defined inline
|
|
|
|
* - Angles are always in radians (RL_DEG2RAD/RL_RAD2DEG macros provided for convenience)
|
|
|
|
* - No compound literals used to make sure libray is compatible with C++
|
|
|
|
*
|
|
|
|
* CONFIGURATION:
|
|
|
|
* #define RAYMATH_IMPLEMENTATION
|
|
|
|
* Generates the implementation of the library into the included file.
|
|
|
|
* If not defined, the library is in header only mode and can be included in other headers
|
|
|
|
* or source files without problems. But only ONE file should hold the implementation.
|
|
|
|
*
|
|
|
|
* #define RAYMATH_STATIC_INLINE
|
|
|
|
* Define static inline functions code, so #include header suffices for use.
|
|
|
|
* This may use up lots of memory.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* LICENSE: zlib/libpng
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
2023-10-24 03:00:28 -07:00
|
|
|
|
|
|
|
#ifndef RAYMATH_H
|
2023-11-17 16:25:54 -08:00
|
|
|
#define RAYMATH_H
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if defined(RAYMATH_IMPLEMENTATION) && defined(RAYMATH_STATIC_INLINE)
|
|
|
|
#error "Specifying both RAYMATH_IMPLEMENTATION and RAYMATH_STATIC_INLINE is contradictory"
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Function specifiers definition
|
2023-11-17 16:25:54 -08:00
|
|
|
#if defined(RAYMATH_IMPLEMENTATION)
|
|
|
|
#if defined(_WIN32) && defined(RL_BUILD_LIBTYPE_SHARED)
|
|
|
|
#define RMAPI __declspec(dllexport) extern inline // We are building raylib as a Win32 shared library (.dll).
|
|
|
|
#elif defined(_WIN32) && defined(RL_USE_LIBTYPE_SHARED)
|
|
|
|
#define RMAPI __declspec(dllimport) // We are using raylib as a Win32 shared library (.dll)
|
|
|
|
#else
|
|
|
|
#define RMAPI extern inline // Provide external definition
|
|
|
|
#endif
|
|
|
|
#elif defined(RAYMATH_STATIC_INLINE)
|
|
|
|
#define RMAPI static inline // Functions may be inlined, no external out-of-line definition
|
2023-10-24 03:00:28 -07:00
|
|
|
#else
|
2023-11-17 16:25:54 -08:00
|
|
|
#if defined(__TINYC__)
|
|
|
|
#define RMAPI static inline // plain inline not supported by tinycc (See issue #435)
|
|
|
|
#else
|
|
|
|
#define RMAPI inline // Functions may be inlined or external definition used
|
|
|
|
#endif
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
#ifndef RL_PI
|
|
|
|
#define RL_PI 3.14159265358979323846f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EPSILON
|
2023-11-17 16:25:54 -08:00
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#ifndef RL_DEG2RAD
|
|
|
|
#define RL_DEG2RAD (RL_PI/180.0f)
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#ifndef RL_RAD2DEG
|
|
|
|
#define RL_RAD2DEG (180.0f/RL_PI)
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Get float vector for RL_Matrix
|
|
|
|
#ifndef RL_MatrixToFloat
|
|
|
|
#define RL_MatrixToFloat(mat) (RL_MatrixToFloatV(mat).v)
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Get float vector for RL_Vector3
|
|
|
|
#ifndef RL_Vector3ToFloat
|
|
|
|
#define RL_Vector3ToFloat(vec) (RL_Vector3ToFloatV(vec).v)
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(RL_VECTOR2_TYPE)
|
|
|
|
// RL_Vector2 type
|
|
|
|
typedef struct RL_Vector2 {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
} RL_Vector2;
|
|
|
|
#define RL_VECTOR2_TYPE
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(RL_VECTOR3_TYPE)
|
|
|
|
// RL_Vector3 type
|
|
|
|
typedef struct RL_Vector3 {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
} RL_Vector3;
|
|
|
|
#define RL_VECTOR3_TYPE
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(RL_VECTOR4_TYPE)
|
|
|
|
// RL_Vector4 type
|
|
|
|
typedef struct RL_Vector4 {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
float w;
|
|
|
|
} RL_Vector4;
|
|
|
|
#define RL_VECTOR4_TYPE
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(RL_QUATERNION_TYPE)
|
|
|
|
// RL_Quaternion type
|
|
|
|
typedef RL_Vector4 RL_Quaternion;
|
|
|
|
#define RL_QUATERNION_TYPE
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(RL_MATRIX_TYPE)
|
|
|
|
// RL_Matrix type (OpenGL style 4x4 - right handed, column major)
|
|
|
|
typedef struct RL_Matrix {
|
|
|
|
float m0, m4, m8, m12; // RL_Matrix first row (4 components)
|
|
|
|
float m1, m5, m9, m13; // RL_Matrix second row (4 components)
|
|
|
|
float m2, m6, m10, m14; // RL_Matrix third row (4 components)
|
|
|
|
float m3, m7, m11, m15; // RL_Matrix fourth row (4 components)
|
|
|
|
} RL_Matrix;
|
|
|
|
#define RL_MATRIX_TYPE
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// NOTE: Helper types to be used instead of array return types for *ToFloat functions
|
2023-11-17 16:25:54 -08:00
|
|
|
typedef struct RL_float3 {
|
|
|
|
float v[3];
|
|
|
|
} RL_float3;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
typedef struct RL_float16 {
|
|
|
|
float v[16];
|
|
|
|
} RL_float16;
|
2023-10-25 00:26:25 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#include <math.h> // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabs()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Utils math
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Clamp float value
|
|
|
|
RMAPI float RL_Clamp(float value, float min, float max)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (value < min)? min : value;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (result > max) result = max;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate linear interpolation between two floats
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Lerp(float start, float end, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = start + amount*(end - start);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Normalize input value within input range
|
|
|
|
RMAPI float RL_Normalize(float value, float start, float end)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (value - start)/(end - start);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Remap input value within input range to output range
|
|
|
|
RMAPI float RL_Remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Wrap input value from min to max
|
|
|
|
RMAPI float RL_Wrap(float value, float min, float max)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = value - (max - min)*floorf((value - min)/(max - min));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether two given floats are almost equal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI int RL_FloatEquals(float x, float y)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(EPSILON)
|
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
int result = (fabsf(x - y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
// Module Functions Definition - RL_Vector2 math
|
2023-10-24 03:00:28 -07:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Vector with components value 0.0f
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Zero(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0.0f, 0.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vector with components value 1.0f
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2One(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 1.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add two vectors (v1 + v2)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Add(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v1.x + v2.x, v1.y + v2.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add vector and float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2AddValue(RL_Vector2 v, float add)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v.x + add, v.y + add };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract two vectors (v1 - v2)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Subtract(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v1.x - v2.x, v1.y - v2.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract vector by float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2SubtractValue(RL_Vector2 v, float sub)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v.x - sub, v.y - sub };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate vector length
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2Length(RL_Vector2 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = sqrtf((v.x*v.x) + (v.y*v.y));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate vector square length
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2LengthSqr(RL_Vector2 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (v.x*v.x) + (v.y*v.y);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate two vectors dot product
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2DotProduct(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (v1.x*v2.x + v1.y*v2.y);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate distance between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2Distance(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate square distance between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2DistanceSqr(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate angle between two vectors
|
|
|
|
// NOTE: Angle is calculated from origin point (0, 0)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2Angle(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dot = v1.x*v2.x + v1.y*v2.y;
|
|
|
|
float det = v1.x*v2.y - v1.y*v2.x;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result = atan2f(det, dot);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate angle defined by a two vectors line
|
|
|
|
// NOTE: Parameters need to be normalized
|
|
|
|
// Current implementation should be aligned with glm::angle
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector2LineAngle(RL_Vector2 start, RL_Vector2 end)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior
|
|
|
|
result = -atan2f(end.y - start.y, end.x - start.x);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scale vector (multiply by value)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Scale(RL_Vector2 v, float scale)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v.x*scale, v.y*scale };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply vector by vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Multiply(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v1.x*v2.x, v1.y*v2.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Negate vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Negate(RL_Vector2 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { -v.x, -v.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Divide vector by vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Divide(RL_Vector2 v1, RL_Vector2 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { v1.x/v2.x, v1.y/v2.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Normalize provided vector
|
|
|
|
RMAPI RL_Vector2 RL_Vector2Normalize(RL_Vector2 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
|
|
|
float length = sqrtf((v.x*v.x) + (v.y*v.y));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
float ilength = 1.0f/length;
|
|
|
|
result.x = v.x*ilength;
|
|
|
|
result.y = v.y*ilength;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Transforms a RL_Vector2 by a given RL_Matrix
|
|
|
|
RMAPI RL_Vector2 RL_Vector2Transform(RL_Vector2 v, RL_Matrix mat)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float x = v.x;
|
|
|
|
float y = v.y;
|
|
|
|
float z = 0;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
|
|
|
|
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate linear interpolation between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Lerp(RL_Vector2 v1, RL_Vector2 v2, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v1.x + amount*(v2.x - v1.x);
|
|
|
|
result.y = v1.y + amount*(v2.y - v1.y);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate reflected vector to normal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Reflect(RL_Vector2 v, RL_Vector2 normal)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dotProduct = (v.x*normal.x + v.y*normal.y); // Dot product
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v.x - (2.0f*normal.x)*dotProduct;
|
|
|
|
result.y = v.y - (2.0f*normal.y)*dotProduct;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate vector by angle
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Rotate(RL_Vector2 v, float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosres = cosf(angle);
|
|
|
|
float sinres = sinf(angle);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v.x*cosres - v.y*sinres;
|
|
|
|
result.y = v.x*sinres + v.y*cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move Vector towards target
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2MoveTowards(RL_Vector2 v, RL_Vector2 target, float maxDistance)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dx = target.x - v.x;
|
|
|
|
float dy = target.y - v.y;
|
|
|
|
float value = (dx*dx) + (dy*dy);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dist = sqrtf(value);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v.x + dx/dist*maxDistance;
|
|
|
|
result.y = v.y + dy/dist*maxDistance;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert the given vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Invert(RL_Vector2 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 1.0f/v.x, 1.0f/v.y };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Clamp the components of the vector between
|
2023-10-24 03:00:28 -07:00
|
|
|
// min and max values specified by the given vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector2 RL_Vector2Clamp(RL_Vector2 v, RL_Vector2 min, RL_Vector2 max)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = fminf(max.x, fmaxf(min.x, v.x));
|
|
|
|
result.y = fminf(max.y, fmaxf(min.y, v.y));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Clamp the magnitude of the vector between two min and max values
|
|
|
|
RMAPI RL_Vector2 RL_Vector2ClampValue(RL_Vector2 v, float min, float max)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector2 result = v;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = (v.x*v.x) + (v.y*v.y);
|
|
|
|
if (length > 0.0f)
|
|
|
|
{
|
|
|
|
length = sqrtf(length);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (length < min)
|
|
|
|
{
|
|
|
|
float scale = min/length;
|
|
|
|
result.x = v.x*scale;
|
|
|
|
result.y = v.y*scale;
|
|
|
|
}
|
|
|
|
else if (length > max)
|
|
|
|
{
|
|
|
|
float scale = max/length;
|
|
|
|
result.x = v.x*scale;
|
|
|
|
result.y = v.y*scale;
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether two given vectors are almost equal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI int RL_Vector2Equals(RL_Vector2 p, RL_Vector2 q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(EPSILON)
|
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
|
|
|
|
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
// Module Functions Definition - RL_Vector3 math
|
2023-10-24 03:00:28 -07:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Vector with components value 0.0f
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Zero(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0.0f, 0.0f, 0.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vector with components value 1.0f
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3One(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 1.0f, 1.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Add(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add vector and float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3AddValue(RL_Vector3 v, float add)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v.x + add, v.y + add, v.z + add };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Subtract(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract vector by float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3SubtractValue(RL_Vector3 v, float sub)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v.x - sub, v.y - sub, v.z - sub };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply vector by scalar
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Scale(RL_Vector3 v, float scalar)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply vector by vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Multiply(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate two vectors cross product
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3CrossProduct(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_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 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate one vector perpendicular vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Perpendicular(RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float min = (float) fabs(v.x);
|
|
|
|
RL_Vector3 cardinalAxis = {1.0f, 0.0f, 0.0f};
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (fabsf(v.y) < min)
|
|
|
|
{
|
|
|
|
min = (float) fabs(v.y);
|
|
|
|
RL_Vector3 tmp = {0.0f, 1.0f, 0.0f};
|
|
|
|
cardinalAxis = tmp;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (fabsf(v.z) < min)
|
|
|
|
{
|
|
|
|
RL_Vector3 tmp = {0.0f, 0.0f, 1.0f};
|
|
|
|
cardinalAxis = tmp;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Cross product between vectors
|
|
|
|
result.x = v.y*cardinalAxis.z - v.z*cardinalAxis.y;
|
|
|
|
result.y = v.z*cardinalAxis.x - v.x*cardinalAxis.z;
|
|
|
|
result.z = v.x*cardinalAxis.y - v.y*cardinalAxis.x;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate vector length
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3Length(const RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate vector square length
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3LengthSqr(const RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = v.x*v.x + v.y*v.y + v.z*v.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate two vectors dot product
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3DotProduct(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate distance between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3Distance(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dx = v2.x - v1.x;
|
|
|
|
float dy = v2.y - v1.y;
|
|
|
|
float dz = v2.z - v1.z;
|
|
|
|
result = sqrtf(dx*dx + dy*dy + dz*dz);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate square distance between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3DistanceSqr(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dx = v2.x - v1.x;
|
|
|
|
float dy = v2.y - v1.y;
|
|
|
|
float dz = v2.z - v1.z;
|
|
|
|
result = dx*dx + dy*dy + dz*dz;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate angle between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_Vector3Angle(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 cross = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
|
|
|
|
float len = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z);
|
|
|
|
float dot = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
|
|
|
|
result = atan2f(len, dot);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Negate provided vector (invert direction)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Negate(RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { -v.x, -v.y, -v.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Divide vector by vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Divide(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Normalize provided vector
|
|
|
|
RMAPI RL_Vector3 RL_Vector3Normalize(RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = v;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length != 0.0f)
|
|
|
|
{
|
|
|
|
float ilength = 1.0f/length;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x *= ilength;
|
|
|
|
result.y *= ilength;
|
|
|
|
result.z *= ilength;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
//Calculate the projection of the vector v1 on to v2
|
|
|
|
RMAPI RL_Vector3 RL_Vector3Project(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
|
|
|
|
|
|
|
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
|
|
|
|
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float mag = v1dv2/v2dv2;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v2.x*mag;
|
|
|
|
result.y = v2.y*mag;
|
|
|
|
result.z = v2.z*mag;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
//Calculate the rejection of the vector v1 on to v2
|
|
|
|
RMAPI RL_Vector3 RL_Vector3Reject(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
|
|
|
|
|
|
|
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
|
|
|
|
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float mag = v1dv2/v2dv2;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v1.x - (v2.x*mag);
|
|
|
|
result.y = v1.y - (v2.y*mag);
|
|
|
|
result.z = v1.z - (v2.z*mag);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Orthonormalize provided vectors
|
|
|
|
// Makes vectors normalized and orthogonal to each other
|
|
|
|
// Gram-Schmidt function implementation
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI void RL_Vector3OrthoNormalize(RL_Vector3 *v1, RL_Vector3 *v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = 0.0f;
|
|
|
|
float ilength = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Normalize(*v1);
|
|
|
|
RL_Vector3 v = *v1;
|
|
|
|
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
v1->x *= ilength;
|
|
|
|
v1->y *= ilength;
|
|
|
|
v1->z *= ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3CrossProduct(*v1, *v2)
|
|
|
|
RL_Vector3 vn1 = { v1->y*v2->z - v1->z*v2->y, v1->z*v2->x - v1->x*v2->z, v1->x*v2->y - v1->y*v2->x };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Normalize(vn1);
|
|
|
|
v = vn1;
|
|
|
|
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
vn1.x *= ilength;
|
|
|
|
vn1.y *= ilength;
|
|
|
|
vn1.z *= ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3CrossProduct(vn1, *v1)
|
|
|
|
RL_Vector3 vn2 = { vn1.y*v1->z - vn1.z*v1->y, vn1.z*v1->x - vn1.x*v1->z, vn1.x*v1->y - vn1.y*v1->x };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
*v2 = vn2;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Transforms a RL_Vector3 by a given RL_Matrix
|
|
|
|
RMAPI RL_Vector3 RL_Vector3Transform(RL_Vector3 v, RL_Matrix mat)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float x = v.x;
|
|
|
|
float y = v.y;
|
|
|
|
float z = v.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
|
|
|
|
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
|
|
|
|
result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Transform a vector by quaternion rotation
|
|
|
|
RMAPI RL_Vector3 RL_Vector3RotateByQuaternion(RL_Vector3 v, RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
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);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rotates a vector around an axis
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3RotateByAxisAngle(RL_Vector3 v, RL_Vector3 axis, float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
// Using Euler-Rodrigues Formula
|
|
|
|
// Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
|
|
|
|
|
|
|
|
RL_Vector3 result = v;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Normalize(axis);
|
|
|
|
float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
float ilength = 1.0f / length;
|
|
|
|
axis.x *= ilength;
|
|
|
|
axis.y *= ilength;
|
|
|
|
axis.z *= ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
angle /= 2.0f;
|
|
|
|
float a = sinf(angle);
|
|
|
|
float b = axis.x*a;
|
|
|
|
float c = axis.y*a;
|
|
|
|
float d = axis.z*a;
|
|
|
|
a = cosf(angle);
|
|
|
|
RL_Vector3 w = { b, c, d };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3CrossProduct(w, v)
|
|
|
|
RL_Vector3 wv = { w.y*v.z - w.z*v.y, w.z*v.x - w.x*v.z, w.x*v.y - w.y*v.x };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3CrossProduct(w, wv)
|
|
|
|
RL_Vector3 wwv = { w.y*wv.z - w.z*wv.y, w.z*wv.x - w.x*wv.z, w.x*wv.y - w.y*wv.x };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Scale(wv, 2*a)
|
|
|
|
a *= 2;
|
|
|
|
wv.x *= a;
|
|
|
|
wv.y *= a;
|
|
|
|
wv.z *= a;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Scale(wwv, 2)
|
|
|
|
wwv.x *= 2;
|
|
|
|
wwv.y *= 2;
|
|
|
|
wwv.z *= 2;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x += wv.x;
|
|
|
|
result.y += wv.y;
|
|
|
|
result.z += wv.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x += wwv.x;
|
|
|
|
result.y += wwv.y;
|
|
|
|
result.z += wwv.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate linear interpolation between two vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Lerp(RL_Vector3 v1, RL_Vector3 v2, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v1.x + amount*(v2.x - v1.x);
|
|
|
|
result.y = v1.y + amount*(v2.y - v1.y);
|
|
|
|
result.z = v1.z + amount*(v2.z - v1.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate reflected vector to normal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Reflect(RL_Vector3 v, RL_Vector3 normal)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// I is the original vector
|
|
|
|
// N is the normal of the incident plane
|
|
|
|
// R = I - (2*N*(DotProduct[I, N]))
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dotProduct = (v.x*normal.x + v.y*normal.y + v.z*normal.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = v.x - (2.0f*normal.x)*dotProduct;
|
|
|
|
result.y = v.y - (2.0f*normal.y)*dotProduct;
|
|
|
|
result.z = v.z - (2.0f*normal.z)*dotProduct;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get min value for each pair of components
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Min(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = fminf(v1.x, v2.x);
|
|
|
|
result.y = fminf(v1.y, v2.y);
|
|
|
|
result.z = fminf(v1.z, v2.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get max value for each pair of components
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Max(RL_Vector3 v1, RL_Vector3 v2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = fmaxf(v1.x, v2.x);
|
|
|
|
result.y = fmaxf(v1.y, v2.y);
|
|
|
|
result.z = fmaxf(v1.z, v2.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
|
|
|
|
// NOTE: Assumes P is on the plane of the triangle
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Barycenter(RL_Vector3 p, RL_Vector3 a, RL_Vector3 b, RL_Vector3 c)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 v0 = { b.x - a.x, b.y - a.y, b.z - a.z }; // RL_Vector3Subtract(b, a)
|
|
|
|
RL_Vector3 v1 = { c.x - a.x, c.y - a.y, c.z - a.z }; // RL_Vector3Subtract(c, a)
|
|
|
|
RL_Vector3 v2 = { p.x - a.x, p.y - a.y, p.z - a.z }; // RL_Vector3Subtract(p, a)
|
|
|
|
float d00 = (v0.x*v0.x + v0.y*v0.y + v0.z*v0.z); // RL_Vector3DotProduct(v0, v0)
|
|
|
|
float d01 = (v0.x*v1.x + v0.y*v1.y + v0.z*v1.z); // RL_Vector3DotProduct(v0, v1)
|
|
|
|
float d11 = (v1.x*v1.x + v1.y*v1.y + v1.z*v1.z); // RL_Vector3DotProduct(v1, v1)
|
|
|
|
float d20 = (v2.x*v0.x + v2.y*v0.y + v2.z*v0.z); // RL_Vector3DotProduct(v2, v0)
|
|
|
|
float d21 = (v2.x*v1.x + v2.y*v1.y + v2.z*v1.z); // RL_Vector3DotProduct(v2, v1)
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float denom = d00*d11 - d01*d01;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.y = (d11*d20 - d01*d21)/denom;
|
|
|
|
result.z = (d00*d21 - d01*d20)/denom;
|
|
|
|
result.x = 1.0f - (result.z + result.y);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Projects a RL_Vector3 from screen space into object space
|
2023-10-24 03:00:28 -07:00
|
|
|
// NOTE: We are avoiding calling other raymath functions despite available
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Unproject(RL_Vector3 source, RL_Matrix projection, RL_Matrix view)
|
|
|
|
{
|
|
|
|
RL_Vector3 result = { 0 };
|
|
|
|
|
|
|
|
// Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it
|
|
|
|
RL_Matrix matViewProj = { // RL_MatrixMultiply(view, projection);
|
|
|
|
view.m0*projection.m0 + view.m1*projection.m4 + view.m2*projection.m8 + view.m3*projection.m12,
|
|
|
|
view.m0*projection.m1 + view.m1*projection.m5 + view.m2*projection.m9 + view.m3*projection.m13,
|
|
|
|
view.m0*projection.m2 + view.m1*projection.m6 + view.m2*projection.m10 + view.m3*projection.m14,
|
|
|
|
view.m0*projection.m3 + view.m1*projection.m7 + view.m2*projection.m11 + view.m3*projection.m15,
|
|
|
|
view.m4*projection.m0 + view.m5*projection.m4 + view.m6*projection.m8 + view.m7*projection.m12,
|
|
|
|
view.m4*projection.m1 + view.m5*projection.m5 + view.m6*projection.m9 + view.m7*projection.m13,
|
|
|
|
view.m4*projection.m2 + view.m5*projection.m6 + view.m6*projection.m10 + view.m7*projection.m14,
|
|
|
|
view.m4*projection.m3 + view.m5*projection.m7 + view.m6*projection.m11 + view.m7*projection.m15,
|
|
|
|
view.m8*projection.m0 + view.m9*projection.m4 + view.m10*projection.m8 + view.m11*projection.m12,
|
|
|
|
view.m8*projection.m1 + view.m9*projection.m5 + view.m10*projection.m9 + view.m11*projection.m13,
|
|
|
|
view.m8*projection.m2 + view.m9*projection.m6 + view.m10*projection.m10 + view.m11*projection.m14,
|
|
|
|
view.m8*projection.m3 + view.m9*projection.m7 + view.m10*projection.m11 + view.m11*projection.m15,
|
|
|
|
view.m12*projection.m0 + view.m13*projection.m4 + view.m14*projection.m8 + view.m15*projection.m12,
|
|
|
|
view.m12*projection.m1 + view.m13*projection.m5 + view.m14*projection.m9 + view.m15*projection.m13,
|
|
|
|
view.m12*projection.m2 + view.m13*projection.m6 + view.m14*projection.m10 + view.m15*projection.m14,
|
|
|
|
view.m12*projection.m3 + view.m13*projection.m7 + view.m14*projection.m11 + view.m15*projection.m15 };
|
|
|
|
|
|
|
|
// Calculate inverted matrix -> RL_MatrixInvert(matViewProj);
|
|
|
|
// Cache the matrix values (speed optimization)
|
|
|
|
float a00 = matViewProj.m0, a01 = matViewProj.m1, a02 = matViewProj.m2, a03 = matViewProj.m3;
|
|
|
|
float a10 = matViewProj.m4, a11 = matViewProj.m5, a12 = matViewProj.m6, a13 = matViewProj.m7;
|
|
|
|
float a20 = matViewProj.m8, a21 = matViewProj.m9, a22 = matViewProj.m10, a23 = matViewProj.m11;
|
|
|
|
float a30 = matViewProj.m12, a31 = matViewProj.m13, a32 = matViewProj.m14, a33 = matViewProj.m15;
|
|
|
|
|
|
|
|
float b00 = a00*a11 - a01*a10;
|
|
|
|
float b01 = a00*a12 - a02*a10;
|
|
|
|
float b02 = a00*a13 - a03*a10;
|
|
|
|
float b03 = a01*a12 - a02*a11;
|
|
|
|
float b04 = a01*a13 - a03*a11;
|
|
|
|
float b05 = a02*a13 - a03*a12;
|
|
|
|
float b06 = a20*a31 - a21*a30;
|
|
|
|
float b07 = a20*a32 - a22*a30;
|
|
|
|
float b08 = a20*a33 - a23*a30;
|
|
|
|
float b09 = a21*a32 - a22*a31;
|
|
|
|
float b10 = a21*a33 - a23*a31;
|
|
|
|
float b11 = a22*a33 - a23*a32;
|
|
|
|
|
|
|
|
// Calculate the invert determinant (inlined to avoid double-caching)
|
|
|
|
float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
|
|
|
|
|
|
|
|
RL_Matrix matViewProjInv = {
|
|
|
|
(a11*b11 - a12*b10 + a13*b09)*invDet,
|
|
|
|
(-a01*b11 + a02*b10 - a03*b09)*invDet,
|
|
|
|
(a31*b05 - a32*b04 + a33*b03)*invDet,
|
|
|
|
(-a21*b05 + a22*b04 - a23*b03)*invDet,
|
|
|
|
(-a10*b11 + a12*b08 - a13*b07)*invDet,
|
|
|
|
(a00*b11 - a02*b08 + a03*b07)*invDet,
|
|
|
|
(-a30*b05 + a32*b02 - a33*b01)*invDet,
|
|
|
|
(a20*b05 - a22*b02 + a23*b01)*invDet,
|
|
|
|
(a10*b10 - a11*b08 + a13*b06)*invDet,
|
|
|
|
(-a00*b10 + a01*b08 - a03*b06)*invDet,
|
|
|
|
(a30*b04 - a31*b02 + a33*b00)*invDet,
|
|
|
|
(-a20*b04 + a21*b02 - a23*b00)*invDet,
|
|
|
|
(-a10*b09 + a11*b07 - a12*b06)*invDet,
|
|
|
|
(a00*b09 - a01*b07 + a02*b06)*invDet,
|
|
|
|
(-a30*b03 + a31*b01 - a32*b00)*invDet,
|
|
|
|
(a20*b03 - a21*b01 + a22*b00)*invDet };
|
|
|
|
|
|
|
|
// Create quaternion from source point
|
|
|
|
RL_Quaternion quat = { source.x, source.y, source.z, 1.0f };
|
|
|
|
|
|
|
|
// Multiply quat point by unprojecte matrix
|
|
|
|
RL_Quaternion qtransformed = { // RL_QuaternionTransform(quat, matViewProjInv)
|
|
|
|
matViewProjInv.m0*quat.x + matViewProjInv.m4*quat.y + matViewProjInv.m8*quat.z + matViewProjInv.m12*quat.w,
|
|
|
|
matViewProjInv.m1*quat.x + matViewProjInv.m5*quat.y + matViewProjInv.m9*quat.z + matViewProjInv.m13*quat.w,
|
|
|
|
matViewProjInv.m2*quat.x + matViewProjInv.m6*quat.y + matViewProjInv.m10*quat.z + matViewProjInv.m14*quat.w,
|
|
|
|
matViewProjInv.m3*quat.x + matViewProjInv.m7*quat.y + matViewProjInv.m11*quat.z + matViewProjInv.m15*quat.w };
|
|
|
|
|
|
|
|
// Normalized world points in vectors
|
|
|
|
result.x = qtransformed.x/qtransformed.w;
|
|
|
|
result.y = qtransformed.y/qtransformed.w;
|
|
|
|
result.z = qtransformed.z/qtransformed.w;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get RL_Vector3 as float array
|
|
|
|
RMAPI RL_float3 RL_Vector3ToFloatV(RL_Vector3 v)
|
|
|
|
{
|
|
|
|
RL_float3 buffer = { 0 };
|
|
|
|
|
|
|
|
buffer.v[0] = v.x;
|
|
|
|
buffer.v[1] = v.y;
|
|
|
|
buffer.v[2] = v.z;
|
|
|
|
|
|
|
|
return buffer;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert the given vector
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Invert(RL_Vector3 v)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Clamp the components of the vector between
|
2023-10-24 03:00:28 -07:00
|
|
|
// min and max values specified by the given vectors
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Clamp(RL_Vector3 v, RL_Vector3 min, RL_Vector3 max)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = fminf(max.x, fmaxf(min.x, v.x));
|
|
|
|
result.y = fminf(max.y, fmaxf(min.y, v.y));
|
|
|
|
result.z = fminf(max.z, fmaxf(min.z, v.z));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Clamp the magnitude of the vector between two values
|
|
|
|
RMAPI RL_Vector3 RL_Vector3ClampValue(RL_Vector3 v, float min, float max)
|
2023-10-25 00:26:25 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = v;
|
2023-10-25 00:26:25 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
|
|
|
|
if (length > 0.0f)
|
|
|
|
{
|
|
|
|
length = sqrtf(length);
|
2023-10-25 00:26:25 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (length < min)
|
|
|
|
{
|
|
|
|
float scale = min/length;
|
|
|
|
result.x = v.x*scale;
|
|
|
|
result.y = v.y*scale;
|
|
|
|
result.z = v.z*scale;
|
|
|
|
}
|
|
|
|
else if (length > max)
|
|
|
|
{
|
|
|
|
float scale = max/length;
|
|
|
|
result.x = v.x*scale;
|
|
|
|
result.y = v.y*scale;
|
|
|
|
result.z = v.z*scale;
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 00:26:25 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether two given vectors are almost equal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI int RL_Vector3Equals(RL_Vector3 p, RL_Vector3 q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(EPSILON)
|
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
|
|
|
|
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
|
|
|
|
((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z)))));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the direction of a refracted ray
|
|
|
|
// v: normalized direction of the incoming ray
|
|
|
|
// n: normalized normal vector of the interface of two optical media
|
|
|
|
// r: ratio of the refractive index of the medium from where the ray comes
|
|
|
|
// to the refractive index of the medium on the other side of the surface
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Vector3 RL_Vector3Refract(RL_Vector3 v, RL_Vector3 n, float r)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float dot = v.x*n.x + v.y*n.y + v.z*n.z;
|
|
|
|
float d = 1.0f - r*r*(1.0f - dot*dot);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (d >= 0.0f)
|
|
|
|
{
|
|
|
|
d = sqrtf(d);
|
|
|
|
v.x = r*v.x - (r*dot + d)*n.x;
|
|
|
|
v.y = r*v.y - (r*dot + d)*n.y;
|
|
|
|
v.z = r*v.z - (r*dot + d)*n.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result = v;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
// Module Functions Definition - RL_Matrix math
|
2023-10-24 03:00:28 -07:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Compute matrix determinant
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_MatrixDeterminant(RL_Matrix mat)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Cache the matrix values (speed optimization)
|
|
|
|
float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
|
|
|
|
float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
|
|
|
|
float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
|
|
|
|
float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
|
|
|
|
a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
|
|
|
|
a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
|
|
|
|
a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
|
|
|
|
a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
|
|
|
|
a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the trace of the matrix (sum of the values along the diagonal)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_MatrixTrace(RL_Matrix mat)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transposes provided matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixTranspose(RL_Matrix mat)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
result.m0 = mat.m0;
|
|
|
|
result.m1 = mat.m4;
|
|
|
|
result.m2 = mat.m8;
|
|
|
|
result.m3 = mat.m12;
|
|
|
|
result.m4 = mat.m1;
|
|
|
|
result.m5 = mat.m5;
|
|
|
|
result.m6 = mat.m9;
|
|
|
|
result.m7 = mat.m13;
|
|
|
|
result.m8 = mat.m2;
|
|
|
|
result.m9 = mat.m6;
|
|
|
|
result.m10 = mat.m10;
|
|
|
|
result.m11 = mat.m14;
|
|
|
|
result.m12 = mat.m3;
|
|
|
|
result.m13 = mat.m7;
|
|
|
|
result.m14 = mat.m11;
|
|
|
|
result.m15 = mat.m15;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert provided matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixInvert(RL_Matrix mat)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
// Cache the matrix values (speed optimization)
|
|
|
|
float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
|
|
|
|
float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
|
|
|
|
float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
|
|
|
|
float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;
|
|
|
|
|
|
|
|
float b00 = a00*a11 - a01*a10;
|
|
|
|
float b01 = a00*a12 - a02*a10;
|
|
|
|
float b02 = a00*a13 - a03*a10;
|
|
|
|
float b03 = a01*a12 - a02*a11;
|
|
|
|
float b04 = a01*a13 - a03*a11;
|
|
|
|
float b05 = a02*a13 - a03*a12;
|
|
|
|
float b06 = a20*a31 - a21*a30;
|
|
|
|
float b07 = a20*a32 - a22*a30;
|
|
|
|
float b08 = a20*a33 - a23*a30;
|
|
|
|
float b09 = a21*a32 - a22*a31;
|
|
|
|
float b10 = a21*a33 - a23*a31;
|
|
|
|
float b11 = a22*a33 - a23*a32;
|
|
|
|
|
|
|
|
// Calculate the invert determinant (inlined to avoid double-caching)
|
|
|
|
float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);
|
|
|
|
|
|
|
|
result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
|
|
|
|
result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
|
|
|
|
result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
|
|
|
|
result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
|
|
|
|
result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
|
|
|
|
result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
|
|
|
|
result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
|
|
|
|
result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
|
|
|
|
result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
|
|
|
|
result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
|
|
|
|
result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
|
|
|
|
result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
|
|
|
|
result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
|
|
|
|
result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
|
|
|
|
result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
|
|
|
|
result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get identity matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixIdentity(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add two matrices
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixAdd(RL_Matrix left, RL_Matrix right)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
result.m0 = left.m0 + right.m0;
|
|
|
|
result.m1 = left.m1 + right.m1;
|
|
|
|
result.m2 = left.m2 + right.m2;
|
|
|
|
result.m3 = left.m3 + right.m3;
|
|
|
|
result.m4 = left.m4 + right.m4;
|
|
|
|
result.m5 = left.m5 + right.m5;
|
|
|
|
result.m6 = left.m6 + right.m6;
|
|
|
|
result.m7 = left.m7 + right.m7;
|
|
|
|
result.m8 = left.m8 + right.m8;
|
|
|
|
result.m9 = left.m9 + right.m9;
|
|
|
|
result.m10 = left.m10 + right.m10;
|
|
|
|
result.m11 = left.m11 + right.m11;
|
|
|
|
result.m12 = left.m12 + right.m12;
|
|
|
|
result.m13 = left.m13 + right.m13;
|
|
|
|
result.m14 = left.m14 + right.m14;
|
|
|
|
result.m15 = left.m15 + right.m15;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract two matrices (left - right)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixSubtract(RL_Matrix left, RL_Matrix right)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
result.m0 = left.m0 - right.m0;
|
|
|
|
result.m1 = left.m1 - right.m1;
|
|
|
|
result.m2 = left.m2 - right.m2;
|
|
|
|
result.m3 = left.m3 - right.m3;
|
|
|
|
result.m4 = left.m4 - right.m4;
|
|
|
|
result.m5 = left.m5 - right.m5;
|
|
|
|
result.m6 = left.m6 - right.m6;
|
|
|
|
result.m7 = left.m7 - right.m7;
|
|
|
|
result.m8 = left.m8 - right.m8;
|
|
|
|
result.m9 = left.m9 - right.m9;
|
|
|
|
result.m10 = left.m10 - right.m10;
|
|
|
|
result.m11 = left.m11 - right.m11;
|
|
|
|
result.m12 = left.m12 - right.m12;
|
|
|
|
result.m13 = left.m13 - right.m13;
|
|
|
|
result.m14 = left.m14 - right.m14;
|
|
|
|
result.m15 = left.m15 - right.m15;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get two matrix multiplication
|
|
|
|
// NOTE: When multiplying matrices... the order matters!
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixMultiply(RL_Matrix left, RL_Matrix right)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
|
|
|
|
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
|
|
|
|
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
|
|
|
|
result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
|
|
|
|
result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
|
|
|
|
result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
|
|
|
|
result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
|
|
|
|
result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
|
|
|
|
result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
|
|
|
|
result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
|
|
|
|
result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
|
|
|
|
result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
|
|
|
|
result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
|
|
|
|
result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
|
|
|
|
result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
|
|
|
|
result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get translation matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixTranslate(float x, float y, float z)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, x,
|
|
|
|
0.0f, 1.0f, 0.0f, y,
|
|
|
|
0.0f, 0.0f, 1.0f, z,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create rotation matrix from axis and angle
|
|
|
|
// NOTE: Angle should be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotate(RL_Vector3 axis, float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float x = axis.x, y = axis.y, z = axis.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float lengthSquared = x*x + y*y + z*z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
|
|
|
|
{
|
|
|
|
float ilength = 1.0f/sqrtf(lengthSquared);
|
|
|
|
x *= ilength;
|
|
|
|
y *= ilength;
|
|
|
|
z *= ilength;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float sinres = sinf(angle);
|
|
|
|
float cosres = cosf(angle);
|
|
|
|
float t = 1.0f - cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = x*x*t + cosres;
|
|
|
|
result.m1 = y*x*t + z*sinres;
|
|
|
|
result.m2 = z*x*t - y*sinres;
|
|
|
|
result.m3 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m4 = x*y*t - z*sinres;
|
|
|
|
result.m5 = y*y*t + cosres;
|
|
|
|
result.m6 = z*y*t + x*sinres;
|
|
|
|
result.m7 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m8 = x*z*t + y*sinres;
|
|
|
|
result.m9 = y*z*t - x*sinres;
|
|
|
|
result.m10 = z*z*t + cosres;
|
|
|
|
result.m11 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m12 = 0.0f;
|
|
|
|
result.m13 = 0.0f;
|
|
|
|
result.m14 = 0.0f;
|
|
|
|
result.m15 = 1.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get x-rotation matrix
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotateX(float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f }; // RL_MatrixIdentity()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosres = cosf(angle);
|
|
|
|
float sinres = sinf(angle);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m5 = cosres;
|
|
|
|
result.m6 = sinres;
|
|
|
|
result.m9 = -sinres;
|
|
|
|
result.m10 = cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get y-rotation matrix
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotateY(float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f }; // RL_MatrixIdentity()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosres = cosf(angle);
|
|
|
|
float sinres = sinf(angle);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = cosres;
|
|
|
|
result.m2 = -sinres;
|
|
|
|
result.m8 = sinres;
|
|
|
|
result.m10 = cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get z-rotation matrix
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotateZ(float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f }; // RL_MatrixIdentity()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosres = cosf(angle);
|
|
|
|
float sinres = sinf(angle);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = cosres;
|
|
|
|
result.m1 = sinres;
|
|
|
|
result.m4 = -sinres;
|
|
|
|
result.m5 = cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
|
2023-10-24 03:00:28 -07:00
|
|
|
// Get xyz-rotation matrix
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotateXYZ(RL_Vector3 angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f }; // RL_MatrixIdentity()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosz = cosf(-angle.z);
|
|
|
|
float sinz = sinf(-angle.z);
|
|
|
|
float cosy = cosf(-angle.y);
|
|
|
|
float siny = sinf(-angle.y);
|
|
|
|
float cosx = cosf(-angle.x);
|
|
|
|
float sinx = sinf(-angle.x);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = cosz*cosy;
|
|
|
|
result.m1 = (cosz*siny*sinx) - (sinz*cosx);
|
|
|
|
result.m2 = (cosz*siny*cosx) + (sinz*sinx);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m4 = sinz*cosy;
|
|
|
|
result.m5 = (sinz*siny*sinx) + (cosz*cosx);
|
|
|
|
result.m6 = (sinz*siny*cosx) - (cosz*sinx);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m8 = -siny;
|
|
|
|
result.m9 = cosy*sinx;
|
|
|
|
result.m10= cosy*cosx;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get zyx-rotation matrix
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixRotateZYX(RL_Vector3 angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cz = cosf(angle.z);
|
|
|
|
float sz = sinf(angle.z);
|
|
|
|
float cy = cosf(angle.y);
|
|
|
|
float sy = sinf(angle.y);
|
|
|
|
float cx = cosf(angle.x);
|
|
|
|
float sx = sinf(angle.x);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = cz*cy;
|
|
|
|
result.m4 = cz*sy*sx - cx*sz;
|
|
|
|
result.m8 = sz*sx + cz*cx*sy;
|
|
|
|
result.m12 = 0;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m1 = cy*sz;
|
|
|
|
result.m5 = cz*cx + sz*sy*sx;
|
|
|
|
result.m9 = cx*sz*sy - cz*sx;
|
|
|
|
result.m13 = 0;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m2 = -sy;
|
|
|
|
result.m6 = cy*sx;
|
|
|
|
result.m10 = cy*cx;
|
|
|
|
result.m14 = 0;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m3 = 0;
|
|
|
|
result.m7 = 0;
|
|
|
|
result.m11 = 0;
|
|
|
|
result.m15 = 1;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get scaling matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixScale(float x, float y, float z)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { x, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, y, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, z, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get perspective projection matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixFrustum(double left, double right, double bottom, double top, double near, double far)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float RLGL_ = (float)(right - left);
|
|
|
|
float tb = (float)(top - bottom);
|
|
|
|
float fn = (float)(far - near);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = ((float)near*2.0f)/RLGL_;
|
|
|
|
result.m1 = 0.0f;
|
|
|
|
result.m2 = 0.0f;
|
|
|
|
result.m3 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m4 = 0.0f;
|
|
|
|
result.m5 = ((float)near*2.0f)/tb;
|
|
|
|
result.m6 = 0.0f;
|
|
|
|
result.m7 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m8 = ((float)right + (float)left)/RLGL_;
|
|
|
|
result.m9 = ((float)top + (float)bottom)/tb;
|
|
|
|
result.m10 = -((float)far + (float)near)/fn;
|
|
|
|
result.m11 = -1.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m12 = 0.0f;
|
|
|
|
result.m13 = 0.0f;
|
|
|
|
result.m14 = -((float)far*(float)near*2.0f)/fn;
|
|
|
|
result.m15 = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get perspective projection matrix
|
|
|
|
// NOTE: Fovy angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixPerspective(double fovY, double aspect, double nearPlane, double farPlane)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
double top = nearPlane*tan(fovY*0.5);
|
|
|
|
double bottom = -top;
|
|
|
|
double right = top*aspect;
|
|
|
|
double left = -right;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_MatrixFrustum(-right, right, -top, top, near, far);
|
|
|
|
float RLGL_ = (float)(right - left);
|
|
|
|
float tb = (float)(top - bottom);
|
|
|
|
float fn = (float)(farPlane - nearPlane);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = ((float)nearPlane*2.0f)/RLGL_;
|
|
|
|
result.m5 = ((float)nearPlane*2.0f)/tb;
|
|
|
|
result.m8 = ((float)right + (float)left)/RLGL_;
|
|
|
|
result.m9 = ((float)top + (float)bottom)/tb;
|
|
|
|
result.m10 = -((float)farPlane + (float)nearPlane)/fn;
|
|
|
|
result.m11 = -1.0f;
|
|
|
|
result.m14 = -((float)farPlane*(float)nearPlane*2.0f)/fn;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get orthographic projection matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixOrtho(double left, double right, double bottom, double top, double nearPlane, double farPlane)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
float RLGL_ = (float)(right - left);
|
|
|
|
float tb = (float)(top - bottom);
|
|
|
|
float fn = (float)(farPlane - nearPlane);
|
|
|
|
|
|
|
|
result.m0 = 2.0f/RLGL_;
|
|
|
|
result.m1 = 0.0f;
|
|
|
|
result.m2 = 0.0f;
|
|
|
|
result.m3 = 0.0f;
|
|
|
|
result.m4 = 0.0f;
|
|
|
|
result.m5 = 2.0f/tb;
|
|
|
|
result.m6 = 0.0f;
|
|
|
|
result.m7 = 0.0f;
|
|
|
|
result.m8 = 0.0f;
|
|
|
|
result.m9 = 0.0f;
|
|
|
|
result.m10 = -2.0f/fn;
|
|
|
|
result.m11 = 0.0f;
|
|
|
|
result.m12 = -((float)left + (float)right)/RLGL_;
|
|
|
|
result.m13 = -((float)top + (float)bottom)/tb;
|
|
|
|
result.m14 = -((float)farPlane + (float)nearPlane)/fn;
|
|
|
|
result.m15 = 1.0f;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get camera look-at matrix (view matrix)
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_MatrixLookAt(RL_Vector3 eye, RL_Vector3 target, RL_Vector3 up)
|
|
|
|
{
|
|
|
|
RL_Matrix result = { 0 };
|
|
|
|
|
|
|
|
float length = 0.0f;
|
|
|
|
float ilength = 0.0f;
|
|
|
|
|
|
|
|
// RL_Vector3Subtract(eye, target)
|
|
|
|
RL_Vector3 vz = { eye.x - target.x, eye.y - target.y, eye.z - target.z };
|
|
|
|
|
|
|
|
// RL_Vector3Normalize(vz)
|
|
|
|
RL_Vector3 v = vz;
|
|
|
|
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
vz.x *= ilength;
|
|
|
|
vz.y *= ilength;
|
|
|
|
vz.z *= ilength;
|
|
|
|
|
|
|
|
// RL_Vector3CrossProduct(up, vz)
|
|
|
|
RL_Vector3 vx = { up.y*vz.z - up.z*vz.y, up.z*vz.x - up.x*vz.z, up.x*vz.y - up.y*vz.x };
|
|
|
|
|
|
|
|
// RL_Vector3Normalize(x)
|
|
|
|
v = vx;
|
|
|
|
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
vx.x *= ilength;
|
|
|
|
vx.y *= ilength;
|
|
|
|
vx.z *= ilength;
|
|
|
|
|
|
|
|
// RL_Vector3CrossProduct(vz, vx)
|
|
|
|
RL_Vector3 vy = { vz.y*vx.z - vz.z*vx.y, vz.z*vx.x - vz.x*vx.z, vz.x*vx.y - vz.y*vx.x };
|
|
|
|
|
|
|
|
result.m0 = vx.x;
|
|
|
|
result.m1 = vy.x;
|
|
|
|
result.m2 = vz.x;
|
|
|
|
result.m3 = 0.0f;
|
|
|
|
result.m4 = vx.y;
|
|
|
|
result.m5 = vy.y;
|
|
|
|
result.m6 = vz.y;
|
|
|
|
result.m7 = 0.0f;
|
|
|
|
result.m8 = vx.z;
|
|
|
|
result.m9 = vy.z;
|
|
|
|
result.m10 = vz.z;
|
|
|
|
result.m11 = 0.0f;
|
|
|
|
result.m12 = -(vx.x*eye.x + vx.y*eye.y + vx.z*eye.z); // RL_Vector3DotProduct(vx, eye)
|
|
|
|
result.m13 = -(vy.x*eye.x + vy.y*eye.y + vy.z*eye.z); // RL_Vector3DotProduct(vy, eye)
|
|
|
|
result.m14 = -(vz.x*eye.x + vz.y*eye.y + vz.z*eye.z); // RL_Vector3DotProduct(vz, eye)
|
|
|
|
result.m15 = 1.0f;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get float array of matrix data
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_float16 RL_MatrixToFloatV(RL_Matrix mat)
|
|
|
|
{
|
|
|
|
RL_float16 result = { 0 };
|
|
|
|
|
|
|
|
result.v[0] = mat.m0;
|
|
|
|
result.v[1] = mat.m1;
|
|
|
|
result.v[2] = mat.m2;
|
|
|
|
result.v[3] = mat.m3;
|
|
|
|
result.v[4] = mat.m4;
|
|
|
|
result.v[5] = mat.m5;
|
|
|
|
result.v[6] = mat.m6;
|
|
|
|
result.v[7] = mat.m7;
|
|
|
|
result.v[8] = mat.m8;
|
|
|
|
result.v[9] = mat.m9;
|
|
|
|
result.v[10] = mat.m10;
|
|
|
|
result.v[11] = mat.m11;
|
|
|
|
result.v[12] = mat.m12;
|
|
|
|
result.v[13] = mat.m13;
|
|
|
|
result.v[14] = mat.m14;
|
|
|
|
result.v[15] = mat.m15;
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-11-17 16:25:54 -08:00
|
|
|
// Module Functions Definition - RL_Quaternion math
|
2023-10-24 03:00:28 -07:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Add two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionAdd(RL_Quaternion q1, RL_Quaternion q2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = {q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w};
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add quaternion and float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionAddValue(RL_Quaternion q, float add)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = {q.x + add, q.y + add, q.z + add, q.w + add};
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionSubtract(RL_Quaternion q1, RL_Quaternion q2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = {q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w};
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract quaternion and float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionSubtractValue(RL_Quaternion q, float sub)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = {q.x - sub, q.y - sub, q.z - sub, q.w - sub};
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get identity quaternion
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionIdentity(void)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the length of a quaternion
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI float RL_QuaternionLength(RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
float result = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Normalize provided quaternion
|
|
|
|
RMAPI RL_Quaternion RL_QuaternionNormalize(RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
float ilength = 1.0f/length;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = q.x*ilength;
|
|
|
|
result.y = q.y*ilength;
|
|
|
|
result.z = q.z*ilength;
|
|
|
|
result.w = q.w*ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert provided quaternion
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionInvert(RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = q;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (lengthSq != 0.0f)
|
|
|
|
{
|
|
|
|
float invLength = 1.0f/lengthSq;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x *= -invLength;
|
|
|
|
result.y *= -invLength;
|
|
|
|
result.z *= -invLength;
|
|
|
|
result.w *= invLength;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate two quaternion multiplication
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionMultiply(RL_Quaternion q1, RL_Quaternion q2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w;
|
|
|
|
float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
|
|
|
|
result.y = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
|
|
|
|
result.z = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
|
|
|
|
result.w = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scale quaternion by float value
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionScale(RL_Quaternion q, float mul)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = q.x*mul;
|
|
|
|
result.y = q.y*mul;
|
|
|
|
result.z = q.z*mul;
|
|
|
|
result.w = q.w*mul;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Divide two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionDivide(RL_Quaternion q1, RL_Quaternion q2)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { q1.x/q2.x, q1.y/q2.y, q1.z/q2.z, q1.w/q2.w };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate linear interpolation between two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionLerp(RL_Quaternion q1, RL_Quaternion q2, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = q1.x + amount*(q2.x - q1.x);
|
|
|
|
result.y = q1.y + amount*(q2.y - q1.y);
|
|
|
|
result.z = q1.z + amount*(q2.z - q1.z);
|
|
|
|
result.w = q1.w + amount*(q2.w - q1.w);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate slerp-optimized interpolation between two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionNlerp(RL_Quaternion q1, RL_Quaternion q2, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_QuaternionLerp(q1, q2, amount)
|
|
|
|
result.x = q1.x + amount*(q2.x - q1.x);
|
|
|
|
result.y = q1.y + amount*(q2.y - q1.y);
|
|
|
|
result.z = q1.z + amount*(q2.z - q1.z);
|
|
|
|
result.w = q1.w + amount*(q2.w - q1.w);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_QuaternionNormalize(q);
|
|
|
|
RL_Quaternion q = result;
|
|
|
|
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
float ilength = 1.0f/length;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = q.x*ilength;
|
|
|
|
result.y = q.y*ilength;
|
|
|
|
result.z = q.z*ilength;
|
|
|
|
result.w = q.w*ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculates spherical linear interpolation between two quaternions
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionSlerp(RL_Quaternion q1, RL_Quaternion q2, float amount)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(EPSILON)
|
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
|
|
|
|
|
|
|
|
if (cosHalfTheta < 0)
|
|
|
|
{
|
|
|
|
q2.x = -q2.x; q2.y = -q2.y; q2.z = -q2.z; q2.w = -q2.w;
|
|
|
|
cosHalfTheta = -cosHalfTheta;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fabsf(cosHalfTheta) >= 1.0f) result = q1;
|
|
|
|
else if (cosHalfTheta > 0.95f) result = RL_QuaternionNlerp(q1, q2, amount);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float halfTheta = acosf(cosHalfTheta);
|
|
|
|
float sinHalfTheta = sqrtf(1.0f - cosHalfTheta*cosHalfTheta);
|
|
|
|
|
|
|
|
if (fabsf(sinHalfTheta) < EPSILON)
|
|
|
|
{
|
|
|
|
result.x = (q1.x*0.5f + q2.x*0.5f);
|
|
|
|
result.y = (q1.y*0.5f + q2.y*0.5f);
|
|
|
|
result.z = (q1.z*0.5f + q2.z*0.5f);
|
|
|
|
result.w = (q1.w*0.5f + q2.w*0.5f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta;
|
|
|
|
float ratioB = sinf(amount*halfTheta)/sinHalfTheta;
|
|
|
|
|
|
|
|
result.x = (q1.x*ratioA + q2.x*ratioB);
|
|
|
|
result.y = (q1.y*ratioA + q2.y*ratioB);
|
|
|
|
result.z = (q1.z*ratioA + q2.z*ratioB);
|
|
|
|
result.w = (q1.w*ratioA + q2.w*ratioB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate quaternion based on the rotation from one vector to another
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionFromVector3ToVector3(RL_Vector3 from, RL_Vector3 to)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float cos2Theta = (from.x*to.x + from.y*to.y + from.z*to.z); // RL_Vector3DotProduct(from, to)
|
|
|
|
RL_Vector3 cross = { from.y*to.z - from.z*to.y, from.z*to.x - from.x*to.z, from.x*to.y - from.y*to.x }; // RL_Vector3CrossProduct(from, to)
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = cross.x;
|
|
|
|
result.y = cross.y;
|
|
|
|
result.z = cross.z;
|
|
|
|
result.w = 1.0f + cos2Theta;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_QuaternionNormalize(q);
|
|
|
|
// NOTE: RL_Normalize to essentially nlerp the original and identity to 0.5
|
|
|
|
RL_Quaternion q = result;
|
|
|
|
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
float ilength = 1.0f/length;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = q.x*ilength;
|
|
|
|
result.y = q.y*ilength;
|
|
|
|
result.z = q.z*ilength;
|
|
|
|
result.w = q.w*ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a quaternion for a given rotation matrix
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionFromMatrix(RL_Matrix mat)
|
|
|
|
{
|
|
|
|
RL_Quaternion result = { 0 };
|
|
|
|
|
|
|
|
float fourWSquaredMinus1 = mat.m0 + mat.m5 + mat.m10;
|
|
|
|
float fourXSquaredMinus1 = mat.m0 - mat.m5 - mat.m10;
|
|
|
|
float fourYSquaredMinus1 = mat.m5 - mat.m0 - mat.m10;
|
|
|
|
float fourZSquaredMinus1 = mat.m10 - mat.m0 - mat.m5;
|
|
|
|
|
|
|
|
int biggestIndex = 0;
|
|
|
|
float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
|
|
|
|
if (fourXSquaredMinus1 > fourBiggestSquaredMinus1)
|
|
|
|
{
|
|
|
|
fourBiggestSquaredMinus1 = fourXSquaredMinus1;
|
|
|
|
biggestIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fourYSquaredMinus1 > fourBiggestSquaredMinus1)
|
|
|
|
{
|
|
|
|
fourBiggestSquaredMinus1 = fourYSquaredMinus1;
|
|
|
|
biggestIndex = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fourZSquaredMinus1 > fourBiggestSquaredMinus1)
|
|
|
|
{
|
|
|
|
fourBiggestSquaredMinus1 = fourZSquaredMinus1;
|
|
|
|
biggestIndex = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
float biggestVal = sqrtf(fourBiggestSquaredMinus1 + 1.0f)*0.5f;
|
|
|
|
float mult = 0.25f / biggestVal;
|
|
|
|
|
|
|
|
switch (biggestIndex)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
result.w = biggestVal;
|
|
|
|
result.x = (mat.m6 - mat.m9)*mult;
|
|
|
|
result.y = (mat.m8 - mat.m2)*mult;
|
|
|
|
result.z = (mat.m1 - mat.m4)*mult;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
result.x = biggestVal;
|
|
|
|
result.w = (mat.m6 - mat.m9)*mult;
|
|
|
|
result.y = (mat.m1 + mat.m4)*mult;
|
|
|
|
result.z = (mat.m8 + mat.m2)*mult;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
result.y = biggestVal;
|
|
|
|
result.w = (mat.m8 - mat.m2)*mult;
|
|
|
|
result.x = (mat.m1 + mat.m4)*mult;
|
|
|
|
result.z = (mat.m6 + mat.m9)*mult;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
result.z = biggestVal;
|
|
|
|
result.w = (mat.m1 - mat.m4)*mult;
|
|
|
|
result.x = (mat.m8 + mat.m2)*mult;
|
|
|
|
result.y = (mat.m6 + mat.m9)*mult;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a matrix for a given quaternion
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Matrix RL_QuaternionToMatrix(RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f }; // RL_MatrixIdentity()
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float a2 = q.x*q.x;
|
|
|
|
float b2 = q.y*q.y;
|
|
|
|
float c2 = q.z*q.z;
|
|
|
|
float ac = q.x*q.z;
|
|
|
|
float ab = q.x*q.y;
|
|
|
|
float bc = q.y*q.z;
|
|
|
|
float ad = q.w*q.x;
|
|
|
|
float bd = q.w*q.y;
|
|
|
|
float cd = q.w*q.z;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m0 = 1 - 2*(b2 + c2);
|
|
|
|
result.m1 = 2*(ab + cd);
|
|
|
|
result.m2 = 2*(ac - bd);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m4 = 2*(ab - cd);
|
|
|
|
result.m5 = 1 - 2*(a2 + c2);
|
|
|
|
result.m6 = 2*(bc + ad);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.m8 = 2*(ac + bd);
|
|
|
|
result.m9 = 2*(bc - ad);
|
|
|
|
result.m10 = 1 - 2*(a2 + b2);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get rotation quaternion for an angle and axis
|
|
|
|
// NOTE: Angle must be provided in radians
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionFromAxisAngle(RL_Vector3 axis, float angle)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float axisLength = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
if (axisLength != 0.0f)
|
|
|
|
{
|
|
|
|
angle *= 0.5f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float length = 0.0f;
|
|
|
|
float ilength = 0.0f;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Vector3Normalize(axis)
|
|
|
|
RL_Vector3 v = axis;
|
|
|
|
length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
axis.x *= ilength;
|
|
|
|
axis.y *= ilength;
|
|
|
|
axis.z *= ilength;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float sinres = sinf(angle);
|
|
|
|
float cosres = cosf(angle);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = axis.x*sinres;
|
|
|
|
result.y = axis.y*sinres;
|
|
|
|
result.z = axis.z*sinres;
|
|
|
|
result.w = cosres;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_QuaternionNormalize(q);
|
|
|
|
RL_Quaternion q = result;
|
|
|
|
length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
ilength = 1.0f/length;
|
|
|
|
result.x = q.x*ilength;
|
|
|
|
result.y = q.y*ilength;
|
|
|
|
result.z = q.z*ilength;
|
|
|
|
result.w = q.w*ilength;
|
|
|
|
}
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the rotation angle and axis for a given quaternion
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI void RL_QuaternionToAxisAngle(RL_Quaternion q, RL_Vector3 *outAxis, float *outAngle)
|
|
|
|
{
|
|
|
|
if (fabsf(q.w) > 1.0f)
|
|
|
|
{
|
|
|
|
// RL_QuaternionNormalize(q);
|
|
|
|
float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
|
|
|
if (length == 0.0f) length = 1.0f;
|
|
|
|
float ilength = 1.0f/length;
|
|
|
|
|
|
|
|
q.x = q.x*ilength;
|
|
|
|
q.y = q.y*ilength;
|
|
|
|
q.z = q.z*ilength;
|
|
|
|
q.w = q.w*ilength;
|
|
|
|
}
|
|
|
|
|
|
|
|
RL_Vector3 resAxis = { 0.0f, 0.0f, 0.0f };
|
|
|
|
float resAngle = 2.0f*acosf(q.w);
|
|
|
|
float den = sqrtf(1.0f - q.w*q.w);
|
|
|
|
|
|
|
|
if (den > EPSILON)
|
|
|
|
{
|
|
|
|
resAxis.x = q.x/den;
|
|
|
|
resAxis.y = q.y/den;
|
|
|
|
resAxis.z = q.z/den;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This occurs when the angle is zero.
|
|
|
|
// Not a problem: just set an arbitrary normalized axis.
|
|
|
|
resAxis.x = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outAxis = resAxis;
|
|
|
|
*outAngle = resAngle;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the quaternion equivalent to Euler angles
|
|
|
|
// NOTE: Rotation order is ZYX
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI RL_Quaternion RL_QuaternionFromEuler(float pitch, float yaw, float roll)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
float x0 = cosf(pitch*0.5f);
|
|
|
|
float x1 = sinf(pitch*0.5f);
|
|
|
|
float y0 = cosf(yaw*0.5f);
|
|
|
|
float y1 = sinf(yaw*0.5f);
|
|
|
|
float z0 = cosf(roll*0.5f);
|
|
|
|
float z1 = sinf(roll*0.5f);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = x1*y0*z0 - x0*y1*z1;
|
|
|
|
result.y = x0*y1*z0 + x1*y0*z1;
|
|
|
|
result.z = x0*y0*z1 - x1*y1*z0;
|
|
|
|
result.w = x0*y0*z0 + x1*y1*z1;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
|
2023-11-17 16:25:54 -08:00
|
|
|
// NOTE: Angles are returned in a RL_Vector3 struct in radians
|
|
|
|
RMAPI RL_Vector3 RL_QuaternionToEuler(RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Vector3 result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Roll (x-axis rotation)
|
|
|
|
float x0 = 2.0f*(q.w*q.x + q.y*q.z);
|
|
|
|
float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y);
|
|
|
|
result.x = atan2f(x0, x1);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Pitch (y-axis rotation)
|
|
|
|
float y0 = 2.0f*(q.w*q.y - q.z*q.x);
|
|
|
|
y0 = y0 > 1.0f ? 1.0f : y0;
|
|
|
|
y0 = y0 < -1.0f ? -1.0f : y0;
|
|
|
|
result.y = asinf(y0);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// Yaw (z-axis rotation)
|
|
|
|
float z0 = 2.0f*(q.w*q.z + q.x*q.y);
|
|
|
|
float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z);
|
|
|
|
result.z = atan2f(z0, z1);
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
// RL_Transform a quaternion given a transformation matrix
|
|
|
|
RMAPI RL_Quaternion RL_QuaternionTransform(RL_Quaternion q, RL_Matrix mat)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
RL_Quaternion result = { 0 };
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w;
|
|
|
|
result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w;
|
|
|
|
result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w;
|
|
|
|
result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w;
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether two given quaternions are almost equal
|
2023-11-17 16:25:54 -08:00
|
|
|
RMAPI int RL_QuaternionEquals(RL_Quaternion p, RL_Quaternion q)
|
2023-10-24 03:00:28 -07:00
|
|
|
{
|
2023-11-17 16:25:54 -08:00
|
|
|
#if !defined(EPSILON)
|
|
|
|
#define EPSILON 0.000001f
|
2023-10-24 03:00:28 -07:00
|
|
|
#endif
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
int result = (((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
|
|
|
|
((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
|
|
|
|
((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
|
|
|
|
((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))))) ||
|
|
|
|
(((fabsf(p.x + q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
|
|
|
|
((fabsf(p.y + q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
|
|
|
|
((fabsf(p.z + q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
|
|
|
|
((fabsf(p.w + q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w))))));
|
2023-10-24 03:00:28 -07:00
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
return result;
|
2023-10-24 03:00:28 -07:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:25:54 -08:00
|
|
|
#endif // RAYMATH_H
|