OGL Engine 1.2.0-dev
Loading...
Searching...
No Matches
aabb.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <glm/glm.hpp>
8#include <glm/gtc/quaternion.hpp>
9
10namespace Engine::Physics {
17 struct AABB {
19 glm::vec2 center;
21 glm::vec2 halfSize;
22
33 AABB(glm::vec2 pos, glm::vec2 size, glm::quat rot = glm::quat()) {
34 glm::vec2 half = size * 0.5f;
35
36 // Définir les 4 coins dans l'espace local
37 glm::vec3 corners[4] = {
38 glm::vec3(-half.x, -half.y, 0),
39 glm::vec3( half.x, -half.y, 0),
40 glm::vec3( half.x, half.y, 0),
41 glm::vec3(-half.x, half.y, 0)
42 };
43
44 // Appliquer la rotation 3D (autour de Z), puis projection 2D
45 glm::vec2 min(FLT_MAX), max(-FLT_MAX);
46 for (int i = 0; i < 4; ++i) {
47 glm::vec3 rotated = rot * corners[i]; // rotation quaternion
48 glm::vec2 p = glm::vec2(rotated.x, rotated.y) + pos;
49
50 min = glm::min(min, p);
51 max = glm::max(max, p);
52 }
53
54 center = (min + max) * 0.5f;
55 halfSize = (max - min) * 0.5f;
56 }
57
63 glm::vec2 Min() const {
64 return center - halfSize;
65 }
66
72 glm::vec2 Max() const {
73 return center + halfSize;
74 }
75
83 bool Intersects(const AABB& other, float margin = 0.0f) const {
84 return (
85 (Max().x + margin) > other.Min().x && (Min().x - margin) < other.Max().x &&
86 (Max().y + margin) > other.Min().y && (Min().y - margin) < other.Max().y
87 );
88 }
89 };
90}
Definition aabb.hpp:10
AABB(glm::vec2 pos, glm::vec2 size, glm::quat rot=glm::quat())
Construit un nouvel objet aabb.
Definition aabb.hpp:33
glm::vec2 Max() const
Renvoie la position la plus forte sur les deux axes (droite/haut de la box)
Definition aabb.hpp:72
glm::vec2 halfSize
La moitié de la taille de l'AABB (sur les deux axes)
Definition aabb.hpp:21
bool Intersects(const AABB &other, float margin=0.0f) const
Renvoie vrai si l'autre AABB passée en paramètre entre en collision avec celle-ci.
Definition aabb.hpp:83
glm::vec2 center
La position centrale de l'AABB.
Definition aabb.hpp:19
glm::vec2 Min() const
Renvoie la position la plus faible sur les deux axes (gauche/bas de la box)
Definition aabb.hpp:63