1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Correções em collision.hpp

This commit is contained in:
Danilo 2024-11-10 21:18:02 -03:00
parent bd65e9fb55
commit e539dde3bd
4 changed files with 33 additions and 21 deletions

View File

@ -1,12 +1,20 @@
#ifndef XNA_COMMON_COLLISION_HPP #ifndef XNA_COMMON_COLLISION_HPP
#define XNA_COMMON_COLLISION_HPP #define XNA_COMMON_COLLISION_HPP
#include "../default.hpp"
#include "math.hpp" #include "math.hpp"
#include "numerics.hpp" #include "numerics.hpp"
#include <optional> #include <optional>
#include <cstdint>
#include <vector>
#include <limits>
namespace xna { namespace xna {
struct Plane;
struct BoundingFrustum;
struct BoundingBox;
struct BoundingSphere;
struct Ray;
class Gjk { class Gjk {
public: public:
constexpr Gjk() {} constexpr Gjk() {}
@ -28,11 +36,11 @@ namespace xna {
using listf = std::vector<float>; using listf = std::vector<float>;
using listff = std::vector<std::vector<float>>; using listff = std::vector<std::vector<float>>;
void UpdateDeterminant(Int xmIdx); void UpdateDeterminant(int32_t xmIdx);
bool UpdateSimplex(Int newIndex); bool UpdateSimplex(int32_t newIndex);
Vector3 ComputeClosestPoint(); Vector3 ComputeClosestPoint();
constexpr bool IsSatisfiesRule(Int xBits, Int yBits) const { constexpr bool IsSatisfiesRule(int32_t xBits, int32_t yBits) const {
bool flag = true; bool flag = true;
for (auto bitsToIndex = Gjk::BitsToIndices[yBits]; bitsToIndex != 0; bitsToIndex >>= 3) for (auto bitsToIndex = Gjk::BitsToIndices[yBits]; bitsToIndex != 0; bitsToIndex >>= 3)
{ {
@ -54,17 +62,12 @@ namespace xna {
} }
private: private:
inline static auto BitsToIndices = std::vector<Int>{ inline static auto BitsToIndices = std::vector<int32_t>{
0, 1, 2, 17, 3, 25, 26, 209, 4, 33, 34, 273, 35, 281, 282, 2257 0, 1, 2, 17, 3, 25, 26, 209, 4, 33, 34, 273, 35, 281, 282, 2257
}; };
using listv3 = std::vector<Vector3>;
using listv3v3 = std::vector<std::vector<Vector3>>;
using listf = std::vector<float>;
using listff = std::vector<std::vector<float>>;
Vector3 closestPoint{}; Vector3 closestPoint{};
Int simplexBits{ 0 }; int32_t simplexBits{ 0 };
float maxLengthSq{ 0 }; float maxLengthSq{ 0 };
listv3 y = listv3(4); listv3 y = listv3(4);
listf yLengthSq = listf(4); listf yLengthSq = listf(4);
@ -212,6 +215,10 @@ namespace xna {
private: private:
static constexpr Ray ComputeIntersectionLine(Plane const& p1, Plane const& p2); static constexpr Ray ComputeIntersectionLine(Plane const& p1, Plane const& p2);
static constexpr Vector3 ComputeIntersection(Plane const& plane, Ray const& ray); static constexpr Vector3 ComputeIntersection(Plane const& plane, Ray const& ray);
private:
static constexpr float FLOAT_MIN_VALUE = (std::numeric_limits<float>::min)();
static constexpr float FLOAT_MAX_VALUE = (std::numeric_limits<float>::max)();
}; };
//Defines an axis-aligned box-shaped 3D volume. //Defines an axis-aligned box-shaped 3D volume.
@ -263,6 +270,10 @@ namespace xna {
constexpr ContainmentType Contains(BoundingSphere const& sphere) const; constexpr ContainmentType Contains(BoundingSphere const& sphere) const;
constexpr void SupportMapping(Vector3 const& v, Vector3& result) const; constexpr void SupportMapping(Vector3 const& v, Vector3& result) const;
private:
static constexpr float FLOAT_MIN_VALUE = (std::numeric_limits<float>::min)();
static constexpr float FLOAT_MAX_VALUE = (std::numeric_limits<float>::max)();
}; };
//Defines a sphere. //Defines a sphere.
@ -556,8 +567,8 @@ namespace xna {
} }
constexpr BoundingBox BoundingBox::CreateFromPoints(std::vector<Vector3> const& points) { constexpr BoundingBox BoundingBox::CreateFromPoints(std::vector<Vector3> const& points) {
Vector3 result1 = Vector3(FloatMaxValue); Vector3 result1 = Vector3(FLOAT_MAX_VALUE);
Vector3 result2 = Vector3(FloatMinValue); Vector3 result2 = Vector3(FLOAT_MIN_VALUE);
for (size_t i = 0; i < points.size(); ++i) { for (size_t i = 0; i < points.size(); ++i) {
const auto& point = points[i]; const auto& point = points[i];

View File

@ -3,6 +3,7 @@
#include <limits> #include <limits>
#include <cmath> #include <cmath>
#include <algorithm>
namespace xna { namespace xna {
//Contains commonly used precalculated values. //Contains commonly used precalculated values.

View File

@ -64,7 +64,7 @@ namespace xna {
if (result1.LengthSquared() < 9.9999997473787516E-06) if (result1.LengthSquared() < 9.9999997473787516E-06)
result1 = Vector3::Subtract(corners[0], box.Max); result1 = Vector3::Subtract(corners[0], box.Max);
auto num1 = FloatMaxValue; auto num1 = FLOAT_MAX_VALUE;
float num2 = 0; float num2 = 0;
do do
{ {
@ -101,8 +101,8 @@ namespace xna {
if (result1 == ContainmentType::Contains) if (result1 == ContainmentType::Contains)
return 0.0F; return 0.0F;
auto num1 = FloatMinValue; auto num1 = FLOAT_MIN_VALUE;
auto num2 = FloatMaxValue; auto num2 = FLOAT_MAX_VALUE;
for (size_t i = 0; i < planes.size(); ++i) { for (size_t i = 0; i < planes.size(); ++i) {
const auto& plane = planes[i]; const auto& plane = planes[i];
@ -153,7 +153,7 @@ namespace xna {
if (result1.LengthSquared() < 9.9999997473787516E-06) if (result1.LengthSquared() < 9.9999997473787516E-06)
result1 = Vector3::UnitX(); result1 = Vector3::UnitX();
auto num1 = FloatMaxValue; auto num1 = FLOAT_MAX_VALUE;
auto num2 = 0.0F; auto num2 = 0.0F;
do do
@ -199,7 +199,7 @@ namespace xna {
if (result1.LengthSquared() < 9.9999997473787516E-06) if (result1.LengthSquared() < 9.9999997473787516E-06)
result1 = Vector3::Subtract(corners[0], frustum.corners[1]); result1 = Vector3::Subtract(corners[0], frustum.corners[1]);
float num1 = FloatMaxValue; float num1 = FLOAT_MAX_VALUE;
float num2 = 0; float num2 = 0;
do do
@ -255,7 +255,7 @@ namespace xna {
std::optional<float> BoundingBox::Intersects(Ray const& ray) const { std::optional<float> BoundingBox::Intersects(Ray const& ray) const {
float num1 = 0.0f; float num1 = 0.0f;
float num2 = FloatMaxValue; float num2 = FLOAT_MAX_VALUE;
if (std::abs(ray.Direction.X) < 9.9999999747524271E-07) if (std::abs(ray.Direction.X) < 9.9999999747524271E-07)
{ {

View File

@ -18,7 +18,7 @@ namespace xna {
return zero / num1; return zero / num1;
} }
bool Gjk::UpdateSimplex(Int newIndex) { bool Gjk::UpdateSimplex(int32_t newIndex) {
auto yBits = simplexBits | 1 << newIndex; auto yBits = simplexBits | 1 << newIndex;
auto xBits = 1 << newIndex; auto xBits = 1 << newIndex;
for (auto sb = simplexBits; sb != 0; --sb) for (auto sb = simplexBits; sb != 0; --sb)
@ -43,7 +43,7 @@ namespace xna {
return flag; return flag;
} }
void Gjk::UpdateDeterminant(Int xmIdx) { void Gjk::UpdateDeterminant(int32_t xmIdx) {
auto index1 = 1 << xmIdx; auto index1 = 1 << xmIdx;
det[index1][xmIdx] = 1.0f; det[index1][xmIdx] = 1.0f;
auto bitsToIndex = Gjk::BitsToIndices[simplexBits]; auto bitsToIndex = Gjk::BitsToIndices[simplexBits];