improved line trace and box primitive

This commit is contained in:
2025-04-18 13:56:08 +03:00
parent a340766348
commit 8bf0bb8e0d
4 changed files with 43 additions and 21 deletions

View File

@@ -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 (03 bottom, 47 top)
std::array<Vector3<float>, 8> p;
p[0] = bottom + (dirForward + dirRight) * sideSize; // frontrightbottom
p[1] = bottom + (dirForward - dirRight) * sideSize; // frontleftbottom
p[2] = bottom + (-dirForward + dirRight) * sideSize; // backrightbottom
p[3] = bottom + (-dirForward - dirRight) * sideSize; // backleftbottom
p[4] = top + (dirForward + dirRight) * sideSize; // frontrighttop
p[5] = top + (dirForward - dirRight) * sideSize; // frontlefttop
p[6] = top + (-dirForward + dirRight) * sideSize; // backrighttop
p[7] = top + (-dirForward - dirRight) * sideSize; // backlefttop
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;
}
}