mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improved line trace and box primitive
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
namespace omath::primitives
|
||||
{
|
||||
[[nodiscard]]
|
||||
std::array<Triangle<Vector3<float>>, 8> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dirForward, const Vector3<float>& dirRight,
|
||||
float ratio = 4.f);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace omath::collision
|
||||
public:
|
||||
Vector3<float> start;
|
||||
Vector3<float> end;
|
||||
|
||||
bool infinite_length = false;
|
||||
[[nodiscard]]
|
||||
Vector3<float> DirectionVector() const;
|
||||
|
||||
|
||||
@@ -4,36 +4,53 @@
|
||||
#include "omath/3d_primitives/box.hpp"
|
||||
|
||||
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
|
||||
std::array<Triangle<Vector3<float>>, 8> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dirForward,
|
||||
const Vector3<float>& dirRight,
|
||||
const float ratio)
|
||||
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dirForward,
|
||||
const Vector3<float>& dirRight,
|
||||
const float ratio)
|
||||
{
|
||||
const auto height = top.DistTo(bottom);
|
||||
const auto sideSize = height / ratio;
|
||||
|
||||
std::array<Vector3<float>, 8> points;
|
||||
// corner layout (0‑3 bottom, 4‑7 top)
|
||||
std::array<Vector3<float>, 8> p;
|
||||
p[0] = bottom + (dirForward + dirRight) * sideSize; // front‑right‑bottom
|
||||
p[1] = bottom + (dirForward - dirRight) * sideSize; // front‑left‑bottom
|
||||
p[2] = bottom + (-dirForward + dirRight) * sideSize; // back‑right‑bottom
|
||||
p[3] = bottom + (-dirForward - dirRight) * sideSize; // back‑left‑bottom
|
||||
p[4] = top + (dirForward + dirRight) * sideSize; // front‑right‑top
|
||||
p[5] = top + (dirForward - dirRight) * sideSize; // front‑left‑top
|
||||
p[6] = top + (-dirForward + dirRight) * sideSize; // back‑right‑top
|
||||
p[7] = top + (-dirForward - dirRight) * sideSize; // back‑left‑top
|
||||
|
||||
points[0] = bottom + (dirForward + dirRight) * sideSize;
|
||||
points[1] = bottom + (dirForward - dirRight) * sideSize;
|
||||
std::array<Triangle<Vector3<float>>, 12> poly;
|
||||
|
||||
points[2] = bottom + (-dirForward + dirRight) * sideSize;
|
||||
points[3] = bottom + (-dirForward - dirRight) * sideSize;
|
||||
// bottom face (+Y up ⇒ wind CW when viewed from above)
|
||||
poly[0] = {p[0], p[2], p[3]};
|
||||
poly[1] = {p[0], p[3], p[1]};
|
||||
|
||||
points[4] = top + (dirForward + dirRight) * sideSize;
|
||||
points[5] = top + (dirForward - dirRight) * sideSize;
|
||||
// top face
|
||||
poly[2] = {p[4], p[7], p[6]};
|
||||
poly[3] = {p[4], p[5], p[7]};
|
||||
|
||||
points[6] = top + (-dirForward + dirRight) * sideSize;
|
||||
points[7] = top + (-dirForward - dirRight) * sideSize;
|
||||
// front face
|
||||
poly[4] = {p[0], p[5], p[1]};
|
||||
poly[5] = {p[0], p[4], p[5]};
|
||||
|
||||
// right face
|
||||
poly[6] = {p[0], p[6], p[2]};
|
||||
poly[7] = {p[0], p[4], p[6]};
|
||||
|
||||
std::array<Triangle<Vector3<float>>, 8> polygons;
|
||||
// back face
|
||||
poly[8] = {p[2], p[7], p[3]};
|
||||
poly[9] = {p[2], p[6], p[7]};
|
||||
|
||||
polygons[0] = {points[0], points[2], points[3]};
|
||||
return polygons;
|
||||
// left face
|
||||
poly[10] = {p[1], p[7], p[5]};
|
||||
poly[11] = {p[1], p[3], p[7]};
|
||||
|
||||
return poly;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,12 @@ namespace omath::collision
|
||||
const auto tHit = sideB.Dot(q) * invDet;
|
||||
|
||||
|
||||
if (tHit <= kEpsilon)
|
||||
if (ray.infinite_length)
|
||||
{
|
||||
if (tHit <= kEpsilon)
|
||||
return ray.end;
|
||||
}
|
||||
else if (tHit < 0.0f || tHit > 1.0f)
|
||||
return ray.end;
|
||||
|
||||
return ray.start + rayDir * tHit;
|
||||
|
||||
Reference in New Issue
Block a user