2016-03-04 15:29:31 +00:00
|
|
|
#include "ball.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include "block.hpp"
|
2016-03-14 20:20:40 +00:00
|
|
|
#include "constants.hpp"
|
2016-03-15 21:00:03 +00:00
|
|
|
#include <array>
|
2016-03-19 14:33:31 +00:00
|
|
|
#include <iostream>
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-03-15 16:08:21 +00:00
|
|
|
Ball::Ball(float x, float y) : Object(x, y), shape(10) {
|
|
|
|
shape.setOrigin(sf::Vector2f(10, 10));
|
2016-03-14 20:11:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 16:03:56 +00:00
|
|
|
sf::Vector2f Ball::getForces(EngineState& state) {
|
2016-03-15 16:08:21 +00:00
|
|
|
sf::Vector2f forces = Object::getForces(state);
|
2016-03-08 18:52:55 +00:00
|
|
|
|
|
|
|
// déplacement de la balle après appui sur les touches de direction
|
2016-03-13 16:03:56 +00:00
|
|
|
if (state.keys[sf::Keyboard::Left]) {
|
2016-03-14 20:20:40 +00:00
|
|
|
forces += sf::Vector2f(-Constants::MOVE, 0);
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 16:03:56 +00:00
|
|
|
if (state.keys[sf::Keyboard::Right]) {
|
2016-03-14 20:20:40 +00:00
|
|
|
forces += sf::Vector2f(Constants::MOVE, 0);
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return forces;
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
2016-03-15 21:00:03 +00:00
|
|
|
|
|
|
|
void Ball::draw(sf::RenderWindow& window) {
|
|
|
|
Object::draw(window);
|
|
|
|
|
|
|
|
// chargement de la texture de test
|
|
|
|
if (!texture.loadFromFile("./res/ball.png")) {
|
|
|
|
// erreur
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:27:24 +00:00
|
|
|
shape.rotate(getVelocity().x * .1f);
|
2016-03-15 21:00:03 +00:00
|
|
|
shape.setTexture(&texture);
|
|
|
|
shape.setPosition(getPosition());
|
|
|
|
window.draw(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<sf::FloatRect> Ball::getAABB() {
|
|
|
|
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
|
|
|
|
getPosition().x - 10,
|
|
|
|
getPosition().y - 10,
|
|
|
|
20, 20
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
bool Ball::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) {
|
|
|
|
return obj.getCollisionInfo(*this, normal, depth);
|
2016-03-15 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
bool Ball::getCollisionInfo(Ball& obj, sf::Vector2f& normal, float& depth) {
|
2016-03-19 14:33:31 +00:00
|
|
|
sf::Vector2f dir = getPosition() - obj.getPosition();
|
2016-03-15 21:00:03 +00:00
|
|
|
float squaredLength = dir.x * dir.x + dir.y * dir.y;
|
|
|
|
|
|
|
|
// TODO: supprimer les valeurs magiques
|
|
|
|
// si les deux balles sont à une distance supérieure
|
|
|
|
// à la somme de leurs deux rayons, il n'y a pas eu collision
|
|
|
|
if (squaredLength > 20 * 20) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float length = std::sqrt(squaredLength);
|
|
|
|
|
|
|
|
// les balles sont sur la même position.
|
|
|
|
// Renvoie une normale apte à séparer les deux balles
|
|
|
|
if (length == 0) {
|
2016-03-18 15:48:22 +00:00
|
|
|
// TODO: supprimer les valeurs magiques
|
|
|
|
depth = 10;
|
2016-03-15 21:00:03 +00:00
|
|
|
normal.x = 0;
|
|
|
|
normal.y = -1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
// TODO: supprimer les valeurs magiques
|
2016-03-15 21:00:03 +00:00
|
|
|
// il y a eu collision
|
2016-03-19 14:33:31 +00:00
|
|
|
depth = 20 - length;
|
2016-03-15 21:00:03 +00:00
|
|
|
normal = dir / length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
bool Ball::getCollisionInfo(Block& obj, sf::Vector2f& normal, float& depth) {
|
2016-03-15 21:00:03 +00:00
|
|
|
// recherche du point le plus proche du centre de la
|
|
|
|
// balle sur le bloc. On regarde la position relative
|
|
|
|
// du cercle par rapport au bloc
|
|
|
|
std::unique_ptr<sf::FloatRect> aabb = obj.getAABB();
|
|
|
|
sf::Vector2f relpos = getPosition() - obj.getPosition();
|
|
|
|
sf::Vector2f closest = relpos;
|
|
|
|
|
|
|
|
// on restreint la position relative pour rester
|
|
|
|
// à l'intérieur du bloc
|
|
|
|
if (closest.x < -aabb->width / 2) {
|
|
|
|
closest.x = -aabb->width / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closest.x > aabb->width / 2) {
|
|
|
|
closest.x = aabb->width / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closest.y < -aabb->height / 2) {
|
|
|
|
closest.y = -aabb->height / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closest.y > aabb->height / 2) {
|
|
|
|
closest.y = aabb->height / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// si la position n'a pas été changée, elle
|
|
|
|
// était déjà à l'intérieur du cercle : le cercle
|
|
|
|
// est dans le bloc
|
|
|
|
float isInside = false;
|
|
|
|
|
|
|
|
if (relpos == closest) {
|
|
|
|
isInside = true;
|
|
|
|
|
|
|
|
// on se colle au bord le plus proche du bloc
|
|
|
|
if (std::abs(relpos.x) > std::abs(relpos.y)) {
|
|
|
|
if (closest.x > 0) {
|
|
|
|
closest.x = aabb->width / 2;
|
|
|
|
} else {
|
|
|
|
closest.x = -aabb->width / 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (closest.y > 0) {
|
|
|
|
closest.y = aabb->height / 2;
|
|
|
|
} else {
|
|
|
|
closest.y = -aabb->height / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// la normale est portée par la direction
|
|
|
|
// du point le plus proche au centre de la balle
|
|
|
|
sf::Vector2f prenormal = relpos - closest;
|
|
|
|
float squaredLength = prenormal.x * prenormal.x + prenormal.y * prenormal.y;
|
|
|
|
|
|
|
|
// TODO: supprimer les valeurs magiques
|
|
|
|
// si la balle est à l'extérieur et que
|
|
|
|
// la normale est plus longue que son rayon,
|
|
|
|
// il n'y a pas collision
|
2016-03-18 15:48:22 +00:00
|
|
|
if (!isInside && squaredLength >= 10 * 10) {
|
2016-03-15 21:00:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:48:22 +00:00
|
|
|
float length = std::sqrt(squaredLength);
|
|
|
|
depth = 10 - length;
|
|
|
|
normal = prenormal / length;
|
2016-03-15 21:00:03 +00:00
|
|
|
|
|
|
|
if (isInside) {
|
|
|
|
normal *= -1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|