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

Implementa Ray

This commit is contained in:
Danilo 2024-05-18 10:24:26 -03:00
parent 3abd8d50bf
commit 61fdf43488
2 changed files with 44 additions and 1 deletions

View File

@ -38,6 +38,24 @@ namespace xna {
return p;
}
std::optional<float> Plane::Intersects(Ray const& ray) const {
const auto num1 = (Normal.X * ray.Direction.X + Normal.Y * ray.Direction.Y + Normal.Z * ray.Direction.Z);
if (std::abs(num1) < 9.9999997473787516E-06)
return {};
const auto num2 = (Normal.X * ray.Position.X + Normal.Y * ray.Position.Y + Normal.Z * ray.Position.Z);
auto num3 = (-D - num2) / num1;
if (num3 < 0.0)
{
if (num3 < -9.9999997473787516E-06)
return {};
num3 = 0.0f;
}
return num3;
}
bool BoundingFrustum::Intersects(BoundingBox const& box) {
gjk.Reset();

View File

@ -41,6 +41,7 @@ namespace xna {
constexpr PlaneIntersectionType Intersects(BoundingBox const& box) const;
constexpr PlaneIntersectionType Intersects(BoundingFrustum const& frustum) const;
constexpr PlaneIntersectionType Intersects(BoundingSphere const& sphere) const;
std::optional<float> Intersects(Ray const& ray) const;
};
struct BoundingFrustum {
@ -166,6 +167,30 @@ namespace xna {
struct Ray {
Vector3 Position{};
Vector3 Direction{};
constexpr Ray() = default;
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;
}
std::optional<float> Intersects(BoundingBox const& box) const {
return box.Intersects(*this);
}
std::optional<float> Intersects(BoundingFrustum const& frustum) const {
return frustum.Intersects(*this);
}
std::optional<float> Intersects(Plane const& plane) const {
return plane.Intersects(*this);
}
std::optional<float> Sphere(BoundingSphere const& sphere) const {
return sphere.Intersects(*this);
}
};
//---------------------------------------------------------------------------------//
@ -570,7 +595,7 @@ namespace xna {
return num < -sphere.Radius
? PlaneIntersectionType::Back : PlaneIntersectionType::Intersecting;
}
}
}
#endif