2016-03-04 15:29:31 +00:00
|
|
|
#include "ball.hpp"
|
|
|
|
|
2016-03-08 18:52:55 +00:00
|
|
|
void Ball::draw(sf::RenderWindow& window) {
|
2016-03-13 12:31:41 +00:00
|
|
|
PhysicsObject::draw(window);
|
|
|
|
|
2016-03-12 13:15:27 +00:00
|
|
|
// chargement de la texture de test
|
2016-03-12 22:15:03 +00:00
|
|
|
if (!texture.loadFromFile("./res/ball.png")) {
|
2016-03-12 13:15:27 +00:00
|
|
|
// erreur
|
2016-03-12 10:22:10 +00:00
|
|
|
}
|
2016-03-12 13:15:27 +00:00
|
|
|
|
|
|
|
shape.setTexture(&texture);
|
2016-03-12 10:22:10 +00:00
|
|
|
|
2016-03-10 20:47:19 +00:00
|
|
|
shape.setPosition(position);
|
|
|
|
window.draw(shape);
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 16:03:56 +00:00
|
|
|
sf::Vector2f Ball::getForces(EngineState& state) {
|
2016-03-08 18:52:55 +00:00
|
|
|
sf::Vector2f forces = PhysicsObject::getForces(state);
|
|
|
|
|
|
|
|
// 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-08 20:15:08 +00:00
|
|
|
forces += sf::Vector2f(-Ball::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-08 20:15:08 +00:00
|
|
|
forces += sf::Vector2f(Ball::MOVE, 0);
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// force d'attraction entre les balles et les blocs chargés
|
|
|
|
if (getCharge() != 0) {
|
2016-03-09 18:42:09 +00:00
|
|
|
for (unsigned int j = 0; j < state.objects.size(); j++) {
|
|
|
|
Object *attractive = state.objects[j];
|
2016-03-08 18:52:55 +00:00
|
|
|
|
2016-03-09 18:42:09 +00:00
|
|
|
if (attractive == this || attractive->getCharge() == 0) {
|
2016-03-08 18:52:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vecteur allant de l'objet attirant vers l'objet considéré
|
2016-03-11 14:17:59 +00:00
|
|
|
sf::Vector2f attraction(position - attractive->getPosition());
|
2016-03-08 18:52:55 +00:00
|
|
|
|
|
|
|
// la norme de ce vecteur est la distance entre les objets
|
|
|
|
float distanceSquared = attraction.x * attraction.x +
|
|
|
|
attraction.y * attraction.y;
|
2016-03-12 22:15:03 +00:00
|
|
|
|
2016-03-12 18:46:56 +00:00
|
|
|
// éviter la division par zéro
|
|
|
|
if (distanceSquared == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-08 18:52:55 +00:00
|
|
|
|
|
|
|
// normalisation du vecteur direction qui porte
|
|
|
|
// la force d'attraction, puis application de la norme
|
|
|
|
attraction /= std::sqrt(distanceSquared);
|
2016-03-08 20:15:08 +00:00
|
|
|
attraction *= Ball::ATTRACTION * (
|
2016-03-09 18:42:09 +00:00
|
|
|
(charge * attractive->getCharge()) /
|
2016-03-08 18:52:55 +00:00
|
|
|
distanceSquared
|
|
|
|
);
|
|
|
|
|
|
|
|
forces += attraction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return forces;
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|