Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💥 Add collisions methods #9

Merged
merged 24 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
801e167
:sparkles: collision Boxs and Spheres
landryarki Sep 25, 2024
f741c3a
Merge pull request #6 from epitech-mirroring/feature/epi-44-movement-…
landryarki Sep 26, 2024
cb54c0e
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
landryarki Sep 26, 2024
f337904
:bug: handle negative size object
landryarki Sep 26, 2024
18376c8
:hammer: edit .clang-tidy file
landryarki Sep 26, 2024
167292b
:recycle: rename collision methods
landryarki Sep 30, 2024
8041bbf
:twisted_rightwards_arrows: Merge branch 'develop' into feature/epi-4…
landryarki Sep 30, 2024
855f426
:memo: add doc to collisions methods
landryarki Sep 30, 2024
a63fabc
:rotating_light: fix linter
landryarki Sep 30, 2024
f978315
:twisted_rightwards_arrows: Merge branch 'develop' into feature/epi-4…
landryarki Oct 1, 2024
7090f85
:twisted_rightwards_arrows:Merge remote-tracking branch 'origin/devel…
landryarki Oct 2, 2024
ba9396e
:wrench: remove pro-type-union-access linter warning
landryarki Oct 2, 2024
d50c35b
:rotating_light: change experimental headers
landryarki Oct 2, 2024
5734f35
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
landryarki Oct 7, 2024
80075cf
:zap: add ICollider interface
landryarki Oct 7, 2024
3b84208
:rotating_light: fix linter
landryarki Oct 7, 2024
c4475d9
:truck: move colliders into it own file
landryarki Oct 7, 2024
9232fc1
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
landryarki Oct 7, 2024
4d658a6
:recycle: moved boxes and spheres to different files
landryarki Oct 10, 2024
73d2338
:hammer: remove boost warnings
landryarki Oct 10, 2024
854968f
:pencil2: put funciton name in lowercase
landryarki Oct 10, 2024
82e1993
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
landryarki Oct 10, 2024
c6bbe14
:recycle: moved constructor content outside of the hpp
landryarki Oct 10, 2024
85b874b
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
landryarki Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Checks: >
-readability-magic-numbers,
-misc-include-cleaner,
-*-special-member-functions,
-*-pro-type-union-access,
2 changes: 2 additions & 0 deletions src/physics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ target_link_libraries(StellarForgePhysics glm::glm)
target_sources(StellarForgePhysics
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/Physics.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Collider.hpp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Physics.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Collider.cpp
)
27 changes: 27 additions & 0 deletions src/physics/Collider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#include "Collider.hpp"
#include "Physics.hpp"

bool Box::Collide(ICollider *collider) {
if (dynamic_cast<Box *>(collider) != nullptr) {
return Physics::Collision::BoxCollideBox(*this, *dynamic_cast<Box *>(collider));
} else if (dynamic_cast<Sphere *>(collider) != nullptr) {
return Physics::Collision::BoxCollideSphere(*this, *dynamic_cast<Sphere *>(collider));
}
return false;
}

bool Sphere::Collide(ICollider *collider) {
if (dynamic_cast<Sphere *>(collider) != nullptr) {
return Physics::Collision::SphereCollideSphere(*this, *dynamic_cast<Sphere *>(collider));
} else if (dynamic_cast<Box *>(collider) != nullptr) {
return Physics::Collision::SphereCollideBox(*this, *dynamic_cast<Box *>(collider));
}
return false;
}
114 changes: 114 additions & 0 deletions src/physics/Collider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#ifndef STELLARFORGE_COLLIDER_HPP
#define STELLARFORGE_COLLIDER_HPP

#include <glm/glm.hpp>

using Vector3 = glm::vec3;

/**
* @class ICollider
* @brief Interface for colliders in the physics system.
* @version v0.1.0
* @since v0.1.0
* @details This interface defines the basic structure for colliders, including a method to check for collisions.
* @author Landry GIGANT
*/
class ICollider {
public:
/**
* @brief Virtual destructor for the ICollider interface.
* @version v0.1.0
* @since v0.1.0
* @author Landry GIGANT
*/
virtual ~ICollider() = default;

/**
* @brief Check if this collider collides with another collider.
* @param collider Pointer to the other collider.
* @return True if the colliders collide, false otherwise.
* @version v0.1.0
* @since v0.1.0
* @author Landry GIGANT
*/
virtual bool Collide(ICollider *collider) = 0;
};

/**
* @class Box
* @brief A class that represents a box in 3D space.
* @details This class contains the position and size of the box.
* @version v0.1.0
* @since v0.1.0
* @author Landry GIGANT
*/
class Box : public ICollider {
public:
/**
* @brief The position of the box in 3D space.
*/
Vector3 position;

/**
* @brief The size of the box in 3D space.
*/
Vector3 size;

/**
* @brief Constructor for the Box class.
* @param position The position of the box in 3D space.
* @param size The size of the box in 3D space.
*/
Box(Vector3 position, Vector3 size) : position(position), size(size) {};

/**
* @brief Check if the box collides with another collider.
* @param collider The collider to check for collision.
* @return True if the box collides with the collider, false otherwise.
*/
bool Collide(ICollider *collider) override;
};

/**
* @class Sphere
* @brief A class that represents a sphere in 3D space.
* @details This class contains the position and radius of the sphere.
* @version v0.1.0
* @since v0.1.0
* @author Landry GIGANT
*/
class Sphere : public ICollider {
public:
/**
* @brief The position of the sphere in 3D space.
*/
Vector3 position;

/**
* @brief The radius of the sphere.
*/
float radius;

/**
* @brief Constructor for the Sphere class.
* @param position The position of the sphere in 3D space.
* @param radius The radius of the sphere.
*/
Sphere(Vector3 position, float radius) : position(position), radius(radius) {};

/**
* @brief Check if the sphere collides with another collider.
* @param collider The collider to check for collision.
* @return True if the sphere collides with the collider, false otherwise.
*/
bool Collide(ICollider *collider) override;
};

#endif //STELLARFORGE_COLLIDER_HPP
33 changes: 32 additions & 1 deletion src/physics/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "Physics.hpp"
#include "glm/geometric.hpp"
#include <algorithm>

void Physics::Movement::applyMovement(Vector3 &pos, const Vector3 &vel,
const float deltaTime) {
Expand Down Expand Up @@ -56,3 +56,34 @@ void Physics::Movement::applyForce(Vector3 &vel, const Vector3 &force,
void Physics::Movement::applyImpulse(Vector3 &vel, const Vector3 &impulse) {
vel += impulse;
}

bool Physics::Collision::BoxCollideBox(const Box &box1, const Box &box2)
{
return (std::min(box1.position.x, box1.position.x + box1.size.x) <= std::max(box2.position.x, box2.position.x + box2.size.x) &&
std::max(box1.position.x, box1.position.x + box1.size.x) >= std::min(box2.position.x, box2.position.x + box2.size.x) &&
std::min(box1.position.y, box1.position.y + box1.size.y) <= std::max(box2.position.y, box2.position.y + box2.size.y) &&
std::max(box1.position.y, box1.position.y + box1.size.y) >= std::min(box2.position.y, box2.position.y + box2.size.y) &&
std::min(box1.position.z, box1.position.z + box1.size.z) <= std::max(box2.position.z, box2.position.z + box2.size.z) &&
std::max(box1.position.z, box1.position.z + box1.size.z) >= std::min(box2.position.z, box2.position.z + box2.size.z));
}

bool Physics::Collision::SphereCollideSphere(const Sphere &sphere1, const Sphere &sphere2) {
return glm::distance(sphere1.position, sphere2.position) < sphere1.radius + sphere2.radius;
}

bool Physics::Collision::BoxCollideSphere(const Box &box, const Sphere &sphere) {
const Vector3 closestPoint = glm::vec3(
glm::clamp(sphere.position.x, std::min(box.position.x, box.position.x + box.size.x),
std::max(box.position.x, box.position.x + box.size.x)),
glm::clamp(sphere.position.y, std::min(box.position.y, box.position.y + box.size.y),
std::max(box.position.y, box.position.y + box.size.y)),
glm::clamp(sphere.position.z, std::min(box.position.z, box.position.z + box.size.z),
std::max(box.position.z, box.position.z + box.size.z))
);
return glm::distance(sphere.position, closestPoint) < sphere.radius;
}

bool Physics::Collision::SphereCollideBox(const Sphere &sphere, const Box &box) {
return BoxCollideSphere(box, sphere);
}

46 changes: 45 additions & 1 deletion src/physics/Physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

#ifndef PHYSICS_HPP
#define PHYSICS_HPP
#include <glm/ext/vector_float3.hpp>

#include <glm/glm.hpp>
#include "Collider.hpp"

/**
* @class Physics
Expand Down Expand Up @@ -109,6 +110,49 @@ class Physics {
*/
static void applyImpulse(Vector3 &vel, const Vector3 &impulse);
};

/**
* @class Collision
* @brief A class that provides static methods for collision detection.
* @details This class contains methods for detecting collisions between boxes and spheres.
* @version v0.1.0
* @since v0.1.0
* @author Landry GIGANT
*/
class Collision {
public:
/**
* @brief Check if two boxes collide.
* @param box1 The first box.
* @param box2 The second box.
* @return True if the boxes collide, false otherwise.
*/
static bool BoxCollideBox(const Box &box1, const Box &box2);

/**
* @brief Check if two spheres collide.
* @param sphere1 The first sphere.
* @param sphere2 The second sphere.
* @return True if the spheres collide, false otherwise.
*/
static bool SphereCollideSphere(const Sphere &sphere1, const Sphere &sphere2);

/**
* @brief Check if a box and a sphere collide.
* @param box The box.
* @param sphere The sphere.
* @return True if the box and the sphere collide, false otherwise.
*/
static bool BoxCollideSphere(const Box &box, const Sphere &sphere);

/**
* @brief Check if a sphere and a box collide.
* @param sphere The sphere.
* @param box The box.
* @return True if the sphere and the box collide, false otherwise.
*/
static bool SphereCollideBox(const Sphere &sphere, const Box &box);
};
};


Expand Down