Ajout de la collision bloc à bloc

This commit is contained in:
Mattéo Delabre 2016-03-19 20:18:26 +01:00
parent dad579a3fa
commit 098c5b1891
1 changed files with 34 additions and 2 deletions

View File

@ -54,6 +54,38 @@ bool Block::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) {
}
bool Block::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) {
// TODO: coder cette fonction
std::unique_ptr<sf::FloatRect> aabb = getAABB();
std::unique_ptr<sf::FloatRect> obj_aabb = obj.getAABB();
sf::Vector2f relpos = getPosition() - obj.getPosition();
float overlap_x = aabb->width / 2 + obj_aabb->width / 2 - std::abs(relpos.x);
float overlap_y = aabb->height / 2 + obj_aabb->height / 2 - std::abs(relpos.y);
// si il n'y a pas de chauvauchement sur l'axe X et Y, pas de collision
if (overlap_x <= 0 || overlap_y <= 0) {
return false;
}
// on choisit l'axe de pénétration maximale pour calculer la normale
if (overlap_x < overlap_y) {
if (relpos.x < 0) {
normal.x = -1;
} else {
normal.x = 1;
}
normal.y = 0;
depth = overlap_x;
} else {
if (relpos.y < 0) {
normal.y = -1;
} else {
normal.y = 1;
}
normal.x = 0;
depth = overlap_y;
}
return true;
}