Skip to content

Commit

Permalink
Merge pull request #9 from epitech-mirroring/feature/epi-46-collision…
Browse files Browse the repository at this point in the history
…s-methods

💥 Add collisions methods
  • Loading branch information
landryarki authored Oct 10, 2024
2 parents a3cd11f + 85b874b commit 83764e9
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/physics/Box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#include "Sphere.hpp"
#include "Box.hpp"
#include "Physics.hpp"

Box::Box(Vector3 position, Vector3 size) : position(position), size(size)
{
}

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;
}
48 changes: 48 additions & 0 deletions src/physics/Box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#ifndef STELLARFORGE_BOX_HPP
#define STELLARFORGE_BOX_HPP

#include "Collider.hpp"

/**
* @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);

/**
* @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;
};

#endif //STELLARFORGE_BOX_HPP
5 changes: 5 additions & 0 deletions src/physics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ target_link_libraries(StellarForgePhysics PUBLIC glm::glm StellarForge::Common)
target_sources(StellarForgePhysics
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/Physics.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Collider.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Box.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Sphere.hpp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Physics.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Box.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Sphere.cpp
)
44 changes: 44 additions & 0 deletions src/physics/Collider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
** 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;
};

#endif //STELLARFORGE_COLLIDER_HPP
31 changes: 31 additions & 0 deletions src/physics/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

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

void Physics::Movement::applyMovement(Vector3 &pos, const Vector3 &vel,
Expand Down Expand Up @@ -56,3 +57,33 @@ 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: 46 additions & 0 deletions src/physics/Physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#define PHYSICS_HPP
#include <glm/vec3.hpp>
#include <glm/gtc/quaternion.hpp>
#include "Collider.hpp"
#include "Box.hpp"
#include "Sphere.hpp"

/**
* @typedef Vector3
Expand Down Expand Up @@ -109,6 +112,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
23 changes: 23 additions & 0 deletions src/physics/Sphere.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#include "Sphere.hpp"
#include "Box.hpp"
#include "Physics.hpp"

Sphere::Sphere(Vector3 position, float radius) : position(position), radius(radius)
{
}

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;
}
48 changes: 48 additions & 0 deletions src/physics/Sphere.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
*/

#ifndef STELLARFORGE_SPHERE_HPP
#define STELLARFORGE_SPHERE_HPP

#include "Collider.hpp"

/**
* @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);

/**
* @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_SPHERE_HPP

0 comments on commit 83764e9

Please sign in to comment.