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:
parent
e539dde3bd
commit
488e0a513e
@ -104,12 +104,12 @@ namespace xna {
|
||||
float D{ 0 };
|
||||
|
||||
constexpr Plane() = default;
|
||||
constexpr Plane(float a, float b, float c, float d):
|
||||
Normal({a,b,c}), D(d){}
|
||||
constexpr Plane(Vector3 const& normal, float d):
|
||||
Normal(normal), D(d){}
|
||||
constexpr Plane(Vector4 const& value):
|
||||
Normal({value.X, value.Y, value.Z}), D(value.W){}
|
||||
constexpr Plane(float a, float b, float c, float d) :
|
||||
Normal({ a,b,c }), D(d) {}
|
||||
constexpr Plane(Vector3 const& normal, float d) :
|
||||
Normal(normal), D(d) {}
|
||||
constexpr Plane(Vector4 const& value) :
|
||||
Normal({ value.X, value.Y, value.Z }), D(value.W) {}
|
||||
|
||||
Plane(Vector3 const& point1, Vector3 const& point2, Vector3 const& point3);
|
||||
|
||||
@ -146,8 +146,8 @@ namespace xna {
|
||||
|
||||
//Defines a frustum and helps determine whether forms intersect with it.
|
||||
struct BoundingFrustum {
|
||||
inline static constexpr int CornerCount = 8;
|
||||
inline static constexpr int PlaneCount = 6;
|
||||
static constexpr int CornerCount = 8;
|
||||
static constexpr int PlaneCount = 6;
|
||||
|
||||
constexpr BoundingFrustum() = default;
|
||||
constexpr BoundingFrustum(Matrix const& matrix) {
|
||||
@ -205,10 +205,10 @@ namespace xna {
|
||||
|
||||
constexpr void SupportMapping(Vector3 const& v, Vector3& result) const;
|
||||
public:
|
||||
std::vector<Vector3> corners{ 8 };
|
||||
Vector3 corners[8];
|
||||
Plane planes[6];
|
||||
|
||||
private:
|
||||
std::vector<Plane> planes{ 6 };
|
||||
Matrix matrix{ Matrix::Identity() };
|
||||
Gjk gjk{};
|
||||
|
||||
@ -232,8 +232,8 @@ namespace xna {
|
||||
Vector3 Max{};
|
||||
|
||||
constexpr BoundingBox() = default;
|
||||
constexpr BoundingBox(Vector3 const& min, Vector3 const& max):
|
||||
Min(min), Max(max){}
|
||||
constexpr BoundingBox(Vector3 const& min, Vector3 const& max) :
|
||||
Min(min), Max(max) {}
|
||||
|
||||
constexpr bool operator==(BoundingBox const& other) const {
|
||||
return Min == other.Min && Max == other.Max;
|
||||
@ -247,7 +247,11 @@ namespace xna {
|
||||
//Creates the smallest BoundingBox that will contain the specified BoundingSphere.
|
||||
static constexpr BoundingBox CreateFromSphere(BoundingSphere const& sphere);
|
||||
//Creates the smallest BoundingBox that will contain a group of points.
|
||||
static constexpr BoundingBox CreateFromPoints(std::vector<Vector3> const& points);
|
||||
static constexpr BoundingBox CreateFromPoints(std::vector<Vector3> const& points) {
|
||||
return CreateFromPoints(points.data(), points.size());
|
||||
}
|
||||
//Creates the smallest BoundingBox that will contain a group of points.
|
||||
static constexpr BoundingBox CreateFromPoints(Vector3 const* points, size_t size);
|
||||
|
||||
//Checks whether the current BoundingBox intersects with another bounding volume.
|
||||
constexpr bool Intersects(BoundingBox const& box) const;
|
||||
@ -284,8 +288,8 @@ namespace xna {
|
||||
float Radius{ 0 };
|
||||
|
||||
constexpr BoundingSphere() = default;
|
||||
constexpr BoundingSphere(Vector3 const& center, float radius):
|
||||
Center(center), Radius(radius < 0 ? 0 : radius){}
|
||||
constexpr BoundingSphere(Vector3 const& center, float radius) :
|
||||
Center(center), Radius(radius < 0 ? 0 : radius) {}
|
||||
|
||||
constexpr bool operator==(BoundingSphere const& other) const {
|
||||
return Center == other.Center && Radius == other.Radius;
|
||||
@ -296,7 +300,12 @@ namespace xna {
|
||||
//Creates the smallest BoundingSphere that can contain a specified BoundingBox.
|
||||
static BoundingSphere CreateFromBoundingBox(BoundingBox const& box);
|
||||
//Creates a BoundingSphere that can contain a specified list of points.
|
||||
static BoundingSphere CreateFromPoints(std::vector<Vector3> const& points);
|
||||
static BoundingSphere CreateFromPoints(std::vector<Vector3> const& points) {
|
||||
return CreateFromPoints(points.data(), points.size());
|
||||
}
|
||||
//Creates a BoundingSphere that can contain a specified list of points.
|
||||
static BoundingSphere CreateFromPoints(Vector3 const* points, size_t size);
|
||||
|
||||
//Creates the smallest BoundingSphere that can contain a specified BoundingFrustum.
|
||||
static BoundingSphere CreateFromFrustum(BoundingFrustum const& points);
|
||||
|
||||
@ -334,8 +343,8 @@ namespace xna {
|
||||
Vector3 Direction{};
|
||||
|
||||
constexpr Ray() = default;
|
||||
constexpr Ray(Vector3 const& position, Vector3 const& direction):
|
||||
Position(position), Direction(direction){}
|
||||
constexpr Ray(Vector3 const& position, Vector3 const& direction) :
|
||||
Position(position), Direction(direction) {}
|
||||
|
||||
constexpr bool operator==(Ray const& other) const {
|
||||
return Position == other.Position && Direction == other.Direction;
|
||||
@ -566,11 +575,11 @@ namespace xna {
|
||||
return fromSphere;
|
||||
}
|
||||
|
||||
constexpr BoundingBox BoundingBox::CreateFromPoints(std::vector<Vector3> const& points) {
|
||||
constexpr BoundingBox BoundingBox::CreateFromPoints(Vector3 const* points, size_t size) {
|
||||
Vector3 result1 = Vector3(FLOAT_MAX_VALUE);
|
||||
Vector3 result2 = Vector3(FLOAT_MIN_VALUE);
|
||||
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
const auto& point = points[i];
|
||||
result1 = Vector3::Min(result1, point);
|
||||
result2 = Vector3::Max(result2, point);
|
||||
@ -611,7 +620,7 @@ namespace xna {
|
||||
|
||||
constexpr bool BoundingBox::Intersects(BoundingSphere const& sphere) const {
|
||||
const auto result1 = Vector3::Clamp(sphere.Center, Min, Max);
|
||||
const auto result2 = Vector3::DistanceSquared(sphere.Center, result1);
|
||||
const auto result2 = Vector3::DistanceSquared(sphere.Center, result1);
|
||||
return result2 <= sphere.Radius * sphere.Radius;
|
||||
}
|
||||
|
||||
@ -624,7 +633,7 @@ namespace xna {
|
||||
}
|
||||
|
||||
constexpr ContainmentType BoundingBox::Contains(Vector3 const& point) const {
|
||||
return Min.X > point.X || point.X > Max.X || Min.Y > point.Y || point.Y > Max.Y || Min.Z > point.Z || point.Z > Max.Z
|
||||
return Min.X > point.X || point.X > Max.X || Min.Y > point.Y || point.Y > Max.Y || Min.Z > point.Z || point.Z > Max.Z
|
||||
? ContainmentType::Disjoint
|
||||
: ContainmentType::Contains;
|
||||
}
|
||||
@ -651,7 +660,7 @@ namespace xna {
|
||||
|
||||
constexpr bool BoundingSphere::Intersects(BoundingBox const& box) const {
|
||||
Vector3 result1 = Vector3::Clamp(Center, box.Min, box.Max);
|
||||
float result2 = Vector3::DistanceSquared(Center, result1);
|
||||
float result2 = Vector3::DistanceSquared(Center, result1);
|
||||
return result2 <= Radius * Radius;
|
||||
}
|
||||
|
||||
@ -677,7 +686,7 @@ namespace xna {
|
||||
|
||||
|
||||
constexpr Plane Plane::Transform(Plane const& plane, Matrix const& matrix) {
|
||||
Matrix result = Matrix::Invert(matrix);
|
||||
Matrix result = Matrix::Invert(matrix);
|
||||
const auto x = plane.Normal.X;
|
||||
const auto y = plane.Normal.Y;
|
||||
const auto z = plane.Normal.Z;
|
||||
@ -759,7 +768,7 @@ namespace xna {
|
||||
constexpr PlaneIntersectionType Plane::Intersects(BoundingSphere const& sphere) const {
|
||||
const auto num = (sphere.Center.X * Normal.X + sphere.Center.Y * Normal.Y + sphere.Center.Z * Normal.Z) + D;
|
||||
|
||||
if (num > sphere.Radius)
|
||||
if (num > sphere.Radius)
|
||||
return PlaneIntersectionType::Front;
|
||||
|
||||
return num < -sphere.Radius
|
||||
|
@ -104,7 +104,7 @@ namespace xna {
|
||||
auto num1 = FLOAT_MIN_VALUE;
|
||||
auto num2 = FLOAT_MAX_VALUE;
|
||||
|
||||
for (size_t i = 0; i < planes.size(); ++i) {
|
||||
for (size_t i = 0; i < PlaneCount; ++i) {
|
||||
const auto& plane = planes[i];
|
||||
|
||||
Vector3 normal = plane.Normal;
|
||||
@ -242,7 +242,7 @@ namespace xna {
|
||||
if (Intersects(frustum)) {
|
||||
containmentType = ContainmentType::Contains;
|
||||
|
||||
for (size_t index = 0; index < corners.size(); ++index) {
|
||||
for (size_t index = 0; index < CornerCount; ++index) {
|
||||
if (Contains(frustum.corners[index]) == ContainmentType::Disjoint)
|
||||
{
|
||||
containmentType = ContainmentType::Intersects;
|
||||
@ -345,10 +345,10 @@ namespace xna {
|
||||
}
|
||||
|
||||
BoundingSphere BoundingSphere::CreateFromFrustum(BoundingFrustum const& frustum) {
|
||||
return BoundingSphere::CreateFromPoints(frustum.corners);
|
||||
return BoundingSphere::CreateFromPoints(frustum.corners, frustum.CornerCount);
|
||||
}
|
||||
|
||||
BoundingSphere BoundingSphere::CreateFromPoints(std::vector<Vector3> const& points) {
|
||||
BoundingSphere BoundingSphere::CreateFromPoints(Vector3 const* points, size_t size) {
|
||||
Vector3 current;
|
||||
auto vector3_1 = current = points[0];
|
||||
auto vector3_2 = current;
|
||||
@ -357,7 +357,7 @@ namespace xna {
|
||||
auto vector3_5 = current;
|
||||
auto vector3_6 = current;
|
||||
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
const auto& point = points[i];
|
||||
|
||||
if (point.X < vector3_6.X)
|
||||
@ -404,7 +404,7 @@ namespace xna {
|
||||
num1 = result3 * 0.5f;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
const auto& point = points[i];
|
||||
|
||||
Vector3 vector3_7;
|
||||
|
Loading…
x
Reference in New Issue
Block a user