From a25788a51b8bc479e3e865d390b979f46a38fce6 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 9 May 2024 12:08:14 +0900 Subject: [PATCH 1/2] Add Vec3 helpers, Vec3 project and reflect fns --- src/sc_sacd.cpp | 26 +++++++++++++++ src/sc_sacd.h | 14 ++++++++ src/test.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) diff --git a/src/sc_sacd.cpp b/src/sc_sacd.cpp index c58bf6b..ac1f5c5 100644 --- a/src/sc_sacd.cpp +++ b/src/sc_sacd.cpp @@ -669,3 +669,29 @@ SC_SACD_AABB_Box SC_SACD_AABB_Combine(SC_SACD_AABB_Box a, SC_SACD_AABB_Box b) { temp_b, temp_c}; } + +SC_SACD_Vec3 SC_SACD_Vec3_Sum(SC_SACD_Vec3 a, SC_SACD_Vec3 b) { return a + b; } + +SC_SACD_Vec3 SC_SACD_Vec3_Difference(SC_SACD_Vec3 a, SC_SACD_Vec3 b) { + return a - b; +} + +SC_SACD_Vec3 SC_SACD_Vec3_Mult(SC_SACD_Vec3 vec, float scalar) { + return vec * scalar; +} + +SC_SACD_Vec3 SC_SACD_Vec3_Div(SC_SACD_Vec3 vec, float scalar) { + return vec / scalar; +} + +SC_SACD_Vec3 SC_SACD_Vec3_Project(SC_SACD_Vec3 vec, SC_SACD_Vec3 target) { + float upper_dot_product = SC_SACD_Dot_Product(vec, target); + float lower_dot_product = SC_SACD_Dot_Product(target, target); + + return target * (upper_dot_product / lower_dot_product); +} + +SC_SACD_Vec3 SC_SACD_Vec3_Reflect(SC_SACD_Vec3 vec, SC_SACD_Vec3 target) { + SC_SACD_Vec3 proj = SC_SACD_Vec3_Project(vec, target); + return proj * 2.0F - vec; +} diff --git a/src/sc_sacd.h b/src/sc_sacd.h index 0af170a..8b400a9 100644 --- a/src/sc_sacd.h +++ b/src/sc_sacd.h @@ -136,6 +136,20 @@ SC_SACD_Generic_Box_To_AABB(SC_SACD_Generic_Box s); SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_AABB_Combine(SC_SACD_AABB_Box a, SC_SACD_AABB_Box b); +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Sum(SC_SACD_Vec3 a, SC_SACD_Vec3 b); +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Difference(SC_SACD_Vec3 a, + SC_SACD_Vec3 b); +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Mult(SC_SACD_Vec3 vec, float scalar); +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Div(SC_SACD_Vec3 vec, float scalar); + +/// Projects "vec" onto "target". +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Project(SC_SACD_Vec3 vec, + SC_SACD_Vec3 target); + +/// Reflects "vec" about "target". +SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Vec3_Reflect(SC_SACD_Vec3 vec, + SC_SACD_Vec3 target); + #ifdef __cplusplus } #endif diff --git a/src/test.cpp b/src/test.cpp index e7618c7..ea8e03c 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -754,6 +754,92 @@ int main() { 2.0F); } + // Test Sum/Diff/Mult/Div Vec3. + { + SC_SACD_Vec3 a{1.0F, 2.0F, 3.0F}; + SC_SACD_Vec3 b{10.0F, 100.0F, 1000.0F}; + + SC_SACD_Vec3 vec_result = SC_SACD_Vec3_Sum(a, b); + CHECK_FLOAT(vec_result.x, 11.0F); + CHECK_FLOAT(vec_result.y, 102.0F); + CHECK_FLOAT(vec_result.z, 1003.0F); + + vec_result = SC_SACD_Vec3_Difference(a, b); + CHECK_FLOAT(vec_result.x, -9.0F); + CHECK_FLOAT(vec_result.y, -98.0F); + CHECK_FLOAT(vec_result.z, -997.0F); + + vec_result = SC_SACD_Vec3_Mult(a, 2.0F); + CHECK_FLOAT(vec_result.x, 2.0F); + CHECK_FLOAT(vec_result.y, 4.0F); + CHECK_FLOAT(vec_result.z, 6.0F); + + vec_result = SC_SACD_Vec3_Div(a, 2.0F); + CHECK_FLOAT(vec_result.x, 0.5F); + CHECK_FLOAT(vec_result.y, 1.0F); + CHECK_FLOAT(vec_result.z, 1.5F); + } + + // Test project. + { + SC_SACD_Vec3 a{2.0F, 3.0F, 4.0F}; + SC_SACD_Vec3 target{0.0F, 1.0F, 0.0F}; + + SC_SACD_Vec3 result = SC_SACD_Vec3_Project(a, target); + CHECK_FLOAT(result.x, 0.0F); + CHECK_FLOAT(result.y, 3.0F); + CHECK_FLOAT(result.z, 0.0F); + + target.x = 2.0F; + target.y = 0.0F; + result = SC_SACD_Vec3_Project(a, target); + CHECK_FLOAT(result.x, 2.0F); + CHECK_FLOAT(result.y, 0.0F); + CHECK_FLOAT(result.z, 0.0F); + + target.x = 0.0F; + target.z = 9.0F; + result = SC_SACD_Vec3_Project(a, target); + CHECK_FLOAT(result.x, 0.0F); + CHECK_FLOAT(result.y, 0.0F); + CHECK_FLOAT(result.z, 4.0F); + } + + // Test reflect. + { + SC_SACD_Vec3 a{2.0F, 3.0F, 4.0F}; + SC_SACD_Vec3 target{0.0F, 3.0F, 0.0F}; + + SC_SACD_Vec3 result = SC_SACD_Vec3_Reflect(a, target); + CHECK_FLOAT(result.x, -2.0F); + CHECK_FLOAT(result.y, 3.0F); + CHECK_FLOAT(result.z, -4.0F); + + target.x = 9.0F; + target.y = 0.0F; + + result = SC_SACD_Vec3_Reflect(a, target); + CHECK_FLOAT(result.x, 2.0F); + CHECK_FLOAT(result.y, -3.0F); + CHECK_FLOAT(result.z, -4.0F); + + target.x = 0.0F; + target.z = 110.0F; + + result = SC_SACD_Vec3_Reflect(a, target); + CHECK_FLOAT(result.x, -2.0F); + CHECK_FLOAT(result.y, -3.0F); + CHECK_FLOAT(result.z, 4.0F); + + target.y = 1.0F; + target.z = 1.0F; + + result = SC_SACD_Vec3_Reflect(a, target); + CHECK_FLOAT(result.x, -2.0F); + CHECK_FLOAT(result.y, 4.0F); + CHECK_FLOAT(result.z, 3.0F); + } + std::cout << "Checks checked: " << checks_checked << '\n' << "Checks passed: " << checks_passed << '\n'; From 20254418079dc8427f136220e1e5a69da0dd6a92 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 9 May 2024 12:08:59 +0900 Subject: [PATCH 2/2] Bump version 3.1.0, update Changelog --- CMakeLists.txt | 2 +- Changelog.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f9b2d67..6a93d65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(SC_3D_CollisionDetectionHelpers_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/src/sc_ add_library(SC_3D_CollisionDetectionHelpers ${SC_3D_CollisionDetectionHelpers_SOURCES}) -set_target_properties(SC_3D_CollisionDetectionHelpers PROPERTIES VERSION 3.0.1 SOVERSION 3) +set_target_properties(SC_3D_CollisionDetectionHelpers PROPERTIES VERSION 3.1.0 SOVERSION 3) target_compile_features(SC_3D_CollisionDetectionHelpers PUBLIC cxx_std_20) if(NOT DEFINED CMAKE_BUILD_TYPE OR "${CMAKE_BUILD_TYPE}" STREQUAL "") diff --git a/Changelog.md b/Changelog.md index e40ece9..6813733 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,11 @@ # Changelog +## Version 3.1.0 + +Add Vec3 helper functions. + +Add Vec3 project and reflect functions. + ## Version 3.0.1 Remove unnecessary header include in sc_sacd.cpp. This should fix builds that