Ajout de fonctions pour le "coarse test" de la collision
This commit is contained in:
		
							parent
							
								
									4cc2bfa197
								
							
						
					
					
						commit
						8b8102eeb0
					
				|  | @ -29,6 +29,11 @@ public: | |||
|      */ | ||||
|     void draw(sf::RenderWindow& window); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Récupère la boîte englobante de l'objet | ||||
|      */ | ||||
|     std::unique_ptr<sf::FloatRect> getAABB(); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Détermine la couche d'affichage de l'objet | ||||
|      */ | ||||
|  |  | |||
|  | @ -29,6 +29,11 @@ public: | |||
|      */ | ||||
|     void update(EngineState& state); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Récupère la boîte englobante de l'objet | ||||
|      */ | ||||
|     std::unique_ptr<sf::FloatRect> getAABB(); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Détermine la couche d'affichage de l'objet | ||||
|      */ | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ | |||
| #define __PTF_OBJECT_HPP__ | ||||
| 
 | ||||
| #include <SFML/Graphics.hpp> | ||||
| #include <memory> | ||||
| #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<sf::FloatRect> getAABB() = 0; | ||||
| 
 | ||||
|     /**
 | ||||
|      * Détermine la couche d'affichage de l'objet | ||||
|      */ | ||||
|  |  | |||
|  | @ -14,6 +14,14 @@ void Ball::draw(sf::RenderWindow& window) { | |||
|     window.draw(shape); | ||||
| } | ||||
| 
 | ||||
| std::unique_ptr<sf::FloatRect> Ball::getAABB() { | ||||
|     return std::unique_ptr<sf::FloatRect>(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); | ||||
| 
 | ||||
|  |  | |||
|  | @ -23,3 +23,11 @@ void Block::draw(sf::RenderWindow& window) { | |||
| void Block::update(EngineState& state) { | ||||
|     // rien à mettre à jour
 | ||||
| } | ||||
| 
 | ||||
| std::unique_ptr<sf::FloatRect> Block::getAABB() { | ||||
|     return std::unique_ptr<sf::FloatRect>(new sf::FloatRect( | ||||
|         position.x - Block::GRID / 2, | ||||
|         position.y - Block::GRID / 2, | ||||
|         Block::GRID, Block::GRID | ||||
|     )); | ||||
| } | ||||
|  |  | |||
|  | @ -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() { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue