mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Merge pull request #38 from orange-cpp/u/orange-cpp/3d-primitives
U/orange cpp/3d primitives
This commit is contained in:
@@ -13,13 +13,12 @@ option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types
|
|||||||
option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF)
|
option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF)
|
||||||
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
|
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
|
||||||
option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON)
|
option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON)
|
||||||
|
option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" ON)
|
||||||
|
|
||||||
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
||||||
add_library(omath SHARED source/matrix.cpp)
|
add_library(omath SHARED source/matrix.cpp)
|
||||||
else()
|
else()
|
||||||
add_library(omath STATIC source/matrix.cpp
|
add_library(omath STATIC source/matrix.cpp)
|
||||||
source/matrix.cpp)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}")
|
message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}")
|
||||||
@@ -55,11 +54,15 @@ endif()
|
|||||||
set_target_properties(omath PROPERTIES
|
set_target_properties(omath PROPERTIES
|
||||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||||
UNITY_BUILD ON
|
|
||||||
UNITY_BUILD_BATCH_SIZE 20
|
|
||||||
CXX_STANDARD 23
|
CXX_STANDARD 23
|
||||||
CXX_STANDARD_REQUIRED ON)
|
CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
if (OMATH_USE_UNITY_BUILD)
|
||||||
|
set_target_properties(omath PROPERTIES
|
||||||
|
UNITY_BUILD ON
|
||||||
|
UNITY_BUILD_BATCH_SIZE 20)
|
||||||
|
endif()
|
||||||
|
|
||||||
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
|
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
|
||||||
set_target_properties(omath PROPERTIES
|
set_target_properties(omath PROPERTIES
|
||||||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
|
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
|
||||||
|
|||||||
@@ -91,7 +91,8 @@ With `omath/projection` module you can achieve simple ESP hack for powered by So
|
|||||||

|

|
||||||
Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2!
|
Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2!
|
||||||

|

|
||||||
|
Or create simple trigger bot with embeded traceline from omath::collision::LineTrace
|
||||||
|

|
||||||
Or even advanced projectile aimbot
|
Or even advanced projectile aimbot
|
||||||
[Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E)
|
[Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E)
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
17
include/omath/3d_primitives/box.hpp
Normal file
17
include/omath/3d_primitives/box.hpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 4/18/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <array>
|
||||||
|
#include "omath/triangle.hpp"
|
||||||
|
#include "omath/vector3.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::primitives
|
||||||
|
{
|
||||||
|
[[nodiscard]]
|
||||||
|
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:
|
public:
|
||||||
Vector3<float> start;
|
Vector3<float> start;
|
||||||
Vector3<float> end;
|
Vector3<float> end;
|
||||||
|
bool infinite_length = false;
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
Vector3<float> DirectionVector() const;
|
Vector3<float> DirectionVector() const;
|
||||||
|
|
||||||
|
|||||||
1
source/3d_primitives/CMakeLists.txt
Normal file
1
source/3d_primitives/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target_sources(omath PRIVATE box.cpp)
|
||||||
56
source/3d_primitives/box.cpp
Normal file
56
source/3d_primitives/box.cpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 4/18/2025.
|
||||||
|
//
|
||||||
|
#include "omath/3d_primitives/box.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::primitives
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
std::array<Triangle<Vector3<float>>, 12> poly;
|
||||||
|
|
||||||
|
// 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]};
|
||||||
|
|
||||||
|
// top face
|
||||||
|
poly[2] = {p[4], p[7], p[6]};
|
||||||
|
poly[3] = {p[4], p[5], p[7]};
|
||||||
|
|
||||||
|
// 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]};
|
||||||
|
|
||||||
|
// back face
|
||||||
|
poly[8] = {p[2], p[7], p[3]};
|
||||||
|
poly[9] = {p[2], p[6], p[7]};
|
||||||
|
|
||||||
|
// left face
|
||||||
|
poly[10] = {p[1], p[7], p[5]};
|
||||||
|
poly[11] = {p[1], p[3], p[7]};
|
||||||
|
|
||||||
|
return poly;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,4 +7,5 @@ add_subdirectory(projectile_prediction)
|
|||||||
add_subdirectory(pathfinding)
|
add_subdirectory(pathfinding)
|
||||||
add_subdirectory(projection)
|
add_subdirectory(projection)
|
||||||
add_subdirectory(collision)
|
add_subdirectory(collision)
|
||||||
add_subdirectory(engines)
|
add_subdirectory(engines)
|
||||||
|
add_subdirectory(3d_primitives)
|
||||||
@@ -54,7 +54,12 @@ namespace omath::collision
|
|||||||
const auto tHit = sideB.Dot(q) * invDet;
|
const auto tHit = sideB.Dot(q) * invDet;
|
||||||
|
|
||||||
|
|
||||||
if (tHit <= kEpsilon)
|
if (ray.infinite_length)
|
||||||
|
{
|
||||||
|
if (tHit <= kEpsilon)
|
||||||
|
return ray.end;
|
||||||
|
}
|
||||||
|
else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon)
|
||||||
return ray.end;
|
return ray.end;
|
||||||
|
|
||||||
return ray.start + rayDir * tHit;
|
return ray.start + rayDir * tHit;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ add_executable(unit-tests
|
|||||||
general/unit_test_view_angles.cpp
|
general/unit_test_view_angles.cpp
|
||||||
general/unit_test_angle.cpp
|
general/unit_test_angle.cpp
|
||||||
general/unit_test_triangle.cpp
|
general/unit_test_triangle.cpp
|
||||||
|
general/unit_test_box_primitive.cpp
|
||||||
|
|
||||||
engines/unit_test_open_gl.cpp
|
engines/unit_test_open_gl.cpp
|
||||||
engines/unit_test_unity_engine.cpp
|
engines/unit_test_unity_engine.cpp
|
||||||
|
|||||||
6
tests/general/unit_test_box_primitive.cpp
Normal file
6
tests/general/unit_test_box_primitive.cpp
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 4/18/2025.
|
||||||
|
//
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <omath/3d_primitives/box.hpp>
|
||||||
|
|
||||||
@@ -1,80 +1,121 @@
|
|||||||
|
//
|
||||||
|
// Revised unit‑test suite for LineTracer (segment‑based Möller–Trumbore)
|
||||||
|
// Pure ASCII: avoids non‑standard characters that MSVC rejects.
|
||||||
|
//
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "omath/collision/line_tracer.hpp"
|
#include "omath/collision/line_tracer.hpp"
|
||||||
#include "omath/triangle.hpp"
|
#include "omath/triangle.hpp"
|
||||||
#include "omath/vector3.hpp"
|
#include "omath/vector3.hpp"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
using namespace omath;
|
using namespace omath;
|
||||||
using namespace omath::collision;
|
using namespace omath::collision;
|
||||||
|
|
||||||
class LineTracerTest : public ::testing::Test
|
using Vec3 = Vector3<float>;
|
||||||
|
|
||||||
|
namespace
|
||||||
{
|
{
|
||||||
protected:
|
|
||||||
// Set up common variables for use in each test
|
|
||||||
Vector3<float> vertex1{0.0f, 0.0f, 0.0f};
|
|
||||||
Vector3<float> vertex2{1.0f, 0.0f, 0.0f};
|
|
||||||
Vector3<float> vertex3{0.0f, 1.0f, 0.0f};
|
|
||||||
Triangle<Vector3<float>> triangle{vertex1, vertex2, vertex3};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Test that a ray intersecting the triangle returns false for CanTraceLine
|
// -----------------------------------------------------------------------------
|
||||||
TEST_F(LineTracerTest, RayIntersectsTriangle)
|
// Constants & helpers
|
||||||
{
|
// -----------------------------------------------------------------------------
|
||||||
constexpr Ray ray{{0.3f, 0.3f, -1.0f}, {0.3f, 0.3f, 1.0f}};
|
constexpr float kTol = 1e-5f;
|
||||||
EXPECT_FALSE(LineTracer::CanTraceLine(ray, triangle));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test that a ray parallel to the triangle plane returns true for CanTraceLine
|
bool VecEqual(const Vec3& a, const Vec3& b, float tol = kTol)
|
||||||
TEST_F(LineTracerTest, RayParallelToTriangle)
|
{
|
||||||
{
|
return std::fabs(a.x - b.x) < tol &&
|
||||||
constexpr Ray ray{{0.3f, 0.3f, 1.0f}, {0.3f, 0.3f, 2.0f}};
|
std::fabs(a.y - b.y) < tol &&
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
std::fabs(a.z - b.z) < tol;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that a ray starting inside the triangle but pointing away returns true
|
// -----------------------------------------------------------------------------
|
||||||
TEST_F(LineTracerTest, RayStartsInTriangleButDoesNotIntersect)
|
// Fixture with one canonical right‑angled triangle in the XY plane.
|
||||||
{
|
// -----------------------------------------------------------------------------
|
||||||
constexpr Ray ray{{0.3f, 0.3f, 0.0f}, {0.3f, 0.3f, -1.0f}};
|
class LineTracerFixture : public ::testing::Test
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
{
|
||||||
}
|
protected:
|
||||||
|
LineTracerFixture() :
|
||||||
|
triangle({0.f, 0.f, 0.f}, {1.f, 0.f, 0.f}, {0.f, 1.f, 0.f})
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// Test that a ray not intersecting the triangle plane returns true
|
Triangle<Vec3> triangle;
|
||||||
TEST_F(LineTracerTest, RayMissesTriangle)
|
|
||||||
{
|
|
||||||
constexpr Ray ray{{2.0f, 2.0f, -1.0f}, {2.0f, 2.0f, 1.0f}};
|
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test that a ray lying exactly in the plane of the triangle without intersecting returns true
|
|
||||||
TEST_F(LineTracerTest, RayInPlaneNotIntersecting)
|
|
||||||
{
|
|
||||||
constexpr Ray ray{{-1.0f, -1.0f, 0.0f}, {1.5f, 1.5f, 0.0f}};
|
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TEST_F(LineTracerTest, RayIntersectsVertex)
|
|
||||||
{
|
|
||||||
const Ray ray{{-1.0f, -1.0f, -1.0f}, vertex1}; // Intersecting at vertex1
|
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(LineTracerTest, RayIntersectsEdge)
|
|
||||||
{
|
|
||||||
constexpr Ray ray{{-1.0f, 0.0f, -1.0f}, {0.5f, 0.0f, 0.0f}};
|
|
||||||
// Intersecting on the edge between vertex1 and vertex2
|
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, triangle));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(LineTracerTest, TriangleFarBeyondRayEndPoint)
|
|
||||||
{
|
|
||||||
// Define a ray with a short length
|
|
||||||
constexpr Ray ray{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}};
|
|
||||||
|
|
||||||
// Define a triangle far beyond the ray's endpoint
|
|
||||||
constexpr Triangle<Vector3<float>> distantTriangle{
|
|
||||||
{1000.0f, 1000.0f, 1000.0f}, {1001.0f, 1000.0f, 1000.0f}, {1000.0f, 1001.0f, 1000.0f}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Expect true because the ray ends long before it could reach the distant triangle
|
// -----------------------------------------------------------------------------
|
||||||
EXPECT_TRUE(LineTracer::CanTraceLine(ray, distantTriangle));
|
// Data‑driven tests for CanTraceLine
|
||||||
}
|
// -----------------------------------------------------------------------------
|
||||||
|
struct TraceCase
|
||||||
|
{
|
||||||
|
Ray ray;
|
||||||
|
bool expected_clear; // true => segment does NOT hit the triangle
|
||||||
|
};
|
||||||
|
|
||||||
|
class CanTraceLineParam : public LineTracerFixture,
|
||||||
|
public ::testing::WithParamInterface<TraceCase>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_P(CanTraceLineParam, VariousRays)
|
||||||
|
{
|
||||||
|
const auto& p = GetParam();
|
||||||
|
EXPECT_EQ(LineTracer::CanTraceLine(p.ray, triangle), p.expected_clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_SUITE_P(
|
||||||
|
BasicScenarios,
|
||||||
|
CanTraceLineParam,
|
||||||
|
::testing::Values(
|
||||||
|
TraceCase{Ray{{ 0.3f, 0.3f, -1.f},{ 0.3f, 0.3f, 1.f}}, false}, // hit through centre
|
||||||
|
TraceCase{Ray{{ 0.3f, 0.3f, 1.f},{ 0.3f, 0.3f, 2.f}}, true}, // parallel above
|
||||||
|
TraceCase{Ray{{ 0.3f, 0.3f, 0.f},{ 0.3f, 0.3f,-1.f}}, true}, // starts inside, goes away
|
||||||
|
TraceCase{Ray{{ 2.0f, 2.0f, -1.f},{ 2.0f, 2.0f, 1.f}}, true}, // misses entirely
|
||||||
|
TraceCase{Ray{{-1.0f,-1.0f, 0.f},{ 1.5f, 1.5f, 0.f}},true}, // lies in plane, outside tri
|
||||||
|
TraceCase{Ray{{-1.0f,-1.0f, -1.f},{ 0.0f, 0.0f, 0.f}}, true}, // endpoint on vertex
|
||||||
|
TraceCase{Ray{{-1.0f, 0.0f, -1.f},{ 0.5f, 0.0f, 0.f}}, true} // endpoint on edge
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Validate that the reported hit point is correct for a genuine intersection.
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
TEST_F(LineTracerFixture, HitPointCorrect)
|
||||||
|
{
|
||||||
|
constexpr Ray ray{{0.3f, 0.3f, -1.f}, {0.3f, 0.3f, 1.f}};
|
||||||
|
constexpr Vec3 expected{0.3f, 0.3f, 0.f};
|
||||||
|
|
||||||
|
const Vec3 hit = LineTracer::GetRayHitPoint(ray, triangle);
|
||||||
|
ASSERT_FALSE(VecEqual(hit, ray.end));
|
||||||
|
EXPECT_TRUE(VecEqual(hit, expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Triangle far beyond the ray should not block.
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
TEST_F(LineTracerFixture, DistantTriangleClear)
|
||||||
|
{
|
||||||
|
constexpr Ray short_ray{{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}};
|
||||||
|
constexpr Triangle<Vec3> distant{{1000.f, 1000.f, 1000.f},
|
||||||
|
{1001.f, 1000.f, 1000.f},
|
||||||
|
{1000.f, 1001.f, 1000.f}};
|
||||||
|
|
||||||
|
EXPECT_TRUE(LineTracer::CanTraceLine(short_ray, distant));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(LineTracerTraceRayEdge, CantHit)
|
||||||
|
{
|
||||||
|
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
|
||||||
|
|
||||||
|
constexpr Ray ray{{}, {1.0, 0, 0}, false};
|
||||||
|
|
||||||
|
EXPECT_TRUE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
|
||||||
|
}
|
||||||
|
TEST(LineTracerTraceRayEdge, CanHit)
|
||||||
|
{
|
||||||
|
constexpr omath::Triangle<Vector3<float>> triangle{{2, 0, 0}, {2, 2, 0}, {2, 2, 2}};
|
||||||
|
|
||||||
|
constexpr Ray ray{{}, {2.1, 0, 0}, false};
|
||||||
|
auto endPoint = omath::collision::LineTracer::GetRayHitPoint(ray, triangle);
|
||||||
|
EXPECT_FALSE(omath::collision::LineTracer::CanTraceLine(ray, triangle));
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user