Update C sources
This commit is contained in:
parent
9d258bad65
commit
c1525b67af
68 changed files with 34056 additions and 25957 deletions
181
raylib/raymath.h
181
raylib/raymath.h
|
@ -20,7 +20,7 @@
|
|||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2015-2020 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2015-2021 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.
|
||||
|
@ -75,7 +75,7 @@
|
|||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#define PI 3.14159265358979323846f
|
||||
#endif
|
||||
|
||||
#ifndef DEG2RAD
|
||||
|
@ -114,13 +114,16 @@
|
|||
float z;
|
||||
} Vector3;
|
||||
|
||||
// Quaternion type
|
||||
typedef struct Quaternion {
|
||||
// Vector4 type
|
||||
typedef struct Vector4 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float w;
|
||||
} Quaternion;
|
||||
} Vector4;
|
||||
|
||||
// Quaternion type
|
||||
typedef Vector4 Quaternion;
|
||||
|
||||
// Matrix type (OpenGL style 4x4 - right handed, column major)
|
||||
typedef struct Matrix {
|
||||
|
@ -157,13 +160,13 @@ RMDEF float Lerp(float start, float end, float amount)
|
|||
// Normalize input value within input range
|
||||
RMDEF float Normalize(float value, float start, float end)
|
||||
{
|
||||
return (value - start) / (end - start);
|
||||
return (value - start)/(end - start);
|
||||
}
|
||||
|
||||
// Remap input value within input range to output range
|
||||
RMDEF float Remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd)
|
||||
RMDEF float Remap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd)
|
||||
{
|
||||
return (value - inputStart) / (inputEnd - inputStart) * (outputEnd - outputStart) + outputStart;
|
||||
return (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
@ -294,11 +297,24 @@ RMDEF Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Calculate reflected vector to normal
|
||||
RMDEF Vector2 Vector2Reflect(Vector2 v, Vector2 normal)
|
||||
{
|
||||
Vector2 result = { 0 };
|
||||
|
||||
float dotProduct = Vector2DotProduct(v, normal);
|
||||
|
||||
result.x = v.x - (2.0f*normal.x)*dotProduct;
|
||||
result.y = v.y - (2.0f*normal.y)*dotProduct;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Rotate Vector by float in Degrees.
|
||||
RMDEF Vector2 Vector2Rotate(Vector2 v, float degs)
|
||||
{
|
||||
float rads = degs*DEG2RAD;
|
||||
Vector2 result = {v.x * cosf(rads) - v.y * sinf(rads) , v.x * sinf(rads) + v.y * cosf(rads) };
|
||||
Vector2 result = {v.x*cosf(rads) - v.y*sinf(rads) , v.x*sinf(rads) + v.y*cosf(rads) };
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -309,14 +325,14 @@ RMDEF Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance)
|
|||
float dx = target.x - v.x;
|
||||
float dy = target.y - v.y;
|
||||
float value = (dx*dx) + (dy*dy);
|
||||
|
||||
|
||||
if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) result = target;
|
||||
|
||||
|
||||
float dist = sqrtf(value);
|
||||
|
||||
|
||||
result.x = v.x + dx/dist*maxDistance;
|
||||
result.y = v.y + dy/dist*maxDistance;
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -840,14 +856,14 @@ RMDEF Matrix MatrixRotate(Vector3 axis, float angle)
|
|||
|
||||
float x = axis.x, y = axis.y, z = axis.z;
|
||||
|
||||
float length = sqrtf(x*x + y*y + z*z);
|
||||
float lengthSquared = x*x + y*y + z*z;
|
||||
|
||||
if ((length != 1.0f) && (length != 0.0f))
|
||||
if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
|
||||
{
|
||||
length = 1.0f/length;
|
||||
x *= length;
|
||||
y *= length;
|
||||
z *= length;
|
||||
float inverseLength = 1.0f/sqrtf(lengthSquared);
|
||||
x *= inverseLength;
|
||||
y *= inverseLength;
|
||||
z *= inverseLength;
|
||||
}
|
||||
|
||||
float sinres = sinf(angle);
|
||||
|
@ -938,30 +954,53 @@ RMDEF Matrix MatrixRotateXYZ(Vector3 ang)
|
|||
float cosx = cosf(-ang.x);
|
||||
float sinx = sinf(-ang.x);
|
||||
|
||||
result.m0 = cosz * cosy;
|
||||
result.m4 = (cosz * siny * sinx) - (sinz * cosx);
|
||||
result.m8 = (cosz * siny * cosx) + (sinz * sinx);
|
||||
result.m0 = cosz*cosy;
|
||||
result.m4 = (cosz*siny*sinx) - (sinz*cosx);
|
||||
result.m8 = (cosz*siny*cosx) + (sinz*sinx);
|
||||
|
||||
result.m1 = sinz * cosy;
|
||||
result.m5 = (sinz * siny * sinx) + (cosz * cosx);
|
||||
result.m9 = (sinz * siny * cosx) - (cosz * sinx);
|
||||
result.m1 = sinz*cosy;
|
||||
result.m5 = (sinz*siny*sinx) + (cosz*cosx);
|
||||
result.m9 = (sinz*siny*cosx) - (cosz*sinx);
|
||||
|
||||
result.m2 = -siny;
|
||||
result.m6 = cosy * sinx;
|
||||
result.m10= cosy * cosx;
|
||||
result.m6 = cosy*sinx;
|
||||
result.m10= cosy*cosx;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns zyx-rotation matrix (angles in radians)
|
||||
// TODO: This solution is suboptimal, it should be possible to create this matrix in one go
|
||||
// instead of using a 3 matrix multiplication
|
||||
RMDEF Matrix MatrixRotateZYX(Vector3 ang)
|
||||
{
|
||||
Matrix result = MatrixRotateZ(ang.z);
|
||||
result = MatrixMultiply(result, MatrixRotateY(ang.y));
|
||||
result = MatrixMultiply(result, MatrixRotateX(ang.x));
|
||||
|
||||
Matrix result = { 0 };
|
||||
|
||||
float cz = cosf(ang.z);
|
||||
float sz = sinf(ang.z);
|
||||
float cy = cosf(ang.y);
|
||||
float sy = sinf(ang.y);
|
||||
float cx = cosf(ang.x);
|
||||
float sx = sinf(ang.x);
|
||||
|
||||
result.m0 = cz*cy;
|
||||
result.m1 = cz*sy*sx - cx*sz;
|
||||
result.m2 = sz*sx + cz*cx*sy;
|
||||
result.m3 = 0;
|
||||
|
||||
result.m4 = cy*sz;
|
||||
result.m5 = cz*cx + sz*sy*sx;
|
||||
result.m6 = cx*sz*sy - cz*sx;
|
||||
result.m7 = 0;
|
||||
|
||||
result.m8 = -sy;
|
||||
result.m9 = cy*sx;
|
||||
result.m10 = cy*cx;
|
||||
result.m11 = 0;
|
||||
|
||||
result.m12 = 0;
|
||||
result.m13 = 0;
|
||||
result.m14 = 0;
|
||||
result.m15 = 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1058,27 +1097,24 @@ RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
|
|||
Vector3 x = Vector3CrossProduct(up, z);
|
||||
x = Vector3Normalize(x);
|
||||
Vector3 y = Vector3CrossProduct(z, x);
|
||||
y = Vector3Normalize(y);
|
||||
|
||||
result.m0 = x.x;
|
||||
result.m1 = x.y;
|
||||
result.m2 = x.z;
|
||||
result.m1 = y.x;
|
||||
result.m2 = z.x;
|
||||
result.m3 = 0.0f;
|
||||
result.m4 = y.x;
|
||||
result.m4 = x.y;
|
||||
result.m5 = y.y;
|
||||
result.m6 = y.z;
|
||||
result.m6 = z.y;
|
||||
result.m7 = 0.0f;
|
||||
result.m8 = z.x;
|
||||
result.m9 = z.y;
|
||||
result.m8 = x.z;
|
||||
result.m9 = y.z;
|
||||
result.m10 = z.z;
|
||||
result.m11 = 0.0f;
|
||||
result.m12 = eye.x;
|
||||
result.m13 = eye.y;
|
||||
result.m14 = eye.z;
|
||||
result.m12 = -Vector3DotProduct(x, eye);
|
||||
result.m13 = -Vector3DotProduct(y, eye);
|
||||
result.m14 = -Vector3DotProduct(z, eye);
|
||||
result.m15 = 1.0f;
|
||||
|
||||
result = MatrixInvert(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1149,7 +1185,7 @@ RMDEF Quaternion QuaternionIdentity(void)
|
|||
// Computes the length of a quaternion
|
||||
RMDEF float QuaternionLength(Quaternion q)
|
||||
{
|
||||
float result = (float)sqrt(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
||||
float result = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1214,10 +1250,10 @@ RMDEF Quaternion QuaternionScale(Quaternion q, float mul)
|
|||
|
||||
float qax = q.x, qay = q.y, qaz = q.z, qaw = q.w;
|
||||
|
||||
result.x = qax * mul + qaw * mul + qay * mul - qaz * mul;
|
||||
result.y = qay * mul + qaw * mul + qaz * mul - qax * mul;
|
||||
result.z = qaz * mul + qaw * mul + qax * mul - qay * mul;
|
||||
result.w = qaw * mul - qax * mul - qay * mul - qaz * mul;
|
||||
result.x = qax*mul + qaw*mul + qay*mul - qaz*mul;
|
||||
result.y = qay*mul + qaw*mul + qaz*mul - qax*mul;
|
||||
result.z = qaz*mul + qaw*mul + qax*mul - qay*mul;
|
||||
result.w = qaw*mul - qax*mul - qay*mul - qaz*mul;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -1225,7 +1261,7 @@ RMDEF Quaternion QuaternionScale(Quaternion q, float mul)
|
|||
// Divide two quaternions
|
||||
RMDEF Quaternion QuaternionDivide(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
Quaternion result = {q1.x / q2.x, q1.y / q2.y, q1.z / q2.z, q1.w / q2.w};
|
||||
Quaternion result = { q1.x/q2.x, q1.y/q2.y, q1.z/q2.z, q1.w/q2.w };
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1258,6 +1294,12 @@ RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount)
|
|||
|
||||
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 (fabs(cosHalfTheta) >= 1.0f) result = q1;
|
||||
else if (cosHalfTheta > 0.95f) result = QuaternionNlerp(q1, q2, amount);
|
||||
else
|
||||
|
@ -1312,17 +1354,17 @@ RMDEF Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
|
|||
// Returns a quaternion for a given rotation matrix
|
||||
RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
|
||||
{
|
||||
Quaternion result = { 0.0f };
|
||||
|
||||
Quaternion result = { 0 };
|
||||
|
||||
if ((mat.m0 > mat.m5) && (mat.m0 > mat.m10))
|
||||
{
|
||||
float s = sqrtf(1.0f + mat.m0 - mat.m5 - mat.m10)*2;
|
||||
|
||||
|
||||
result.x = 0.25f*s;
|
||||
result.y = (mat.m4 + mat.m1)/s;
|
||||
result.z = (mat.m2 + mat.m8)/s;
|
||||
result.w = (mat.m9 - mat.m6)/s;
|
||||
}
|
||||
}
|
||||
else if (mat.m5 > mat.m10)
|
||||
{
|
||||
float s = sqrtf(1.0f + mat.m5 - mat.m0 - mat.m10)*2;
|
||||
|
@ -1330,7 +1372,7 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
|
|||
result.y = 0.25f*s;
|
||||
result.z = (mat.m9 + mat.m6)/s;
|
||||
result.w = (mat.m2 - mat.m8)/s;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float s = sqrtf(1.0f + mat.m10 - mat.m0 - mat.m5)*2;
|
||||
|
@ -1339,7 +1381,7 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
|
|||
result.z = 0.25f*s;
|
||||
result.w = (mat.m4 - mat.m1)/s;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1347,20 +1389,20 @@ RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
|
|||
RMDEF Matrix QuaternionToMatrix(Quaternion q)
|
||||
{
|
||||
Matrix result = MatrixIdentity();
|
||||
|
||||
|
||||
float a2 = 2*(q.x*q.x), b2=2*(q.y*q.y), c2=2*(q.z*q.z); //, d2=2*(q.w*q.w);
|
||||
|
||||
|
||||
float ab = 2*(q.x*q.y), ac=2*(q.x*q.z), bc=2*(q.y*q.z);
|
||||
float ad = 2*(q.x*q.w), bd=2*(q.y*q.w), cd=2*(q.z*q.w);
|
||||
|
||||
result.m0 = 1 - b2 - c2;
|
||||
result.m1 = ab - cd;
|
||||
result.m2 = ac + bd;
|
||||
|
||||
|
||||
result.m4 = ab + cd;
|
||||
result.m5 = 1 - a2 - c2;
|
||||
result.m6 = bc - ad;
|
||||
|
||||
|
||||
result.m8 = ac - bd;
|
||||
result.m9 = bc + ad;
|
||||
result.m10 = 1 - a2 - b2;
|
||||
|
@ -1419,17 +1461,18 @@ RMDEF void QuaternionToAxisAngle(Quaternion q, Vector3 *outAxis, float *outAngle
|
|||
*outAngle = resAngle;
|
||||
}
|
||||
|
||||
// Returns he quaternion equivalent to Euler angles
|
||||
RMDEF Quaternion QuaternionFromEuler(float roll, float pitch, float yaw)
|
||||
// Returns the quaternion equivalent to Euler angles
|
||||
// NOTE: Rotation order is ZYX
|
||||
RMDEF Quaternion QuaternionFromEuler(float pitch, float yaw, float roll)
|
||||
{
|
||||
Quaternion q = { 0 };
|
||||
|
||||
float x0 = cosf(roll*0.5f);
|
||||
float x1 = sinf(roll*0.5f);
|
||||
float y0 = cosf(pitch*0.5f);
|
||||
float y1 = sinf(pitch*0.5f);
|
||||
float z0 = cosf(yaw*0.5f);
|
||||
float z1 = sinf(yaw*0.5f);
|
||||
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);
|
||||
|
||||
q.x = x1*y0*z0 - x0*y1*z1;
|
||||
q.y = x0*y1*z0 + x1*y0*z1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue