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 9 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 @@ -18,3 +18,4 @@ Checks: >
-hicpp-uppercase-literal-suffix,
-cppcoreguidelines-avoid-magic-numbers,
-readability-magic-numbers,
-cppcoreguidelines-pro-type-union-access,
31 changes: 31 additions & 0 deletions src/physics/Physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

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

bool Physics::Collision::BoxCollideBox(Box box1, 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(Sphere sphere1, Sphere sphere2) {
return glm::distance(sphere1.position, sphere2.position) < sphere1.radius + sphere2.radius;
}

bool Physics::Collision::BoxCollideSphere(Box box, 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(Sphere sphere, Box box) {
return BoxCollideSphere(box, sphere);
}
99 changes: 99 additions & 0 deletions src/physics/Physics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,105 @@ class Physics {
*/
static void applyImpulse(Vector3 &vel, const Vector3 &impulse);
};

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

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

/**
* @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(Box box1, 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(Sphere sphere1, 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(Box box, 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(Sphere sphere, Box box);
};
};


Expand Down