Compare commits

..

No commits in common. "763b2d3a6d365c4e1d28592f587f3e747fc6ba40" and "5a1413bda92de2582332c390abe3088ea5125278" have entirely different histories.

2 changed files with 24 additions and 37 deletions

View file

@ -1,9 +1,5 @@
# Changelog # Changelog
## Upcoming Changes
Refactoring of internally used function(s).
## Version 2.0.2 ## Version 2.0.2
Fix SC_SACD_Translate_Mat4(...). It was missing a "1" in the first element of Fix SC_SACD_Translate_Mat4(...). It was missing a "1" in the first element of

View file

@ -1,7 +1,6 @@
#include "sc_sacd.h" #include "sc_sacd.h"
// Standard library includes. // Standard library includes.
#include <array>
#include <cmath> #include <cmath>
#include <stdfloat> #include <stdfloat>
#include <vector> #include <vector>
@ -61,8 +60,9 @@ SC_SACD_Vec3 operator*(const SC_SACD_Mat4 &mat, const SC_SACD_Vec3 &vec) {
vec.x * mat.z0 + vec.y * mat.z1 + vec.z * mat.z2 + mat.z3}; vec.x * mat.z0 + vec.y * mat.z1 + vec.z * mat.z2 + mat.z3};
} }
std::array<SC_SACD_Vec3, 3> SC_SACD_Get_Box_Normals( std::vector<SC_SACD_Vec3> SC_SACD_Get_Box_Normals(
const SC_SACD_Generic_Box *box) { const SC_SACD_Generic_Box *box) {
std::vector<SC_SACD_Vec3> normals;
SC_SACD_Vec3 a, b, c; SC_SACD_Vec3 a, b, c;
// Facing positive x-axis. // Facing positive x-axis.
@ -85,7 +85,7 @@ std::array<SC_SACD_Vec3, 3> SC_SACD_Get_Box_Normals(
b = b - a; b = b - a;
c = c - a; c = c - a;
auto normal_x = SC_SACD_Cross_Product(b, c); normals.push_back(SC_SACD_Cross_Product(b, c));
// Facing positive y-axis. // Facing positive y-axis.
a.x = 0.0F; a.x = 0.0F;
@ -107,7 +107,7 @@ std::array<SC_SACD_Vec3, 3> SC_SACD_Get_Box_Normals(
b = b - a; b = b - a;
c = c - a; c = c - a;
auto normal_y = SC_SACD_Cross_Product(b, c); normals.push_back(SC_SACD_Cross_Product(b, c));
// Facing positive z-axis. // Facing positive z-axis.
a.x = 0.0F; a.x = 0.0F;
@ -129,14 +129,14 @@ std::array<SC_SACD_Vec3, 3> SC_SACD_Get_Box_Normals(
b = b - a; b = b - a;
c = c - a; c = c - a;
auto normal_z = SC_SACD_Cross_Product(b, c); normals.push_back(SC_SACD_Cross_Product(b, c));
return {normal_x, normal_y, normal_z}; return normals;
} }
std::array<SC_SACD_Vec3, 3> SC_SACD_Get_Box_Normals_Normalized( std::vector<SC_SACD_Vec3> SC_SACD_Get_Box_Normals_Normalized(
const SC_SACD_Generic_Box *box) { const SC_SACD_Generic_Box *box) {
auto normals = SC_SACD_Get_Box_Normals(box); std::vector<SC_SACD_Vec3> normals = SC_SACD_Get_Box_Normals(box);
for (auto &normal : normals) { for (auto &normal : normals) {
normal = normal / std::sqrt(SC_SACD_Dot_Product(normal, normal)); normal = normal / std::sqrt(SC_SACD_Dot_Product(normal, normal));
@ -213,18 +213,17 @@ struct SC_SACD_MinMax {
}; };
std::vector<SC_SACD_MinMax> SC_SACD_Get_Box_MinMax( std::vector<SC_SACD_MinMax> SC_SACD_Get_Box_MinMax(
const SC_SACD_Generic_Box *box, const SC_SACD_Vec3 *normals, const SC_SACD_Generic_Box *box, const std::vector<SC_SACD_Vec3> &normals) {
std::size_t size) {
std::vector<SC_SACD_MinMax> minmaxes; std::vector<SC_SACD_MinMax> minmaxes;
std::vector<SC_SACD_Vec3> corners = SC_SACD_Get_Box_Corners(box); std::vector<SC_SACD_Vec3> corners = SC_SACD_Get_Box_Corners(box);
// Assuming normals are not normalized, and will not normalize anyway. // Assuming normals are not normalized, and will not normalize anyway.
// MinMax count should be same as normals count. // MinMax count should be same as normals count.
for (std::size_t idx = 0; idx < size; ++idx) { for (const auto &normal : normals) {
SC_SACD_MinMax minmax{INFINITY, -INFINITY}; SC_SACD_MinMax minmax{INFINITY, -INFINITY};
for (const auto &corner : corners) { for (const auto &corner : corners) {
float projected = SC_SACD_Dot_Product(corner, normals[idx]); float projected = SC_SACD_Dot_Product(corner, normal);
if (projected > minmax.max) { if (projected > minmax.max) {
minmax.max = projected; minmax.max = projected;
} }
@ -273,21 +272,14 @@ int SC_SACD_AABB_Box_Collision(const SC_SACD_AABB_Box *a,
int SC_SACD_Generic_Box_Collision(const SC_SACD_Generic_Box *a, int SC_SACD_Generic_Box_Collision(const SC_SACD_Generic_Box *a,
const SC_SACD_Generic_Box *b) { const SC_SACD_Generic_Box *b) {
// Get all normals. // Get all normals.
std::vector<SC_SACD_Vec3> normals; std::vector<SC_SACD_Vec3> normals = SC_SACD_Get_Box_Normals(a);
{ for (const auto &normal : SC_SACD_Get_Box_Normals(b)) {
for (const auto &normal : SC_SACD_Get_Box_Normals(a)) { normals.push_back(normal);
normals.push_back(normal);
}
for (const auto &normal : SC_SACD_Get_Box_Normals(b)) {
normals.push_back(normal);
}
} }
// Get all minmaxes. // Get all minmaxes.
std::vector<SC_SACD_MinMax> minmaxes_a = std::vector<SC_SACD_MinMax> minmaxes_a = SC_SACD_Get_Box_MinMax(a, normals);
SC_SACD_Get_Box_MinMax(a, normals.data(), normals.size()); std::vector<SC_SACD_MinMax> minmaxes_b = SC_SACD_Get_Box_MinMax(b, normals);
std::vector<SC_SACD_MinMax> minmaxes_b =
SC_SACD_Get_Box_MinMax(b, normals.data(), normals.size());
// Check minmaxes. // Check minmaxes.
for (unsigned int i = 0; i < normals.size(); ++i) { for (unsigned int i = 0; i < normals.size(); ++i) {
@ -367,7 +359,7 @@ int SC_SACD_Sphere_Box_Collision(const SC_SACD_Sphere *sphere,
std::vector<SC_SACD_Vec3> normals{sphere_box_normal}; std::vector<SC_SACD_Vec3> normals{sphere_box_normal};
std::vector<SC_SACD_MinMax> box_minmaxes = std::vector<SC_SACD_MinMax> box_minmaxes =
SC_SACD_Get_Box_MinMax(box, normals.data(), normals.size()); SC_SACD_Get_Box_MinMax(box, normals);
float projected_0 = SC_SACD_Dot_Product( float projected_0 = SC_SACD_Dot_Product(
sphere_box_normal, sphere_pos + sphere_box_normal * sphere->radius); sphere_box_normal, sphere_pos + sphere_box_normal * sphere->radius);
@ -385,14 +377,13 @@ int SC_SACD_Sphere_Box_Collision(const SC_SACD_Sphere *sphere,
// Next check the planes for the 3 normals of the box. // Next check the planes for the 3 normals of the box.
auto box_normals = SC_SACD_Get_Box_Normals(box); normals = SC_SACD_Get_Box_Normals(box);
box_minmaxes = box_minmaxes = SC_SACD_Get_Box_MinMax(box, normals);
SC_SACD_Get_Box_MinMax(box, box_normals.data(), box_normals.size()); for (unsigned int i = 0; i < normals.size(); ++i) {
for (unsigned int i = 0; i < box_normals.size(); ++i) { projected_0 = SC_SACD_Dot_Product(normals[i],
projected_0 = SC_SACD_Dot_Product( sphere_pos + normals[i] * sphere->radius);
box_normals[i], sphere_pos + box_normals[i] * sphere->radius); projected_1 = SC_SACD_Dot_Product(normals[i],
projected_1 = SC_SACD_Dot_Product( sphere_pos - normals[i] * sphere->radius);
box_normals[i], sphere_pos - box_normals[i] * sphere->radius);
if (projected_0 < projected_1) { if (projected_0 < projected_1) {
if (box_minmaxes[i].max < projected_0 || if (box_minmaxes[i].max < projected_0 ||
box_minmaxes[i].min > projected_1) { box_minmaxes[i].min > projected_1) {