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-12 13:15:27 +00:00
|
|
|
// chargement de la texture de test
|
|
|
|
if (!texture.loadFromFile("./Images/texture_test.jpeg")) {
|
|
|
|
// 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-08 18:52:55 +00:00
|
|
|
sf::Vector2f Ball::getForces(State state) {
|
|
|
|
sf::Vector2f forces = PhysicsObject::getForces(state);
|
|
|
|
|
|
|
|
// déplacement de la balle après appui sur les touches de direction
|
|
|
|
if (state.goLeftKey) {
|
2016-03-08 20:15:08 +00:00
|
|
|
forces += sf::Vector2f(-Ball::MOVE, 0);
|
2016-03-08 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.goRightKey) {
|
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;
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|