Optimize CollisionsHelper::FrustumContainsBox

This commit is contained in:
Wojtek Figat
2023-06-28 12:21:58 +02:00
parent 75d5023354
commit 17aa4ea60d
2 changed files with 6 additions and 14 deletions

View File

@@ -12,6 +12,7 @@
/// </summary> /// </summary>
API_STRUCT(InBuild) struct FLAXENGINE_API BoundingFrustum API_STRUCT(InBuild) struct FLAXENGINE_API BoundingFrustum
{ {
friend CollisionsHelper;
private: private:
Matrix _matrix; Matrix _matrix;

View File

@@ -939,7 +939,6 @@ bool CollisionsHelper::RayIntersectsSphere(const Ray& ray, const BoundingSphere&
normal = Vector3::Up; normal = Vector3::Up;
return false; return false;
} }
const Vector3 point = ray.Position + ray.Direction * distance; const Vector3 point = ray.Position + ray.Direction * distance;
normal = Vector3::Normalize(point - sphere.Center); normal = Vector3::Normalize(point - sphere.Center);
return true; return true;
@@ -953,22 +952,17 @@ bool CollisionsHelper::RayIntersectsSphere(const Ray& ray, const BoundingSphere&
point = Vector3::Zero; point = Vector3::Zero;
return false; return false;
} }
point = ray.Position + ray.Direction * distance; point = ray.Position + ray.Direction * distance;
return true; return true;
} }
PlaneIntersectionType CollisionsHelper::PlaneIntersectsPoint(const Plane& plane, const Vector3& point) PlaneIntersectionType CollisionsHelper::PlaneIntersectsPoint(const Plane& plane, const Vector3& point)
{ {
Real distance = Vector3::Dot(plane.Normal, point); const Real distance = Vector3::Dot(plane.Normal, point) + plane.D;
distance += plane.D;
if (distance > Plane::DistanceEpsilon) if (distance > Plane::DistanceEpsilon)
return PlaneIntersectionType::Front; return PlaneIntersectionType::Front;
if (distance < Plane::DistanceEpsilon) if (distance < Plane::DistanceEpsilon)
return PlaneIntersectionType::Back; return PlaneIntersectionType::Back;
return PlaneIntersectionType::Intersecting; return PlaneIntersectionType::Intersecting;
} }
@@ -1169,7 +1163,6 @@ ContainmentType CollisionsHelper::SphereContainsPoint(const BoundingSphere& sphe
{ {
if (Vector3::DistanceSquared(point, sphere.Center) <= sphere.Radius * sphere.Radius) if (Vector3::DistanceSquared(point, sphere.Center) <= sphere.Radius * sphere.Radius)
return ContainmentType::Contains; return ContainmentType::Contains;
return ContainmentType::Disjoint; return ContainmentType::Disjoint;
} }
@@ -1254,13 +1247,10 @@ ContainmentType CollisionsHelper::SphereContainsBox(const BoundingSphere& sphere
ContainmentType CollisionsHelper::SphereContainsSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2) ContainmentType CollisionsHelper::SphereContainsSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2)
{ {
const Real distance = Vector3::Distance(sphere1.Center, sphere2.Center); const Real distance = Vector3::Distance(sphere1.Center, sphere2.Center);
if (sphere1.Radius + sphere2.Radius < distance) if (sphere1.Radius + sphere2.Radius < distance)
return ContainmentType::Disjoint; return ContainmentType::Disjoint;
if (sphere1.Radius - sphere2.Radius < distance) if (sphere1.Radius - sphere2.Radius < distance)
return ContainmentType::Intersects; return ContainmentType::Intersects;
return ContainmentType::Contains; return ContainmentType::Contains;
} }
@@ -1274,7 +1264,8 @@ ContainmentType CollisionsHelper::FrustumContainsBox(const BoundingFrustum& frus
auto result = ContainmentType::Contains; auto result = ContainmentType::Contains;
for (int32 i = 0; i < 6; i++) for (int32 i = 0; i < 6; i++)
{ {
Plane plane = frustum.GetPlane(i); Plane plane = frustum._planes[i];
Vector3 p = box.Minimum; Vector3 p = box.Minimum;
if (plane.Normal.X >= 0) if (plane.Normal.X >= 0)
p.X = box.Maximum.X; p.X = box.Maximum.X;
@@ -1282,7 +1273,7 @@ ContainmentType CollisionsHelper::FrustumContainsBox(const BoundingFrustum& frus
p.Y = box.Maximum.Y; p.Y = box.Maximum.Y;
if (plane.Normal.Z >= 0) if (plane.Normal.Z >= 0)
p.Z = box.Maximum.Z; p.Z = box.Maximum.Z;
if (PlaneIntersectsPoint(plane, p) == PlaneIntersectionType::Back) if (Vector3::Dot(plane.Normal, p) + plane.D < Plane::DistanceEpsilon)
return ContainmentType::Disjoint; return ContainmentType::Disjoint;
p = box.Maximum; p = box.Maximum;
@@ -1292,7 +1283,7 @@ ContainmentType CollisionsHelper::FrustumContainsBox(const BoundingFrustum& frus
p.Y = box.Minimum.Y; p.Y = box.Minimum.Y;
if (plane.Normal.Z >= 0) if (plane.Normal.Z >= 0)
p.Z = box.Minimum.Z; p.Z = box.Minimum.Z;
if (PlaneIntersectsPoint(plane, p) == PlaneIntersectionType::Back) if (Vector3::Dot(plane.Normal, p) + plane.D < Plane::DistanceEpsilon)
result = ContainmentType::Intersects; result = ContainmentType::Intersects;
} }
return result; return result;