Example Collider3D implementation

This commit is contained in:
2023-02-24 22:51:31 -08:00
parent cf222c469c
commit d5fdc29cb4
10 changed files with 104 additions and 81 deletions

View File

@ -4,10 +4,11 @@
# https://opensource.org/licenses/MIT
# Sources
# target_sources(${DAWN_TARGET_NAME}
# PRIVATE
# RayTester3D.cpp
# )
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Collider3D.cpp
CubeCollider.cpp
)
# Subdirs
add_subdirectory(collider)
tool_scenecomponent(Collider3D scene/components/physics/3d/Collider3D.hpp)
tool_scenecomponent(CubeCollider scene/components/physics/3d/CubeCollider.hpp)

View File

@ -0,0 +1,47 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "physics/3d/Ray3D.hpp"
namespace Dawn {
struct Collider3DRayResult {
glm::vec3 point;
float_t distance;
glm::vec3 normal;
};
enum Collider3DType {
COLLIDER3D_TYPE_CUBE
};
class Collider3D : public SceneItemComponent {
protected:
public:
Collider3D(SceneItem *item);
/**
* Perform a raycast against this collider.
*
* @param result Where to store the result of the raycast collision
* @param ray The ray to cast against.
* @return True if the ray intercepts this collider, otherwise false.
*/
virtual bool_t raycast(
struct Collider3DRayResult *result,
struct Ray3D ray
) = 0;
/**
* Returns which type of collider this is. Useful for performing dynamic
* casting in your event listeners.
*
* @return The collider type this is.
*/
virtual enum Collider3DType getColliderType() = 0;
};
}

View File

@ -0,0 +1,32 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeCollider.hpp"
using namespace Dawn;
CubeCollider::CubeCollider(SceneItem *item) : Collider3D(item) {
}
bool_t CubeCollider::raycast(
struct Collider3DRayResult *result,
struct Ray3D ray
) {
assertNotNull(result);
return Dawn::raytestCube(
ray,
(struct AABB3D){ .min = this->min, .max = this->max },
this->transform->getWorldTransform(),
&result->point,
&result->normal,
&result->distance
);
}
enum Collider3DType CubeCollider::getColliderType() {
return COLLIDER3D_TYPE_CUBE;
}

View File

@ -10,9 +10,16 @@
namespace Dawn {
class CubeCollider : public Collider3D {
public:
glm::vec3 center = glm::vec3(0, 0, 0);
glm::vec3 size = glm::vec3(1, 1, 1);
glm::vec3 min = glm::vec3(-0.5f, -0.5f, -0.5f);
glm::vec3 max = glm::vec3(0.5f, 0.5f, 0.5f);
CubeCollider(SceneItem *item);
bool_t raycast(
struct Collider3DRayResult *result,
struct Ray3D ray
) override;
enum Collider3DType getColliderType() override;
};
}

View File

@ -1,14 +0,0 @@
# 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
Collider3D.cpp
CubeCollider.cpp
)
tool_scenecomponent(Collider3D scene/components/physics/3d/collider/Collider3D.hpp)
tool_scenecomponent(CubeCollider scene/components/physics/3d/collider/CubeCollider.hpp)

View File

@ -1,22 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "physics/3d/Ray3D.hpp"
namespace Dawn {
struct Collider3DRayResult {
glm::vec3 normal;
glm::vec3 point;
};
class Collider3D : public SceneItemComponent {
public:
Collider3D(SceneItem *item);
virtual bool_t raycast(struct Ray3D ray, struct Collider3DRayResult *out) = 0;
};
}

View File

@ -1,12 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeCollider.hpp"
using namespace Dawn;
CubeCollider::CubeCollider(SceneItem *item) : Collider3D(item) {
}