From 8b8102eeb08d7b97940382381d7ea49cdfa628b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sun, 13 Mar 2016 19:07:35 +0100 Subject: [PATCH] Ajout de fonctions pour le "coarse test" de la collision --- include/ball.hpp | 5 +++++ include/block.hpp | 5 +++++ include/object.hpp | 6 ++++++ src/ball.cpp | 8 ++++++++ src/block.cpp | 8 ++++++++ src/engine.cpp | 18 ++++++++++++++++++ 6 files changed, 50 insertions(+) diff --git a/include/ball.hpp b/include/ball.hpp index 9369000..8a5d746 100644 --- a/include/ball.hpp +++ b/include/ball.hpp @@ -29,6 +29,11 @@ public: */ void draw(sf::RenderWindow& window); + /** + * Récupère la boîte englobante de l'objet + */ + std::unique_ptr getAABB(); + /** * Détermine la couche d'affichage de l'objet */ diff --git a/include/block.hpp b/include/block.hpp index 9d39a74..717c70b 100644 --- a/include/block.hpp +++ b/include/block.hpp @@ -29,6 +29,11 @@ public: */ void update(EngineState& state); + /** + * Récupère la boîte englobante de l'objet + */ + std::unique_ptr getAABB(); + /** * Détermine la couche d'affichage de l'objet */ diff --git a/include/object.hpp b/include/object.hpp index 31e2b33..edd395b 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -2,6 +2,7 @@ #define __PTF_OBJECT_HPP__ #include +#include #include "engine_state.hpp" class Object { @@ -24,6 +25,11 @@ public: */ virtual void update(EngineState& state) = 0; + /** + * Récupère la boîte englobante de l'objet + */ + virtual std::unique_ptr getAABB() = 0; + /** * Détermine la couche d'affichage de l'objet */ diff --git a/src/ball.cpp b/src/ball.cpp index 7350096..61db850 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -14,6 +14,14 @@ void Ball::draw(sf::RenderWindow& window) { window.draw(shape); } +std::unique_ptr Ball::getAABB() { + return std::unique_ptr(new sf::FloatRect( + position.x - 10 * mass, + position.y - 10 * mass, + 20 * mass, 20 * mass + )); +} + sf::Vector2f Ball::getForces(EngineState& state) { sf::Vector2f forces = PhysicsObject::getForces(state); diff --git a/src/block.cpp b/src/block.cpp index 0c36883..24c147e 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -23,3 +23,11 @@ void Block::draw(sf::RenderWindow& window) { void Block::update(EngineState& state) { // rien à mettre à jour } + +std::unique_ptr Block::getAABB() { + return std::unique_ptr(new sf::FloatRect( + position.x - Block::GRID / 2, + position.y - Block::GRID / 2, + Block::GRID, Block::GRID + )); +} diff --git a/src/engine.cpp b/src/engine.cpp index d4e7f5f..d7209d3 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -48,6 +48,24 @@ void Engine::update() { for (unsigned int i = 0; i < state.objects.size(); i++) { state.objects[i]->update(state); } + + // collisions entre objets + for (unsigned int i = 0; i < state.objects.size(); i++) { + Object* objA = state.objects[i]; + + for (unsigned int j = i + 1; j < state.objects.size(); j++) { + Object* objB = state.objects[j]; + + // vérifie si on a un risque de collision + if (objA->getAABB()->intersects(*objB->getAABB())) { + // TODO: gestion de la collision => + // calcul de la normale + // calcul de la profondeur du choc + // si profondeur > 0 : + // résolution de la collision + } + } + } } void Engine::draw() {