Starting basic physics, nothing fancy
This commit is contained in:
14
src/dawn/physics/3d/AABB3D.hpp
Normal file
14
src/dawn/physics/3d/AABB3D.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct AABB3D {
|
||||
glm::vec3 min;
|
||||
glm::vec3 max;
|
||||
};
|
||||
}
|
10
src/dawn/physics/3d/CMakeLists.txt
Normal file
10
src/dawn/physics/3d/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Ray3D.cpp
|
||||
)
|
14
src/dawn/physics/3d/PhysicsSphere.hpp
Normal file
14
src/dawn/physics/3d/PhysicsSphere.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct PhysicsSphere {
|
||||
glm::vec3 center;
|
||||
float_t radius;
|
||||
};
|
||||
}
|
15
src/dawn/physics/3d/PhysicsTriangle.hpp
Normal file
15
src/dawn/physics/3d/PhysicsTriangle.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct PhysicsTriangle {
|
||||
glm::vec3 v0;
|
||||
glm::vec3 v1;
|
||||
glm::vec3 v2;
|
||||
};
|
||||
}
|
106
src/dawn/physics/3d/Ray3D.cpp
Normal file
106
src/dawn/physics/3d/Ray3D.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Ray3D.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
struct Ray3D {
|
||||
glm::vec3 origin;
|
||||
glm::vec3 direction;
|
||||
};
|
||||
|
||||
struct PhysicsSphere {
|
||||
glm::vec3 center;
|
||||
float_t radius;
|
||||
};
|
||||
|
||||
bool_t Dawn::raytestSphere(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsSphere sphere,
|
||||
glm::vec3 *hit,
|
||||
glm::vec3 *normal
|
||||
) {
|
||||
glm::vec3 h, n;
|
||||
auto result = glm::intersectRaySphere(
|
||||
ray.origin, ray.direction,
|
||||
sphere.center, sphere.radius,
|
||||
h, n
|
||||
);
|
||||
|
||||
if(hit != nullptr) *hit = h;
|
||||
if(normal != nullptr) *normal = n;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool_t Dawn::raytestTriangle(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsTriangle triangle,
|
||||
glm::vec2 *hit,
|
||||
float_t *distance
|
||||
) {
|
||||
glm::vec2 h;
|
||||
float_t d;
|
||||
auto result = glm::intersectRayTriangle(
|
||||
ray.origin, ray.direction,
|
||||
triangle.v0, triangle.v1, triangle.v2,
|
||||
h, d
|
||||
);
|
||||
if(hit != nullptr) *hit = h;
|
||||
if(distance != nullptr) *distance = d;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool_t Dawn::raytestAABB(
|
||||
struct Ray3D ray,
|
||||
struct AABB3D box,
|
||||
glm::vec3 *point,
|
||||
glm::vec3 *normal,
|
||||
float_t *distance
|
||||
) {
|
||||
// Compute the inverse direction of the ray, for numerical stability
|
||||
glm::vec3 invDir(1.0f / ray.direction.x, 1.0f / ray.direction.y, 1.0f / ray.direction.z);
|
||||
|
||||
// Compute the t-values for the two intersection candidates
|
||||
glm::vec3 tMin = (box.min - ray.origin) * invDir;
|
||||
glm::vec3 tMax = (box.max - ray.origin) * invDir;
|
||||
|
||||
// Make sure tMin is less than or equal to tMax for all components
|
||||
glm::vec3 t1 = glm::min(tMin, tMax);
|
||||
glm::vec3 t2 = glm::max(tMin, tMax);
|
||||
float tNear = glm::compMax(t1);
|
||||
float tFar = glm::compMin(t2);
|
||||
|
||||
// If tNear is greater than or equal to tFar, there is no intersection
|
||||
if (tNear >= tFar) return false;
|
||||
|
||||
// If tFar is negative, the ray is pointing away from the box
|
||||
if(tFar < 0.0f) return false;
|
||||
|
||||
// Compute the hit point and normal
|
||||
glm::vec3 hitPoint = ray.origin + tNear * ray.direction;
|
||||
|
||||
if(point != nullptr) *point = hitPoint;
|
||||
if(distance != nullptr) *distance = tNear;
|
||||
if(normal != nullptr) {
|
||||
if (hitPoint.x == box.min.x) {
|
||||
*normal = glm::vec3(-1, 0, 0);
|
||||
} else if (hitPoint.x == box.max.x) {
|
||||
*normal = glm::vec3(1, 0, 0);
|
||||
} else if (hitPoint.y == box.min.y) {
|
||||
*normal = glm::vec3(0, -1, 0);
|
||||
} else if (hitPoint.y == box.max.y) {
|
||||
*normal = glm::vec3(0, 1, 0);
|
||||
} else if (hitPoint.z == box.min.z) {
|
||||
*normal = glm::vec3(0, 0, -1);
|
||||
} else if (hitPoint.z == box.max.z) {
|
||||
*normal = glm::vec3(0, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// The ray intersects the box
|
||||
return true;
|
||||
}
|
40
src/dawn/physics/3d/Ray3D.hpp
Normal file
40
src/dawn/physics/3d/Ray3D.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "PhysicsTriangle.hpp"
|
||||
#include "PhysicsSphere.hpp"
|
||||
#include "AABB3D.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct Ray3D {
|
||||
glm::vec3 origin;
|
||||
glm::vec3 direction;
|
||||
};
|
||||
|
||||
bool_t raytestSphere(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsSphere sphere,
|
||||
glm::vec3 *hit,
|
||||
glm::vec3 *normal
|
||||
);
|
||||
|
||||
bool_t raytestTriangle(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsTriangle triangle,
|
||||
glm::vec2 *hit,
|
||||
float_t *distance
|
||||
);
|
||||
|
||||
bool_t raytestAABB(
|
||||
struct Ray3D ray,
|
||||
struct AABB3D box,
|
||||
glm::vec3 *point,
|
||||
glm::vec3 *normal,
|
||||
float_t *distance
|
||||
);
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
PhysicsManager.cpp
|
||||
)
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
PhysicsManager.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(3d)
|
Reference in New Issue
Block a user