Compare commits
5 commits
a40fc77762
...
8f7243cb24
Author | SHA1 | Date | |
---|---|---|---|
8f7243cb24 | |||
1d44827f96 | |||
ef52303a6a | |||
531b17a60b | |||
56a4c0783b |
5 changed files with 184 additions and 2 deletions
|
@ -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 2.1.0 SOVERSION 2)
|
||||
set_target_properties(SC_3D_CollisionDetectionHelpers PROPERTIES VERSION 2.2.0 SOVERSION 2)
|
||||
target_compile_features(SC_3D_CollisionDetectionHelpers PUBLIC cxx_std_20)
|
||||
|
||||
if(NOT DEFINED CMAKE_BUILD_TYPE OR "${CMAKE_BUILD_TYPE}" STREQUAL "")
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
# Changelog
|
||||
|
||||
## Upcoming Changes
|
||||
## Version 2.2.0
|
||||
|
||||
Refactoring of internally used function.
|
||||
|
||||
Add functions to convert Sphere and GenericBox to AABB.
|
||||
|
||||
Add function to combine two AABBs.
|
||||
|
||||
## Version 2.1.0
|
||||
|
||||
Refactoring of internally used function(s).
|
||||
|
|
107
src/sc_sacd.cpp
107
src/sc_sacd.cpp
|
@ -572,3 +572,110 @@ SC_SACD_Vec3 SC_SACD_Closest_Point(const SC_SACD_Vec3 *pos,
|
|||
float SC_SACD_Vec3_Length(const SC_SACD_Vec3 vec) {
|
||||
return std::sqrt(SC_SACD_Dot_Product(vec, vec));
|
||||
}
|
||||
|
||||
SC_SACD_AABB_Box SC_SACD_Sphere_To_AABB(const SC_SACD_Sphere s) {
|
||||
SC_SACD_AABB_Box aabb{};
|
||||
|
||||
aabb.x = s.x;
|
||||
aabb.y = s.y;
|
||||
aabb.z = s.z;
|
||||
|
||||
aabb.width = s.radius * 2.0F;
|
||||
aabb.height = s.radius * 2.0F;
|
||||
aabb.depth = s.radius * 2.0F;
|
||||
|
||||
return aabb;
|
||||
}
|
||||
|
||||
SC_SACD_AABB_Box SC_SACD_Generic_Box_To_AABB(const SC_SACD_Generic_Box s) {
|
||||
SC_SACD_AABB_Box aabb{};
|
||||
|
||||
auto corners = SC_SACD_Get_Box_Corners(&s);
|
||||
|
||||
SC_SACD_Vec3 min{INFINITY, INFINITY, INFINITY};
|
||||
SC_SACD_Vec3 max{-INFINITY, -INFINITY, -INFINITY};
|
||||
|
||||
for (const auto &corner : corners) {
|
||||
if (corner.x < min.x) {
|
||||
min.x = corner.x;
|
||||
}
|
||||
if (corner.y < min.y) {
|
||||
min.y = corner.y;
|
||||
}
|
||||
if (corner.z < min.z) {
|
||||
min.z = corner.z;
|
||||
}
|
||||
|
||||
if (corner.x > max.x) {
|
||||
max.x = corner.x;
|
||||
}
|
||||
if (corner.y > max.y) {
|
||||
max.y = corner.y;
|
||||
}
|
||||
if (corner.z > max.z) {
|
||||
max.z = corner.z;
|
||||
}
|
||||
}
|
||||
|
||||
aabb.width = max.x - min.x;
|
||||
aabb.height = max.y - min.y;
|
||||
aabb.depth = max.z - min.z;
|
||||
|
||||
aabb.x = min.x + aabb.width / 2.0F;
|
||||
aabb.y = min.y + aabb.height / 2.0F;
|
||||
aabb.z = min.z + aabb.depth / 2.0F;
|
||||
|
||||
return aabb;
|
||||
}
|
||||
|
||||
SC_SACD_AABB_Box SC_SACD_AABB_Combine(const SC_SACD_AABB_Box a,
|
||||
const SC_SACD_AABB_Box b) {
|
||||
SC_SACD_Vec3 min, max;
|
||||
|
||||
// Populate min values.
|
||||
|
||||
float temp_a = a.x - a.width / 2.0F;
|
||||
float temp_b = b.x - b.width / 2.0F;
|
||||
|
||||
min.x = temp_a < temp_b ? temp_a : temp_b;
|
||||
|
||||
temp_a = a.y - a.height / 2.0F;
|
||||
temp_b = b.y - b.height / 2.0F;
|
||||
|
||||
min.y = temp_a < temp_b ? temp_a : temp_b;
|
||||
|
||||
temp_a = a.z - a.depth / 2.0F;
|
||||
temp_b = b.z - b.depth / 2.0F;
|
||||
|
||||
min.z = temp_a < temp_b ? temp_a : temp_b;
|
||||
|
||||
// Populate max values.
|
||||
|
||||
temp_a = a.x + a.width / 2.0F;
|
||||
temp_b = b.x + b.width / 2.0F;
|
||||
|
||||
max.x = temp_a > temp_b ? temp_a : temp_b;
|
||||
|
||||
temp_a = a.y + a.height / 2.0F;
|
||||
temp_b = b.y + b.height / 2.0F;
|
||||
|
||||
max.y = temp_a > temp_b ? temp_a : temp_b;
|
||||
|
||||
temp_a = a.z + a.depth / 2.0F;
|
||||
temp_b = b.z + b.depth / 2.0F;
|
||||
|
||||
max.z = temp_a > temp_b ? temp_a : temp_b;
|
||||
|
||||
// Populate the result.
|
||||
|
||||
temp_a = max.x - min.x;
|
||||
temp_b = max.y - min.y;
|
||||
float temp_c = max.z - min.z;
|
||||
|
||||
return SC_SACD_AABB_Box{min.x + temp_a / 2.0F,
|
||||
min.y + temp_b / 2.0F,
|
||||
min.z + temp_c / 2.0F,
|
||||
temp_a,
|
||||
temp_b,
|
||||
temp_c};
|
||||
}
|
||||
|
|
|
@ -133,6 +133,14 @@ SC_SACD_EXPORT SC_SACD_Vec3 SC_SACD_Closest_Point(const SC_SACD_Vec3 *pos,
|
|||
|
||||
SC_SACD_EXPORT float SC_SACD_Vec3_Length(const SC_SACD_Vec3 vec);
|
||||
|
||||
SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_Sphere_To_AABB(const SC_SACD_Sphere s);
|
||||
SC_SACD_EXPORT SC_SACD_AABB_Box
|
||||
SC_SACD_Generic_Box_To_AABB(const SC_SACD_Generic_Box s);
|
||||
|
||||
/// Combines AABB's such that the new AABB encompasses the two AABB's.
|
||||
SC_SACD_EXPORT SC_SACD_AABB_Box SC_SACD_AABB_Combine(const SC_SACD_AABB_Box a,
|
||||
const SC_SACD_AABB_Box b);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
63
src/test.cpp
63
src/test.cpp
|
@ -633,6 +633,69 @@ int main() {
|
|||
CHECK_TRUE(SC_SACD_Generic_Box_Collision(&a, &b));
|
||||
}
|
||||
|
||||
// Test Sphere/GenericBox to AABB.
|
||||
{
|
||||
SC_SACD_Sphere s{10.0F, 10.0F, 10.0F, 5.0F};
|
||||
|
||||
SC_SACD_AABB_Box aabb = SC_SACD_Sphere_To_AABB(s);
|
||||
CHECK_FLOAT(aabb.x, s.x);
|
||||
CHECK_FLOAT(aabb.y, s.y);
|
||||
CHECK_FLOAT(aabb.z, s.z);
|
||||
CHECK_FLOAT(aabb.width, s.radius * 2.0F);
|
||||
CHECK_FLOAT(aabb.height, s.radius * 2.0F);
|
||||
CHECK_FLOAT(aabb.depth, s.radius * 2.0F);
|
||||
|
||||
SC_SACD_Generic_Box box = SC_SACD_Generic_Box_Default();
|
||||
box.x = 20.0F;
|
||||
box.y = 20.0F;
|
||||
box.z = 20.0F;
|
||||
|
||||
aabb = SC_SACD_Generic_Box_To_AABB(box);
|
||||
CHECK_FLOAT(aabb.x, box.x);
|
||||
CHECK_FLOAT(aabb.y, box.y);
|
||||
CHECK_FLOAT(aabb.z, box.z);
|
||||
CHECK_FLOAT(aabb.width, box.width);
|
||||
CHECK_FLOAT(aabb.height, box.height);
|
||||
CHECK_FLOAT(aabb.depth, box.depth);
|
||||
|
||||
box.transform =
|
||||
SC_SACD_Rotation_Mat4_ZAxis(std::numbers::pi_v<float> / 4.0F);
|
||||
|
||||
aabb = SC_SACD_Generic_Box_To_AABB(box);
|
||||
CHECK_FLOAT(aabb.x, box.x);
|
||||
CHECK_FLOAT(aabb.y, box.y);
|
||||
CHECK_FLOAT(aabb.z, box.z);
|
||||
CHECK_FLOAT(aabb.width, std::sqrt(2.0F) * 2.0F);
|
||||
CHECK_FLOAT(aabb.height, std::sqrt(2.0F) * 2.0F);
|
||||
CHECK_FLOAT(aabb.depth, box.depth);
|
||||
|
||||
auto translate = SC_SACD_Translate_Mat4(-5.0F, -4.0F, 1.0F);
|
||||
box.transform = SC_SACD_Mat4_Mult(&translate, &box.transform);
|
||||
|
||||
aabb = SC_SACD_Generic_Box_To_AABB(box);
|
||||
CHECK_FLOAT(aabb.x, box.x - 5.0F);
|
||||
CHECK_FLOAT(aabb.y, box.y - 4.0F);
|
||||
CHECK_FLOAT(aabb.z, box.z + 1.0F);
|
||||
CHECK_FLOAT(aabb.width, std::sqrt(2.0F) * 2.0F);
|
||||
CHECK_FLOAT(aabb.height, std::sqrt(2.0F) * 2.0F);
|
||||
CHECK_FLOAT(aabb.depth, box.depth);
|
||||
}
|
||||
|
||||
// Test combining AABB.
|
||||
{
|
||||
SC_SACD_AABB_Box a{5.0F, 5.0F, 5.0F, 2.0F, 2.0F, 2.0F};
|
||||
|
||||
SC_SACD_AABB_Box b{-3.0F, -3.0F, -3.0F, 2.0F, 2.0F, 2.0F};
|
||||
|
||||
auto combined = SC_SACD_AABB_Combine(a, b);
|
||||
CHECK_FLOAT(combined.x, (7.0F - 5.0F) / 2.0F);
|
||||
CHECK_FLOAT(combined.y, (7.0F - 5.0F) / 2.0F);
|
||||
CHECK_FLOAT(combined.z, (7.0F - 5.0F) / 2.0F);
|
||||
CHECK_FLOAT(combined.width, 10.0F);
|
||||
CHECK_FLOAT(combined.height, 10.0F);
|
||||
CHECK_FLOAT(combined.depth, 10.0F);
|
||||
}
|
||||
|
||||
std::cout << "Checks checked: " << checks_checked << '\n'
|
||||
<< "Checks passed: " << checks_passed << '\n';
|
||||
|
||||
|
|
Loading…
Reference in a new issue