From c0afdcb057e12ea6f03f12ca0c879d503118a545 Mon Sep 17 00:00:00 2001 From: em-eight Date: Sat, 21 Mar 2026 09:43:09 -0500 Subject: [PATCH] Refactor some egg math symbols --- config/RMCP01/symbols.txt | 28 +++++++++---------- lib/egg/math/eggMath.hpp | 21 +++------------ lib/egg/math/eggMatrix.hpp | 28 +++++++++---------- lib/egg/math/eggQuat.cpp | 54 +++++++++++++++++++++++++++++++++---- lib/egg/math/eggQuat.hpp | 25 +++++++++++++---- src/field/CollisionInfo.hpp | 4 ++- 6 files changed, 102 insertions(+), 58 deletions(-) diff --git a/config/RMCP01/symbols.txt b/config/RMCP01/symbols.txt index d5fc11a..6886910 100644 --- a/config/RMCP01/symbols.txt +++ b/config/RMCP01/symbols.txt @@ -9212,14 +9212,14 @@ EGG_List_ct = .text:0x8022F760; // type:function size:0x1C scope:global align:4 EGG_List_append = .text:0x8022F77C; // type:function size:0x70 scope:global align:4 EGG_ListIterator_ct = .text:0x8022F7EC; // type:function size:0x18 scope:global align:4 EGG_ListIterator_get = .text:0x8022F804; // type:function size:0x8 scope:global align:4 -sqrt__Q23EGG5MathfFf = .text:0x8022F80C; // type:function size:0x50 scope:global align:4 -frsqrt__Q23EGG5MathfFf = .text:0x8022F85C; // type:function size:0x4 scope:global align:4 -sin__Q23EGG5MathfFf = .text:0x8022F860; // type:function size:0xC scope:global align:4 -cos__Q23EGG5MathfFf = .text:0x8022F86C; // type:function size:0xC scope:global align:4 -tan__Q23EGG5MathfFf = .text:0x8022F878; // type:function size:0x24 scope:global align:4 -asin__Q23EGG5MathfFf = .text:0x8022F89C; // type:function size:0x24 scope:global align:4 -acos__Q23EGG5MathfFf = .text:0x8022F8C0; // type:function size:0x24 scope:global align:4 -atan2_Q23EGG5MathfFff = .text:0x8022F8E4; // type:function size:0x28 scope:global align:4 +sqrt__Q23EGG7MathFf = .text:0x8022F80C; // type:function size:0x50 scope:global align:4 +frsqrt__Q23EGG7MathFf = .text:0x8022F85C; // type:function size:0x4 scope:global align:4 +sin__Q23EGG7MathFf = .text:0x8022F860; // type:function size:0xC scope:global align:4 +cos__Q23EGG7MathFf = .text:0x8022F86C; // type:function size:0xC scope:global align:4 +tan__Q23EGG7MathFf = .text:0x8022F878; // type:function size:0x24 scope:global align:4 +asin__Q23EGG7MathFf = .text:0x8022F89C; // type:function size:0x24 scope:global align:4 +acos__Q23EGG7MathFf = .text:0x8022F8C0; // type:function size:0x24 scope:global align:4 +atan2__Q23EGG7MathFf = .text:0x8022F8E4; // type:function size:0x28 scope:global align:4 inverseToC__Q23EGG9Matrix34fCFRQ23EGG9Matrix34f = .text:0x8022F90C; // type:function size:0x1B0 scope:global align:4 inverseTo__Q23EGG9Matrix34fCFRQ23EGG9Matrix34f = .text:0x8022FABC; // type:function size:0x4 scope:global align:4 inverseTransposeTo__Q23EGG9Matrix34fCFRQ23EGG9Matrix34f = .text:0x8022FAC0; // type:function size:0x4 scope:global align:4 @@ -9418,14 +9418,14 @@ set__Q23EGG5QuatfFffff = .text:0x80239DFC; // type:function size:0x14 scope:glob setRPY__Q23EGG5QuatfFRCQ23EGG8Vector3f = .text:0x80239E10; // type:function size:0x148 scope:global align:4 setRPY__Q23EGG5QuatfFfff = .text:0x80239F58; // type:function size:0x148 scope:global align:4 setAxisRotation__Q23EGG5QuatfFRCQ23EGG8Vector3ff = .text:0x8023A0A0; // type:function size:0x98 scope:global align:4 -squareNorm__Q23EGG5QuatfFv = .text:0x8023A138; // type:function size:0x30 scope:global align:4 +squareNorm__Q23EGG5QuatfCFv = .text:0x8023A138; // type:function size:0x30 scope:global align:4 normalise__Q23EGG5QuatfFv = .text:0x8023A168; // type:function size:0xA8 scope:global align:4 -inverse__Q23EGG5QuatfFv = .text:0x8023A210; // type:function size:0xC0 scope:global align:4 -rotateVector__Q23EGG5QuatfFRCQ23EGG8Vector3fRQ23EGG8Vector3f = .text:0x8023A2D0; // type:function size:0x134 scope:global align:4 -rotateVectorInv__Q23EGG5QuatfFRCQ23EGG8Vector3fRQ23EGG8Vector3f = .text:0x8023A404; // type:function size:0x13C scope:global align:4 -__ml__3EGGFRCQ23EGG5QuatfRCQ23EGG8Vector3f = .text:0x8023A540; // type:function size:0x84 scope:global align:4 +inverse__Q23EGG5QuatfCFRQ23EGG5Quatf = .text:0x8023A210; // type:function size:0xC0 scope:global align:4 +rotateVector__Q23EGG5QuatfCFRCQ23EGG8Vector3fRQ23EGG8Vector3f = .text:0x8023A2D0; // type:function size:0x134 scope:global align:4 +rotateVectorInv__Q23EGG5QuatfCFRCQ23EGG8Vector3fRQ23EGG8Vector3f = .text:0x8023A404; // type:function size:0x13C scope:global align:4 +vecMul__Q23EGG5QuatfCFRCQ23EGG5QuatfRCQ23EGG8Vector3fRQ23EGG5Quatf = .text:0x8023A540; // type:function size:0x84 scope:global align:4 slerpTo__Q23EGG5QuatfCFRCQ23EGG5QuatffRQ23EGG5Quatf = .text:0x8023A5C4; // type:function size:0x1C4 scope:global align:4 -makeVectorRotation__Q23EGG5QuatfFRQ23EGG8Vector3fRQ23EGG8Vector3f = .text:0x8023A788; // type:function size:0xFC scope:global align:4 +makeVectorRotation__Q23EGG5QuatfFRCQ23EGG8Vector3fRCQ23EGG8Vector3f = .text:0x8023A788; // type:function size:0xFC scope:global align:4 SaveBanner_create = .text:0x8023A884; // type:function size:0x68 scope:global align:4 SaveBanner_initBanner = .text:0x8023A8EC; // type:function size:0x118 scope:global align:4 SaveBanner_existBannerFile = .text:0x8023AA04; // type:function size:0x74 scope:global align:4 diff --git a/lib/egg/math/eggMath.hpp b/lib/egg/math/eggMath.hpp index 2fe0cad..7528c4c 100644 --- a/lib/egg/math/eggMath.hpp +++ b/lib/egg/math/eggMath.hpp @@ -14,11 +14,13 @@ namespace EGG { -#if 0 template struct Math { static T sqrt(T); static T frsqrt(T); + static T inv(T t) { return 1 / t; } + static T abs(T t) { return t > (T)0 ? t : -t;} + static T sin(T radians); static T cos(T radians); static T tan(T radians); @@ -28,22 +30,5 @@ template struct Math { }; typedef Math Mathf; -#endif - -namespace Mathf { -float sqrt(float); -float frsqrt(float); - -inline f32 abs(f32 t) { return t > (f32)0 ? t : -t;} - -float sin(float); -float cos(float); -float tan(float); -float asin(float); -float acos(float); -float atan2(float, float); - -inline bool isNonNegative(f32 x) {return x > -FLT_MIN;} -} // namespace Mathf } // namespace EGG diff --git a/lib/egg/math/eggMatrix.hpp b/lib/egg/math/eggMatrix.hpp index 3803c61..5cda523 100644 --- a/lib/egg/math/eggMatrix.hpp +++ b/lib/egg/math/eggMatrix.hpp @@ -37,25 +37,23 @@ namespace EGG void makeS(const Vector3f&); void makeR(const Vector3f&); void setAxisRotation(const Vector3f &, f32); - EGG::Vector3f multVector(const EGG::Vector3f&) const; - // Defined inline and emitted in KartWheelPhysics - EGG::Vector3f multVector33(const EGG::Vector3f&) const NEVER_INLINE; + EGG::Vector3f multVector(const EGG::Vector3f&) const; + // Defined inline and emitted in KartWheelPhysics + EGG::Vector3f multVector33(const EGG::Vector3f&) const NEVER_INLINE; void loadPosMtx(u32); f32& operator()(int i, int j) { return mtx[i][j]; } - friend Vector3f operator*(const Matrix34f& M, const Vector3f& v) { - Vector3f out; - const float vecX = v.x; - const float vecY = v.y; - const float vecZ = v.z; - out.x = (vecX * M.mtx[0][0]) + (vecY * M.mtx[0][1]) + (vecZ * M.mtx[0][2]); - out.y = (vecX * M.mtx[1][0]) + (vecY * M.mtx[1][1]) + (vecZ * M.mtx[1][2]); - out.z = (vecX * M.mtx[2][0]) + (vecY * M.mtx[2][1]) + (vecZ * M.mtx[2][2]); - return out; - } - - //f32 tbl[3][4]; + friend Vector3f operator*(const Matrix34f& M, const Vector3f& v) { + Vector3f out; + const float vecX = v.x; + const float vecY = v.y; + const float vecZ = v.z; + out.x = (vecX * M.mtx[0][0]) + (vecY * M.mtx[0][1]) + (vecZ * M.mtx[0][2]); + out.y = (vecX * M.mtx[1][0]) + (vecY * M.mtx[1][1]) + (vecZ * M.mtx[1][2]); + out.z = (vecX * M.mtx[2][0]) + (vecY * M.mtx[2][1]) + (vecZ * M.mtx[2][2]); + return out; + } static Matrix34f ident; }; diff --git a/lib/egg/math/eggQuat.cpp b/lib/egg/math/eggQuat.cpp index a8059a6..ae6491b 100644 --- a/lib/egg/math/eggQuat.cpp +++ b/lib/egg/math/eggQuat.cpp @@ -3,16 +3,13 @@ * @brief TODO */ +#include #include #define VEC3_DTOR_INLINE #include namespace EGG { -using EGG::Mathf::cos; -using EGG::Mathf::sin; -using EGG::Mathf::sqrt; - #undef FLT_EPSILON #define FLT_EPSILON 1.192092896e-07F @@ -61,11 +58,54 @@ void Quatf::setRPY(float x, float y, float z) { this->setRPY(vec); } +void Quatf::setAxisRotation(const Vector3f &axis, f32 rot) { + const f32 half_angle = rot * 0.5f; + const f32 cos = Math::cos(half_angle); + const f32 sin = Math::sin(half_angle); + set(cos, sin * axis.x, sin * axis.y, sin * axis.z); +} + -float Quatf::squareNorm() { +float Quatf::squareNorm() const { return axisSquareNorm() + w*w; } +void Quatf::normalise() { + f32 sqNorm = squareNorm(); + if (sqNorm > FLT_EPSILON) { + f32 mag = Math::sqrt(sqNorm); + if (mag > 0.0f) { + multScalar(Math::inv(mag)); + } + } +} + +void Quatf::inverse(Quatf& out) const { + f32 sqNorm = squareNorm(); + if (sqNorm > 0.0f) { + f32 sqNormInv = 1.0f / sqNorm; + out.x = sqNormInv * -x; + out.y = sqNormInv * -y; + out.z = sqNormInv * -z; + out.w = sqNormInv * w; + } else { + out.w = w; + out.x = -x; + out.y = -y; + out.z = -z; + } +} + +void Quatf::rotateVector(const Vector3f& vec, Vector3f& ret) const { + Quatf conj = conjugate(); + Quatf res; + vecMul(*this, vec, res); + + ret.x = (res.y * conj.z + (res.x * conj.w + res.w * conj.x)) - res.z * conj.y; + ret.y = (res.z * conj.x + (res.y * conj.w + res.w * conj.y)) - res.x * conj.z; + ret.z = (res.x * conj.y + (res.z * conj.w + res.w * conj.z)) - res.y * conj.x; +} + void Quatf::slerpTo(const EGG::Quatf &q1, f32 t, EGG::Quatf &dst) const { f32 dot = x * q1.x + y * q1.y + z * q1.z + w * q1.w; @@ -105,4 +145,8 @@ void Quatf::slerpTo(const EGG::Quatf &q1, f32 t, EGG::Quatf &dst) const { dst.w = s * w + recip * q1.w; } +void Quatf::makeVectorRotation(const Vector3f& v0, const Vector3f& v1) { + +} + } // namespace EGG diff --git a/lib/egg/math/eggQuat.hpp b/lib/egg/math/eggQuat.hpp index d889f3e..fde7526 100644 --- a/lib/egg/math/eggQuat.hpp +++ b/lib/egg/math/eggQuat.hpp @@ -13,23 +13,38 @@ namespace EGG { class Quatf { public: + Quatf() {}; + Quatf(float w, float x, float y, float z) : w(w), x(x), y(y), z(z) {} void set(float w, float x, float y, float z); void setRPY(const Vector3f& euler); void setRPY(float r, float p, float y); void setAxisRotation(const Vector3f& axis, float angle); float axisSquareNorm() const { return x*x + y*y + z*z; }; // maybe Vector3f subobj - float squareNorm(); + float squareNorm() const; void normalise(); - Quatf inverse(); - void rotateVector(const Vector3f& v, Vector3f& out); - void rotateVectorInv(const Vector3f& v, Vector3f& out); + void inverse(Quatf& out) const; + void rotateVector(const Vector3f& v, Vector3f& out) const; + void rotateVectorInv(const Vector3f& v, Vector3f& out) const; + // yep, that's what the declaration seems to be, completely bizarre + void vecMul(const Quatf& q, const Vector3f& v, Quatf& out) const; void slerpTo(const Quatf& r4, float, Quatf& r5) const; - void makeVectorRotation(Vector3f& v0, Vector3f& v1); + void makeVectorRotation(const Vector3f& v0, const Vector3f& v1); + + Quatf conjugate() const { + return Quatf(w, -x, -y, -z); + } inline void setIdentity() { set(1.0f, 0.0f, 0.0f, 0.0f); } + void multScalar(f32 s) { + w *= s; + x *= s; + y *= s; + z *= s; + } + inline Quatf& operator=(const Quatf& rhs) { x = rhs.x; y = rhs.y; diff --git a/src/field/CollisionInfo.hpp b/src/field/CollisionInfo.hpp index c48988e..078dd20 100644 --- a/src/field/CollisionInfo.hpp +++ b/src/field/CollisionInfo.hpp @@ -4,6 +4,8 @@ #include "egg/math/eggMatrix.hpp" #include "egg/math/eggMath.hpp" +inline bool isNonNegative(f32 x) {return x > -FLT_MIN;} + namespace Field { struct SoftWallColInfo { EGG::Vector3f bboxLow; @@ -96,7 +98,7 @@ struct ColInfo : public ColInfoPartial { if ((kclAttributeTypeBit & KCL_TYPE_FLOOR) != 0) { this->updateFloor(now_dist, fnrm); } else if ((kclAttributeTypeBit & KCL_TYPE_WALL) != 0) { - if (EGG::Mathf::isNonNegative(this->wallDist)) { + if (isNonNegative(this->wallDist)) { f32 perpendicularity = 1.0f - VEC3Dot(&this->wallNrm, &fnrm); bool updatePerp; if (perpendicularity > this->colPerpendicularity) {