mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-19 08:23:28 +00:00
Compare commits
3 Commits
29f3e2565d
...
feature/ph
| Author | SHA1 | Date | |
|---|---|---|---|
| 69e9735063 | |||
| 35b8510cf4 | |||
| ba662e44e2 |
@@ -34,6 +34,8 @@ option(OMATH_ENABLE_FORCE_INLINE
|
|||||||
|
|
||||||
option(OMATH_ENABLE_LUA
|
option(OMATH_ENABLE_LUA
|
||||||
"omath bindings for lua" OFF)
|
"omath bindings for lua" OFF)
|
||||||
|
option(OMATH_ENABLE_PHYSX
|
||||||
|
"PhysX-backed collider implementations" OFF)
|
||||||
if(VCPKG_MANIFEST_FEATURES)
|
if(VCPKG_MANIFEST_FEATURES)
|
||||||
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
|
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
|
||||||
if(omath_feature STREQUAL "imgui")
|
if(omath_feature STREQUAL "imgui")
|
||||||
@@ -48,6 +50,8 @@ if(VCPKG_MANIFEST_FEATURES)
|
|||||||
set(OMATH_BUILD_EXAMPLES ON)
|
set(OMATH_BUILD_EXAMPLES ON)
|
||||||
elseif(omath_feature STREQUAL "lua")
|
elseif(omath_feature STREQUAL "lua")
|
||||||
set(OMATH_ENABLE_LUA ON)
|
set(OMATH_ENABLE_LUA ON)
|
||||||
|
elseif(omath_feature STREQUAL "physx")
|
||||||
|
set(OMATH_ENABLE_PHYSX ON)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
endforeach()
|
endforeach()
|
||||||
@@ -78,6 +82,7 @@ if(${PROJECT_IS_TOP_LEVEL})
|
|||||||
message(STATUS "[${PROJECT_NAME}]: Coverage feature status ${OMATH_ENABLE_COVERAGE}")
|
message(STATUS "[${PROJECT_NAME}]: Coverage feature status ${OMATH_ENABLE_COVERAGE}")
|
||||||
message(STATUS "[${PROJECT_NAME}]: Valgrind feature status ${OMATH_ENABLE_VALGRIND}")
|
message(STATUS "[${PROJECT_NAME}]: Valgrind feature status ${OMATH_ENABLE_VALGRIND}")
|
||||||
message(STATUS "[${PROJECT_NAME}]: Lua feature status ${OMATH_ENABLE_LUA}")
|
message(STATUS "[${PROJECT_NAME}]: Lua feature status ${OMATH_ENABLE_LUA}")
|
||||||
|
message(STATUS "[${PROJECT_NAME}]: PhysX feature status ${OMATH_ENABLE_PHYSX}")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
||||||
@@ -100,6 +105,13 @@ if (OMATH_ENABLE_LUA)
|
|||||||
target_include_directories(${PROJECT_NAME} PRIVATE ${SOL2_INCLUDE_DIRS})
|
target_include_directories(${PROJECT_NAME} PRIVATE ${SOL2_INCLUDE_DIRS})
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
if (OMATH_ENABLE_PHYSX)
|
||||||
|
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_ENABLE_PHYSX)
|
||||||
|
|
||||||
|
find_package(unofficial-omniverse-physx-sdk CONFIG REQUIRED)
|
||||||
|
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::omniverse-physx-sdk::sdk)
|
||||||
|
endif ()
|
||||||
|
|
||||||
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
||||||
|
|
||||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION}")
|
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_VERSION="${PROJECT_VERSION}")
|
||||||
|
|||||||
@@ -145,7 +145,7 @@
|
|||||||
"hidden": true,
|
"hidden": true,
|
||||||
"inherits": ["linux-base", "vcpkg-base"],
|
"inherits": ["linux-base", "vcpkg-base"],
|
||||||
"cacheVariables": {
|
"cacheVariables": {
|
||||||
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;lua"
|
"VCPKG_MANIFEST_FEATURES": "tests;imgui;avx2;lua;physx"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
59
include/omath/collision/physx_box_collider.hpp
Normal file
59
include/omath/collision/physx_box_collider.hpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
//
|
||||||
|
// Created by orange-cpp
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef OMATH_ENABLE_PHYSX
|
||||||
|
|
||||||
|
#include "collider_interface.hpp"
|
||||||
|
#include <PxPhysicsAPI.h>
|
||||||
|
|
||||||
|
namespace omath::collision
|
||||||
|
{
|
||||||
|
/// Axis-aligned box collider backed by PhysX PxBoxGeometry.
|
||||||
|
/// Half-extents are stored in PhysX convention (positive values along each axis).
|
||||||
|
class PhysXBoxCollider final : public ColliderInterface<Vector3<float>>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @param half_extents Half-widths along X, Y and Z axes (all must be > 0).
|
||||||
|
/// @param origin World-space centre of the box.
|
||||||
|
explicit PhysXBoxCollider(const VectorType& half_extents, const VectorType& origin = {})
|
||||||
|
: m_geometry(physx::PxVec3(half_extents.x, half_extents.y, half_extents.z))
|
||||||
|
, m_origin(origin)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Support function: returns the world-space point on the box furthest in @p direction.
|
||||||
|
/// For a box, the furthest point along d is origin + (sign(d.x)*hx, sign(d.y)*hy, sign(d.z)*hz).
|
||||||
|
[[nodiscard]]
|
||||||
|
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||||
|
{
|
||||||
|
const auto& he = m_geometry.halfExtents;
|
||||||
|
return {
|
||||||
|
m_origin.x + (direction.x >= 0.f ? he.x : -he.x),
|
||||||
|
m_origin.y + (direction.y >= 0.f ? he.y : -he.y),
|
||||||
|
m_origin.z + (direction.z >= 0.f ? he.z : -he.z),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const VectorType& get_origin() const override { return m_origin; }
|
||||||
|
|
||||||
|
void set_origin(const VectorType& new_origin) override { m_origin = new_origin; }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const physx::PxBoxGeometry& get_geometry() const { return m_geometry; }
|
||||||
|
|
||||||
|
/// Update half-extents at runtime.
|
||||||
|
void set_half_extents(const VectorType& half_extents)
|
||||||
|
{
|
||||||
|
m_geometry = physx::PxBoxGeometry(physx::PxVec3(half_extents.x, half_extents.y, half_extents.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
physx::PxBoxGeometry m_geometry;
|
||||||
|
VectorType m_origin;
|
||||||
|
};
|
||||||
|
} // namespace omath::collision
|
||||||
|
|
||||||
|
#endif // OMATH_ENABLE_PHYSX
|
||||||
137
include/omath/collision/physx_rigid_body.hpp
Normal file
137
include/omath/collision/physx_rigid_body.hpp
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
//
|
||||||
|
// Created by orange-cpp
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef OMATH_ENABLE_PHYSX
|
||||||
|
|
||||||
|
#include "collider_interface.hpp"
|
||||||
|
#include "physx_world.hpp"
|
||||||
|
#include <PxPhysicsAPI.h>
|
||||||
|
#include <extensions/PxRigidBodyExt.h>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace omath::collision
|
||||||
|
{
|
||||||
|
/// Dynamic rigid body backed by a PhysX PxRigidDynamic actor.
|
||||||
|
/// Implements ColliderInterface so it can participate in both omath GJK
|
||||||
|
/// and PhysX simulation-based collision resolution.
|
||||||
|
///
|
||||||
|
/// Ownership: the actor is added to the world's scene on construction
|
||||||
|
/// and removed + released on destruction.
|
||||||
|
class PhysXRigidBody final : public ColliderInterface<Vector3<float>>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @param world PhysXWorld that owns the scene.
|
||||||
|
/// @param geometry Shape geometry (PxBoxGeometry, PxSphereGeometry, …).
|
||||||
|
/// @param origin Initial world-space position.
|
||||||
|
/// @param density Mass density used to compute mass and inertia.
|
||||||
|
PhysXRigidBody(PhysXWorld& world, const physx::PxGeometry& geometry,
|
||||||
|
const VectorType& origin = {}, float density = 1.f)
|
||||||
|
: m_world(world)
|
||||||
|
, m_geometry(geometry)
|
||||||
|
{
|
||||||
|
const physx::PxTransform pose(physx::PxVec3(origin.x, origin.y, origin.z));
|
||||||
|
m_actor = world.get_physics().createRigidDynamic(pose);
|
||||||
|
|
||||||
|
physx::PxShape* shape = world.get_physics().createShape(
|
||||||
|
geometry, world.get_default_material(), true);
|
||||||
|
m_actor->attachShape(*shape);
|
||||||
|
shape->release();
|
||||||
|
|
||||||
|
physx::PxRigidBodyExt::updateMassAndInertia(*m_actor, density);
|
||||||
|
world.get_scene().addActor(*m_actor);
|
||||||
|
}
|
||||||
|
|
||||||
|
~PhysXRigidBody() override
|
||||||
|
{
|
||||||
|
m_world.get_scene().removeActor(*m_actor);
|
||||||
|
m_actor->release();
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysXRigidBody(const PhysXRigidBody&) = delete;
|
||||||
|
PhysXRigidBody& operator=(const PhysXRigidBody&) = delete;
|
||||||
|
|
||||||
|
// ── ColliderInterface ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Support function — delegates to the stored geometry type so the body
|
||||||
|
/// can be used with omath GJK alongside the non-simulated colliders.
|
||||||
|
[[nodiscard]]
|
||||||
|
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||||
|
{
|
||||||
|
const VectorType o = get_origin();
|
||||||
|
switch (m_geometry.getType())
|
||||||
|
{
|
||||||
|
case physx::PxGeometryType::eBOX:
|
||||||
|
{
|
||||||
|
const auto& he = m_geometry.box().halfExtents;
|
||||||
|
return {
|
||||||
|
o.x + (direction.x >= 0.f ? he.x : -he.x),
|
||||||
|
o.y + (direction.y >= 0.f ? he.y : -he.y),
|
||||||
|
o.z + (direction.z >= 0.f ? he.z : -he.z),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case physx::PxGeometryType::eSPHERE:
|
||||||
|
{
|
||||||
|
const float r = m_geometry.sphere().radius;
|
||||||
|
const float len = std::sqrt(direction.x * direction.x +
|
||||||
|
direction.y * direction.y +
|
||||||
|
direction.z * direction.z);
|
||||||
|
if (len == 0.f)
|
||||||
|
return o;
|
||||||
|
const float inv = r / len;
|
||||||
|
return { o.x + direction.x * inv,
|
||||||
|
o.y + direction.y * inv,
|
||||||
|
o.z + direction.z * inv };
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return o; // unsupported geometry — return centre
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const VectorType& get_origin() const override
|
||||||
|
{
|
||||||
|
const auto& p = m_actor->getGlobalPose().p;
|
||||||
|
m_cached_origin = { p.x, p.y, p.z };
|
||||||
|
return m_cached_origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_origin(const VectorType& new_origin) override
|
||||||
|
{
|
||||||
|
physx::PxTransform pose = m_actor->getGlobalPose();
|
||||||
|
pose.p = physx::PxVec3(new_origin.x, new_origin.y, new_origin.z);
|
||||||
|
m_actor->setGlobalPose(pose);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── PhysX-specific API ───────────────────────────────────────────────
|
||||||
|
|
||||||
|
[[nodiscard]] physx::PxRigidDynamic& get_actor() { return *m_actor; }
|
||||||
|
[[nodiscard]] const physx::PxRigidDynamic& get_actor() const { return *m_actor; }
|
||||||
|
|
||||||
|
void set_linear_velocity(const VectorType& v)
|
||||||
|
{
|
||||||
|
m_actor->setLinearVelocity(physx::PxVec3(v.x, v.y, v.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
VectorType get_linear_velocity() const
|
||||||
|
{
|
||||||
|
const auto& v = m_actor->getLinearVelocity();
|
||||||
|
return { v.x, v.y, v.z };
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_kinematic(bool enabled)
|
||||||
|
{
|
||||||
|
m_actor->setRigidBodyFlag(physx::PxRigidBodyFlag::eKINEMATIC, enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
PhysXWorld& m_world;
|
||||||
|
physx::PxGeometryHolder m_geometry;
|
||||||
|
physx::PxRigidDynamic* m_actor{nullptr};
|
||||||
|
mutable VectorType m_cached_origin{};
|
||||||
|
};
|
||||||
|
} // namespace omath::collision
|
||||||
|
|
||||||
|
#endif // OMATH_ENABLE_PHYSX
|
||||||
64
include/omath/collision/physx_sphere_collider.hpp
Normal file
64
include/omath/collision/physx_sphere_collider.hpp
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
//
|
||||||
|
// Created by orange-cpp
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef OMATH_ENABLE_PHYSX
|
||||||
|
|
||||||
|
#include "collider_interface.hpp"
|
||||||
|
#include <PxPhysicsAPI.h>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace omath::collision
|
||||||
|
{
|
||||||
|
/// Sphere collider backed by PhysX PxSphereGeometry.
|
||||||
|
class PhysXSphereCollider final : public ColliderInterface<Vector3<float>>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @param radius Sphere radius (must be > 0).
|
||||||
|
/// @param origin World-space centre of the sphere.
|
||||||
|
explicit PhysXSphereCollider(float radius, const VectorType& origin = {})
|
||||||
|
: m_geometry(radius)
|
||||||
|
, m_origin(origin)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Support function: returns the world-space point on the sphere furthest in @p direction.
|
||||||
|
/// For a sphere that is simply origin + normalize(direction) * radius.
|
||||||
|
[[nodiscard]]
|
||||||
|
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
|
||||||
|
{
|
||||||
|
const float len = std::sqrt(direction.x * direction.x +
|
||||||
|
direction.y * direction.y +
|
||||||
|
direction.z * direction.z);
|
||||||
|
if (len == 0.f)
|
||||||
|
return m_origin;
|
||||||
|
|
||||||
|
const float inv = m_geometry.radius / len;
|
||||||
|
return {
|
||||||
|
m_origin.x + direction.x * inv,
|
||||||
|
m_origin.y + direction.y * inv,
|
||||||
|
m_origin.z + direction.z * inv,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const VectorType& get_origin() const override { return m_origin; }
|
||||||
|
|
||||||
|
void set_origin(const VectorType& new_origin) override { m_origin = new_origin; }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
const physx::PxSphereGeometry& get_geometry() const { return m_geometry; }
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
float get_radius() const { return m_geometry.radius; }
|
||||||
|
|
||||||
|
void set_radius(float radius) { m_geometry = physx::PxSphereGeometry(radius); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
physx::PxSphereGeometry m_geometry;
|
||||||
|
VectorType m_origin;
|
||||||
|
};
|
||||||
|
} // namespace omath::collision
|
||||||
|
|
||||||
|
#endif // OMATH_ENABLE_PHYSX
|
||||||
82
include/omath/collision/physx_world.hpp
Normal file
82
include/omath/collision/physx_world.hpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
//
|
||||||
|
// Created by orange-cpp
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef OMATH_ENABLE_PHYSX
|
||||||
|
|
||||||
|
#include <PxPhysicsAPI.h>
|
||||||
|
|
||||||
|
namespace omath::collision
|
||||||
|
{
|
||||||
|
/// RAII owner of a PhysX Foundation + Physics + Scene.
|
||||||
|
/// One world per simulation context; not copyable or movable.
|
||||||
|
class PhysXWorld final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit PhysXWorld(physx::PxVec3 gravity = {0.f, -9.81f, 0.f},
|
||||||
|
physx::PxU32 cpu_threads = 2)
|
||||||
|
{
|
||||||
|
m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_allocator, m_error_callback);
|
||||||
|
|
||||||
|
m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_foundation,
|
||||||
|
physx::PxTolerancesScale{});
|
||||||
|
|
||||||
|
physx::PxSceneDesc desc(m_physics->getTolerancesScale());
|
||||||
|
desc.gravity = gravity;
|
||||||
|
desc.cpuDispatcher = physx::PxDefaultCpuDispatcherCreate(cpu_threads);
|
||||||
|
m_dispatcher = static_cast<physx::PxDefaultCpuDispatcher*>(desc.cpuDispatcher);
|
||||||
|
desc.filterShader = physx::PxDefaultSimulationFilterShader;
|
||||||
|
|
||||||
|
m_scene = m_physics->createScene(desc);
|
||||||
|
|
||||||
|
// Default material: static friction 0.5, dynamic friction 0.5, restitution 0.
|
||||||
|
m_default_material = m_physics->createMaterial(0.5f, 0.5f, 0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
~PhysXWorld()
|
||||||
|
{
|
||||||
|
m_scene->release();
|
||||||
|
m_dispatcher->release();
|
||||||
|
m_default_material->release();
|
||||||
|
m_physics->release();
|
||||||
|
m_foundation->release();
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysXWorld(const PhysXWorld&) = delete;
|
||||||
|
PhysXWorld& operator=(const PhysXWorld&) = delete;
|
||||||
|
|
||||||
|
/// Advance the simulation by @p dt seconds and block until results are ready.
|
||||||
|
void step(float dt)
|
||||||
|
{
|
||||||
|
m_scene->simulate(dt);
|
||||||
|
m_scene->fetchResults(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] physx::PxPhysics& get_physics() { return *m_physics; }
|
||||||
|
[[nodiscard]] physx::PxScene& get_scene() { return *m_scene; }
|
||||||
|
[[nodiscard]] physx::PxMaterial& get_default_material() { return *m_default_material; }
|
||||||
|
|
||||||
|
/// Add an infinite static ground plane at y = @p y_level facing +Y.
|
||||||
|
physx::PxRigidStatic* add_ground_plane(float y_level = 0.f)
|
||||||
|
{
|
||||||
|
physx::PxRigidStatic* plane = PxCreatePlane(
|
||||||
|
*m_physics,
|
||||||
|
physx::PxPlane(0.f, 1.f, 0.f, -y_level),
|
||||||
|
*m_default_material);
|
||||||
|
m_scene->addActor(*plane);
|
||||||
|
return plane;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
physx::PxDefaultAllocator m_allocator{};
|
||||||
|
physx::PxDefaultErrorCallback m_error_callback{};
|
||||||
|
physx::PxFoundation* m_foundation{nullptr};
|
||||||
|
physx::PxPhysics* m_physics{nullptr};
|
||||||
|
physx::PxDefaultCpuDispatcher* m_dispatcher{nullptr};
|
||||||
|
physx::PxScene* m_scene{nullptr};
|
||||||
|
physx::PxMaterial* m_default_material{nullptr};
|
||||||
|
};
|
||||||
|
} // namespace omath::collision
|
||||||
|
|
||||||
|
#endif // OMATH_ENABLE_PHYSX
|
||||||
341
tests/general/unit_test_physx_colliders.cpp
Normal file
341
tests/general/unit_test_physx_colliders.cpp
Normal file
@@ -0,0 +1,341 @@
|
|||||||
|
//
|
||||||
|
// Created by orange-cpp
|
||||||
|
//
|
||||||
|
#ifdef OMATH_ENABLE_PHYSX
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <omath/collision/gjk_algorithm.hpp>
|
||||||
|
#include <omath/collision/physx_box_collider.hpp>
|
||||||
|
#include <omath/collision/physx_rigid_body.hpp>
|
||||||
|
#include <omath/collision/physx_sphere_collider.hpp>
|
||||||
|
#include <omath/collision/physx_world.hpp>
|
||||||
|
|
||||||
|
using namespace omath::collision;
|
||||||
|
using omath::Vector3;
|
||||||
|
|
||||||
|
// ─── PhysXBoxCollider ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, DefaultOriginIsZero)
|
||||||
|
{
|
||||||
|
PhysXBoxCollider box({1.f, 1.f, 1.f});
|
||||||
|
EXPECT_EQ(box.get_origin(), Vector3<float>(0.f, 0.f, 0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, SetAndGetOrigin)
|
||||||
|
{
|
||||||
|
PhysXBoxCollider box({1.f, 1.f, 1.f}, {3.f, 4.f, 5.f});
|
||||||
|
EXPECT_EQ(box.get_origin(), Vector3<float>(3.f, 4.f, 5.f));
|
||||||
|
|
||||||
|
box.set_origin({-1.f, 0.f, 2.f});
|
||||||
|
EXPECT_EQ(box.get_origin(), Vector3<float>(-1.f, 0.f, 2.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, FurthestPointPositiveDirection)
|
||||||
|
{
|
||||||
|
// Box centred at origin with half-extents (2, 3, 4).
|
||||||
|
// Direction (+x, +y, +z) → furthest corner is (+2, +3, +4).
|
||||||
|
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||||
|
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 2.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, 3.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 4.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, FurthestPointNegativeDirection)
|
||||||
|
{
|
||||||
|
// Direction (-x, -y, -z) → furthest corner is (-2, -3, -4).
|
||||||
|
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||||
|
const auto p = box.find_abs_furthest_vertex_position({-1.f, -1.f, -1.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, -2.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, -3.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, -4.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, FurthestPointMixedDirection)
|
||||||
|
{
|
||||||
|
// Direction (+x, -y, +z) → furthest corner is (+2, -3, +4).
|
||||||
|
PhysXBoxCollider box({2.f, 3.f, 4.f});
|
||||||
|
const auto p = box.find_abs_furthest_vertex_position({1.f, -1.f, 1.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 2.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, -3.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 4.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, FurthestPointWithNonZeroOrigin)
|
||||||
|
{
|
||||||
|
// Box at (10, 0, 0), half-extents (1, 1, 1). Direction +x → (11, 1, 1).
|
||||||
|
PhysXBoxCollider box({1.f, 1.f, 1.f}, {10.f, 0.f, 0.f});
|
||||||
|
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 11.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, 1.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 1.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxCollider, SetHalfExtentsUpdatesGeometry)
|
||||||
|
{
|
||||||
|
PhysXBoxCollider box({1.f, 1.f, 1.f});
|
||||||
|
box.set_half_extents({5.f, 6.f, 7.f});
|
||||||
|
|
||||||
|
const auto& he = box.get_geometry().halfExtents;
|
||||||
|
EXPECT_FLOAT_EQ(he.x, 5.f);
|
||||||
|
EXPECT_FLOAT_EQ(he.y, 6.f);
|
||||||
|
EXPECT_FLOAT_EQ(he.z, 7.f);
|
||||||
|
|
||||||
|
// Furthest vertex must reflect the new extents.
|
||||||
|
const auto p = box.find_abs_furthest_vertex_position({1.f, 1.f, 1.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 5.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, 6.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 7.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── PhysXSphereCollider ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, DefaultOriginIsZero)
|
||||||
|
{
|
||||||
|
PhysXSphereCollider sphere(1.f);
|
||||||
|
EXPECT_EQ(sphere.get_origin(), Vector3<float>(0.f, 0.f, 0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, SetAndGetOrigin)
|
||||||
|
{
|
||||||
|
PhysXSphereCollider sphere(1.f, {1.f, 2.f, 3.f});
|
||||||
|
EXPECT_EQ(sphere.get_origin(), Vector3<float>(1.f, 2.f, 3.f));
|
||||||
|
|
||||||
|
sphere.set_origin({-5.f, 0.f, 0.f});
|
||||||
|
EXPECT_EQ(sphere.get_origin(), Vector3<float>(-5.f, 0.f, 0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, FurthestPointAlongPureXAxis)
|
||||||
|
{
|
||||||
|
// Direction (1,0,0), radius 3 → furthest point is (3, 0, 0).
|
||||||
|
PhysXSphereCollider sphere(3.f);
|
||||||
|
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 3.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, 0.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, FurthestPointAlongDiagonal)
|
||||||
|
{
|
||||||
|
// Direction (1,1,0), radius 1 → furthest point at distance 1 from origin.
|
||||||
|
PhysXSphereCollider sphere(1.f);
|
||||||
|
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 1.f, 0.f});
|
||||||
|
const float dist = std::sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
|
||||||
|
EXPECT_NEAR(dist, 1.f, 1e-5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, FurthestPointWithNonZeroOrigin)
|
||||||
|
{
|
||||||
|
// Sphere at (5, 0, 0), radius 2. Direction +x → (7, 0, 0).
|
||||||
|
PhysXSphereCollider sphere(2.f, {5.f, 0.f, 0.f});
|
||||||
|
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 7.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.y, 0.f);
|
||||||
|
EXPECT_FLOAT_EQ(p.z, 0.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, ZeroDirectionReturnsOrigin)
|
||||||
|
{
|
||||||
|
PhysXSphereCollider sphere(5.f, {1.f, 2.f, 3.f});
|
||||||
|
const auto p = sphere.find_abs_furthest_vertex_position({0.f, 0.f, 0.f});
|
||||||
|
EXPECT_EQ(p, sphere.get_origin());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereCollider, SetRadiusUpdatesGeometry)
|
||||||
|
{
|
||||||
|
PhysXSphereCollider sphere(1.f);
|
||||||
|
sphere.set_radius(10.f);
|
||||||
|
EXPECT_FLOAT_EQ(sphere.get_radius(), 10.f);
|
||||||
|
|
||||||
|
// Furthest point along +x should now be at x = 10.
|
||||||
|
const auto p = sphere.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||||
|
EXPECT_FLOAT_EQ(p.x, 10.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── GJK: Box vs Box ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
using GjkBox = omath::collision::GjkAlgorithm<PhysXBoxCollider>;
|
||||||
|
using GjkSphere = omath::collision::GjkAlgorithm<PhysXSphereCollider>;
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, CollidingOverlap)
|
||||||
|
{
|
||||||
|
// Two unit boxes: A at origin, B shifted by 0.5 — clearly overlapping.
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {0.5f, 0.f, 0.f});
|
||||||
|
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, NotCollidingTouching)
|
||||||
|
{
|
||||||
|
// Boxes exactly touching on the +X face: A[-1,1] and B[1,3] along X.
|
||||||
|
// GJK treats boundary contact (Minkowski difference passes through origin) as non-collision.
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {2.f, 0.f, 0.f});
|
||||||
|
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, CollidingSlightOverlap)
|
||||||
|
{
|
||||||
|
// Boxes overlapping by 0.1 along X: A[-1,1] and B[0.9,2.9].
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {1.9f, 0.f, 0.f});
|
||||||
|
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, NotCollidingSeparated)
|
||||||
|
{
|
||||||
|
// Boxes separated by a gap: A[-1,1] and B[3,5] along X.
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {4.f, 0.f, 0.f});
|
||||||
|
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, CollidingSameOrigin)
|
||||||
|
{
|
||||||
|
// Same position — fully overlapping.
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f});
|
||||||
|
EXPECT_TRUE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, NotCollidingDiagonalSeparation)
|
||||||
|
{
|
||||||
|
// Boxes separated along a diagonal so no axis-aligned faces overlap.
|
||||||
|
const PhysXBoxCollider a({1.f, 1.f, 1.f});
|
||||||
|
const PhysXBoxCollider b({1.f, 1.f, 1.f}, {3.f, 3.f, 3.f});
|
||||||
|
EXPECT_FALSE(GjkBox::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXBoxGjk, DifferentSizesColliding)
|
||||||
|
{
|
||||||
|
// Large box vs small box inside it.
|
||||||
|
const PhysXBoxCollider large({5.f, 5.f, 5.f});
|
||||||
|
const PhysXBoxCollider small_box({1.f, 1.f, 1.f}, {2.f, 0.f, 0.f});
|
||||||
|
EXPECT_TRUE(GjkBox::is_collide(large, small_box));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── GJK: Sphere vs Sphere ───────────────────────────────────────────────────
|
||||||
|
|
||||||
|
TEST(PhysXSphereGjk, CollidingOverlap)
|
||||||
|
{
|
||||||
|
// Radii 1 each, centres 1 apart — overlapping.
|
||||||
|
const PhysXSphereCollider a(1.f);
|
||||||
|
const PhysXSphereCollider b(1.f, {1.f, 0.f, 0.f});
|
||||||
|
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereGjk, CollidingSameOrigin)
|
||||||
|
{
|
||||||
|
const PhysXSphereCollider a(1.f);
|
||||||
|
const PhysXSphereCollider b(1.f);
|
||||||
|
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereGjk, NotCollidingSeparated)
|
||||||
|
{
|
||||||
|
// Radii 1 each, centres 3 apart — gap of 1.
|
||||||
|
const PhysXSphereCollider a(1.f);
|
||||||
|
const PhysXSphereCollider b(1.f, {3.f, 0.f, 0.f});
|
||||||
|
EXPECT_FALSE(GjkSphere::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereGjk, DifferentRadiiColliding)
|
||||||
|
{
|
||||||
|
// r=2 and r=1, centres 2.5 apart — still overlapping.
|
||||||
|
const PhysXSphereCollider a(2.f);
|
||||||
|
const PhysXSphereCollider b(1.f, {2.5f, 0.f, 0.f});
|
||||||
|
EXPECT_TRUE(GjkSphere::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSphereGjk, DifferentRadiiNotColliding)
|
||||||
|
{
|
||||||
|
// r=1 and r=1, centres 5 apart — separated.
|
||||||
|
const PhysXSphereCollider a(1.f);
|
||||||
|
const PhysXSphereCollider b(1.f, {5.f, 0.f, 0.f});
|
||||||
|
EXPECT_FALSE(GjkSphere::is_collide(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── PhysX simulation-based collision resolution ─────────────────────────────
|
||||||
|
|
||||||
|
// Helper: step the world N times with a fixed dt.
|
||||||
|
static void step_n(omath::collision::PhysXWorld& world, int n, float dt = 1.f / 60.f)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
world.step(dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSimulation, BoxFallsAndStopsOnGround)
|
||||||
|
{
|
||||||
|
// A box dropped from y=5 should come to rest at y≈0.5 (half-extent) above the ground plane.
|
||||||
|
omath::collision::PhysXWorld world;
|
||||||
|
world.add_ground_plane(0.f);
|
||||||
|
|
||||||
|
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f),
|
||||||
|
{0.f, 5.f, 0.f});
|
||||||
|
|
||||||
|
step_n(world, 300); // ~5 simulated seconds
|
||||||
|
|
||||||
|
EXPECT_NEAR(box.get_origin().y, 0.5f, 0.05f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSimulation, SphereFallsAndStopsOnGround)
|
||||||
|
{
|
||||||
|
// A sphere of radius 1 dropped from y=5 should rest at y≈1.
|
||||||
|
omath::collision::PhysXWorld world;
|
||||||
|
world.add_ground_plane(0.f);
|
||||||
|
|
||||||
|
omath::collision::PhysXRigidBody sphere(world, physx::PxSphereGeometry(1.f),
|
||||||
|
{0.f, 5.f, 0.f});
|
||||||
|
|
||||||
|
step_n(world, 300);
|
||||||
|
|
||||||
|
EXPECT_NEAR(sphere.get_origin().y, 1.f, 0.05f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSimulation, TwoBoxesCollideSeparate)
|
||||||
|
{
|
||||||
|
// Two boxes launched toward each other — after collision they must be
|
||||||
|
// further apart than their combined half-extents (no overlap).
|
||||||
|
omath::collision::PhysXWorld world({0.f, 0.f, 0.f}); // no gravity
|
||||||
|
|
||||||
|
omath::collision::PhysXRigidBody left (world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f), {-3.f, 0.f, 0.f});
|
||||||
|
omath::collision::PhysXRigidBody right(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f), { 3.f, 0.f, 0.f});
|
||||||
|
|
||||||
|
left.set_linear_velocity({ 5.f, 0.f, 0.f});
|
||||||
|
right.set_linear_velocity({-5.f, 0.f, 0.f});
|
||||||
|
|
||||||
|
step_n(world, 120); // 2 simulated seconds
|
||||||
|
|
||||||
|
const float distance = right.get_origin().x - left.get_origin().x;
|
||||||
|
// Boxes must not be overlapping (combined extents = 1.0).
|
||||||
|
EXPECT_GE(distance, 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSimulation, BoxGetOriginMatchesSetOrigin)
|
||||||
|
{
|
||||||
|
// Kinematic teleport — set_origin must immediately reflect in get_origin.
|
||||||
|
omath::collision::PhysXWorld world;
|
||||||
|
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(1.f, 1.f, 1.f));
|
||||||
|
box.set_kinematic(true);
|
||||||
|
|
||||||
|
box.set_origin({7.f, 3.f, -2.f});
|
||||||
|
|
||||||
|
EXPECT_NEAR(box.get_origin().x, 7.f, 1e-4f);
|
||||||
|
EXPECT_NEAR(box.get_origin().y, 3.f, 1e-4f);
|
||||||
|
EXPECT_NEAR(box.get_origin().z, -2.f, 1e-4f);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PhysXSimulation, BoxFallsUnderGravity)
|
||||||
|
{
|
||||||
|
// Without a floor, a box should be lower after simulation than its start.
|
||||||
|
omath::collision::PhysXWorld world; // default gravity -9.81 Y
|
||||||
|
omath::collision::PhysXRigidBody box(world, physx::PxBoxGeometry(0.5f, 0.5f, 0.5f),
|
||||||
|
{0.f, 10.f, 0.f});
|
||||||
|
|
||||||
|
const float y_start = box.get_origin().y;
|
||||||
|
step_n(world, 60); // 1 simulated second
|
||||||
|
|
||||||
|
EXPECT_LT(box.get_origin().y, y_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // OMATH_ENABLE_PHYSX
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"default-registry": {
|
"default-registry": {
|
||||||
"kind": "git",
|
"kind": "git",
|
||||||
"baseline": "b1b19307e2d2ec1eefbdb7ea069de7d4bcd31f01",
|
"baseline": "efa4634bd526b87559684607d2cbbdeeec0f07d8",
|
||||||
"repository": "https://github.com/microsoft/vcpkg"
|
"repository": "https://github.com/microsoft/vcpkg"
|
||||||
},
|
},
|
||||||
"registries": [
|
"registries": [
|
||||||
|
|||||||
@@ -52,6 +52,13 @@
|
|||||||
"lua",
|
"lua",
|
||||||
"sol2"
|
"sol2"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"physx": {
|
||||||
|
"description": "PhysX-backed collider implementations",
|
||||||
|
"dependencies": [
|
||||||
|
"physx"
|
||||||
|
],
|
||||||
|
"supports": "(windows & x64 & !mingw & !uwp) | (linux & x64) | (linux & arm64)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user