Daniele Bartolini 10 лет назад
Родитель
Сommit
3995bc0db6
2 измененных файлов с 16 добавлено и 8 удалено
  1. 10 7
      src/core/math/intersection.cpp
  2. 6 1
      src/core/math/intersection.h

+ 10 - 7
src/core/math/intersection.cpp

@@ -149,20 +149,23 @@ float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix
 	return tmin;
 }
 
-bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip)
+bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip)
 {
-	const Vector3 n1 = p1.n;
-	const Vector3 n2 = p2.n;
-	const Vector3 n3 = p3.n;
-	const float den  = -dot(cross(n1, n2), n3);
+	const Vector3 na = a.n;
+	const Vector3 nb = b.n;
+	const Vector3 nc = c.n;
+	const float den  = -dot(cross(na, nb), nc);
 
 	if (fequal(den, 0.0f))
 		return false;
 
 	const float inv_den = 1.0f / den;
 
-	Vector3 res = p1.d * cross(n2, n3) + p2.d * cross(n3, n1) + p3.d * cross(n1, n2);
-	ip = res * inv_den;
+	const Vector3 nbnc = a.d * cross(nb, nc);
+	const Vector3 ncna = b.d * cross(nc, na);
+	const Vector3 nanb = c.d * cross(na, nb);
+
+	ip = (nbnc + ncna + nanb) * inv_den;
 
 	return true;
 }

+ 6 - 1
src/core/math/intersection.h

@@ -28,8 +28,13 @@ float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sph
 /// bounding box (tm, half_extents) or -1.0 if no intersection.
 float ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const Vector3& half_extents);
 
-bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip);
+/// Returns whether the planes @a a, @a b and @a c intersects and if so fills @a ip with the intersection point.
+bool plane_3_intersection(const Plane& a, const Plane& b, const Plane& c, Vector3& ip);
+
+/// Returns whether the frustum @a f and the sphere @a s intersects.
 bool frustum_sphere_intersection(const Frustum& f, const Sphere& s);
+
+/// Returns whether the frustum @a f and the AABB @a b intersects.
 bool frustum_box_intersection(const Frustum& f, const AABB& b);
 
 /// @}