Skip to content

Commit 8bf0bb8

Browse files
committed
improved line trace and box primitive
1 parent a340766 commit 8bf0bb8

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

include/omath/3d_primitives/box.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace omath::primitives
1212
{
1313
[[nodiscard]]
14-
std::array<Triangle<Vector3<float>>, 8> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
14+
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
1515
const Vector3<float>& dirForward, const Vector3<float>& dirRight,
1616
float ratio = 4.f);
1717
}

include/omath/collision/line_tracer.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace omath::collision
1313
public:
1414
Vector3<float> start;
1515
Vector3<float> end;
16-
16+
bool infinite_length = false;
1717
[[nodiscard]]
1818
Vector3<float> DirectionVector() const;
1919

source/3d_primitives/box.cpp

+35-18
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,53 @@
44
#include "omath/3d_primitives/box.hpp"
55

66

7-
87
namespace omath::primitives
98
{
10-
11-
std::array<Triangle<Vector3<float>>, 8> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
12-
const Vector3<float>& dirForward,
13-
const Vector3<float>& dirRight,
14-
const float ratio)
9+
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
10+
const Vector3<float>& dirForward,
11+
const Vector3<float>& dirRight,
12+
const float ratio)
1513
{
1614
const auto height = top.DistTo(bottom);
1715
const auto sideSize = height / ratio;
1816

19-
std::array<Vector3<float>, 8> points;
17+
// corner layout (0‑3 bottom, 4‑7 top)
18+
std::array<Vector3<float>, 8> p;
19+
p[0] = bottom + (dirForward + dirRight) * sideSize; // front‑right‑bottom
20+
p[1] = bottom + (dirForward - dirRight) * sideSize; // front‑left‑bottom
21+
p[2] = bottom + (-dirForward + dirRight) * sideSize; // back‑right‑bottom
22+
p[3] = bottom + (-dirForward - dirRight) * sideSize; // back‑left‑bottom
23+
p[4] = top + (dirForward + dirRight) * sideSize; // front‑right‑top
24+
p[5] = top + (dirForward - dirRight) * sideSize; // front‑left‑top
25+
p[6] = top + (-dirForward + dirRight) * sideSize; // back‑right‑top
26+
p[7] = top + (-dirForward - dirRight) * sideSize; // back‑left‑top
27+
28+
std::array<Triangle<Vector3<float>>, 12> poly;
2029

21-
points[0] = bottom + (dirForward + dirRight) * sideSize;
22-
points[1] = bottom + (dirForward - dirRight) * sideSize;
30+
// bottom face (+Y up ⇒ wind CW when viewed from above)
31+
poly[0] = {p[0], p[2], p[3]};
32+
poly[1] = {p[0], p[3], p[1]};
2333

24-
points[2] = bottom + (-dirForward + dirRight) * sideSize;
25-
points[3] = bottom + (-dirForward - dirRight) * sideSize;
34+
// top face
35+
poly[2] = {p[4], p[7], p[6]};
36+
poly[3] = {p[4], p[5], p[7]};
2637

27-
points[4] = top + (dirForward + dirRight) * sideSize;
28-
points[5] = top + (dirForward - dirRight) * sideSize;
38+
// front face
39+
poly[4] = {p[0], p[5], p[1]};
40+
poly[5] = {p[0], p[4], p[5]};
2941

30-
points[6] = top + (-dirForward + dirRight) * sideSize;
31-
points[7] = top + (-dirForward - dirRight) * sideSize;
42+
// right face
43+
poly[6] = {p[0], p[6], p[2]};
44+
poly[7] = {p[0], p[4], p[6]};
3245

46+
// back face
47+
poly[8] = {p[2], p[7], p[3]};
48+
poly[9] = {p[2], p[6], p[7]};
3349

34-
std::array<Triangle<Vector3<float>>, 8> polygons;
50+
// left face
51+
poly[10] = {p[1], p[7], p[5]};
52+
poly[11] = {p[1], p[3], p[7]};
3553

36-
polygons[0] = {points[0], points[2], points[3]};
37-
return polygons;
54+
return poly;
3855
}
3956
}

source/collision/line_tracer.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ namespace omath::collision
5454
const auto tHit = sideB.Dot(q) * invDet;
5555

5656

57-
if (tHit <= kEpsilon)
57+
if (ray.infinite_length)
58+
{
59+
if (tHit <= kEpsilon)
60+
return ray.end;
61+
}
62+
else if (tHit < 0.0f || tHit > 1.0f)
5863
return ray.end;
5964

6065
return ray.start + rayDir * tHit;

0 commit comments

Comments
 (0)