From e0bd54742df6b1108131006e13fda28ca81baa18 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 6 May 2024 11:30:33 +0900 Subject: [PATCH] Add Scale Mat4 creation function Also added some relevant UnitTests. --- src/sc_sacd.cpp | 5 +++++ src/sc_sacd.h | 2 ++ src/test.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/sc_sacd.cpp b/src/sc_sacd.cpp index a67e620..f6f8e94 100644 --- a/src/sc_sacd.cpp +++ b/src/sc_sacd.cpp @@ -546,6 +546,11 @@ SC_SACD_Mat4 SC_SACD_Translate_Mat4(float x, float y, float z) { 0.0F, 0.0F, 1.0F, z, 0.0F, 0.0F, 0.0F, 1.0F}; } +SC_SACD_Mat4 SC_SACD_Scale_Mat4(float x, float y, float z) { + return SC_SACD_Mat4{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}; +} + SC_SACD_Vec3 SC_SACD_Closest_Point_Dir_Normalized(const SC_SACD_Vec3 *pos, const SC_SACD_Vec3 *dir, const SC_SACD_Vec3 *point) { diff --git a/src/sc_sacd.h b/src/sc_sacd.h index 9cb0e21..e0bddb5 100644 --- a/src/sc_sacd.h +++ b/src/sc_sacd.h @@ -119,6 +119,8 @@ SC_SACD_EXPORT SC_SACD_Mat4 SC_SACD_Rotation_Mat4_ZAxis(float z_radians); SC_SACD_EXPORT SC_SACD_Mat4 SC_SACD_Translate_Mat4(float x, float y, float z); +SC_SACD_EXPORT SC_SACD_Mat4 SC_SACD_Scale_Mat4(float x, float y, float z); + /// This variant of Closest_Point expects "dir" to be a unit vector. SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Closest_Point_Dir_Normalized( const SC_SACD_Vec3 *pos, const SC_SACD_Vec3 *dir, diff --git a/src/test.cpp b/src/test.cpp index 77acd62..a759639 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -591,6 +591,48 @@ int main() { CHECK_FALSE(SC_SACD_Generic_Box_Collision(&a, &b)); } + // Box with Scale Mat4. + { + SC_SACD_Generic_Box a = SC_SACD_Generic_Box_Default(); + SC_SACD_Generic_Box b = SC_SACD_Generic_Box_Default(); + + a.x = 1.1F; + b.x = -1.1F; + CHECK_FALSE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(2.0F, 1.0F, 1.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(-2.0F, 1.0F, 1.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.x = 0.0F; + b.x = 0.0F; + a.y = 1.1F; + b.y = -1.1F; + a.transform = SC_SACD_Mat4_Identity(); + CHECK_FALSE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(1.0F, 2.0F, 1.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(1.0F, -2.0F, 1.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.y = 0.0F; + b.y = 0.0F; + a.z = 1.1F; + b.z = -1.1F; + a.transform = SC_SACD_Mat4_Identity(); + CHECK_FALSE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(1.0F, 1.0F, 2.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + + a.transform = SC_SACD_Scale_Mat4(1.0F, 1.0F, -2.0F); + CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b)); + } + std::cout << "Checks checked: " << checks_checked << '\n' << "Checks passed: " << checks_passed << '\n';