Centralisation des constantes dans un en-tête constants

This commit is contained in:
Mattéo Delabre 2016-03-14 21:20:40 +01:00
parent cb0dd83817
commit a25a637a35
8 changed files with 57 additions and 27 deletions

View File

@ -16,9 +16,6 @@ protected:
*/ */
virtual sf::Vector2f getForces(EngineState& state); virtual sf::Vector2f getForces(EngineState& state);
static constexpr float ATTRACTION = 25000;
static constexpr float MOVE = 100;
public: public:
Ball(float x, float y); Ball(float x, float y);

View File

@ -12,8 +12,6 @@ protected:
sf::RectangleShape shape; sf::RectangleShape shape;
public: public:
static constexpr float GRID = 32;
Block(float x, float y); Block(float x, float y);
/** /**

33
include/constants.hpp Normal file
View File

@ -0,0 +1,33 @@
#ifndef __PTF_CONSTANTS_HPP__
#define __PTF_CONSTANTS_HPP__
namespace Constants {
/**
* Constante d'attraction. Utilisée dans la formule
* pour le calcul de l'attraction coulombienne entre
* deux objets
*/
static constexpr float ATTRACTION = 25000;
/**
* Constante de déplacement. Définit la quantité de
* mouvement qui est donnée à un objet lorsqu'il
* est manipulé manuellement par le joueur
*/
static constexpr float MOVE = 100;
/**
* Constante de gravité. Utilisée dans la formule
* pour calculer la force de gravité appliquée
* uniformément vers le bas de la fenêtre sur tous
* les objets
*/
static constexpr float GRAVITY = 20;
/**
* Taille de la grille des blocs en pixels
*/
static constexpr float GRID = 32;
}
#endif

View File

@ -19,8 +19,6 @@ protected:
*/ */
virtual sf::Vector2f getForces(EngineState& state); virtual sf::Vector2f getForces(EngineState& state);
static constexpr float GRAVITY = 20;
public: public:
PhysicsObject(float x, float y); PhysicsObject(float x, float y);

View File

@ -1,4 +1,5 @@
#include "ball.hpp" #include "ball.hpp"
#include "constants.hpp"
Ball::Ball(float x, float y) : PhysicsObject(x, y), shape(10 * mass) { Ball::Ball(float x, float y) : PhysicsObject(x, y), shape(10 * mass) {
shape.setOrigin(sf::Vector2f(10 * mass, 10 * mass)); shape.setOrigin(sf::Vector2f(10 * mass, 10 * mass));
@ -35,11 +36,11 @@ sf::Vector2f Ball::getForces(EngineState& state) {
// déplacement de la balle après appui sur les touches de direction // déplacement de la balle après appui sur les touches de direction
if (state.keys[sf::Keyboard::Left]) { if (state.keys[sf::Keyboard::Left]) {
forces += sf::Vector2f(-Ball::MOVE, 0); forces += sf::Vector2f(-Constants::MOVE, 0);
} }
if (state.keys[sf::Keyboard::Right]) { if (state.keys[sf::Keyboard::Right]) {
forces += sf::Vector2f(Ball::MOVE, 0); forces += sf::Vector2f(Constants::MOVE, 0);
} }
// force d'attraction entre les balles et les blocs chargés // force d'attraction entre les balles et les blocs chargés
@ -66,7 +67,7 @@ sf::Vector2f Ball::getForces(EngineState& state) {
// normalisation du vecteur direction qui porte // normalisation du vecteur direction qui porte
// la force d'attraction, puis application de la norme // la force d'attraction, puis application de la norme
attraction /= std::sqrt(distanceSquared); attraction /= std::sqrt(distanceSquared);
attraction *= Ball::ATTRACTION * ( attraction *= Constants::ATTRACTION * (
(charge * attractive->getCharge()) / (charge * attractive->getCharge()) /
distanceSquared distanceSquared
); );

View File

@ -1,7 +1,8 @@
#include "block.hpp" #include "block.hpp"
#include "constants.hpp"
Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Block::GRID, Block::GRID)) { Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Constants::GRID, Constants::GRID)) {
shape.setOrigin(sf::Vector2f(Block::GRID / 2, Block::GRID / 2)); shape.setOrigin(sf::Vector2f(Constants::GRID / 2, Constants::GRID / 2));
} }
void Block::draw(sf::RenderWindow& window) { void Block::draw(sf::RenderWindow& window) {
@ -30,9 +31,9 @@ void Block::update(EngineState& state) {
std::unique_ptr<sf::FloatRect> Block::getAABB() { std::unique_ptr<sf::FloatRect> Block::getAABB() {
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect( return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
position.x - Block::GRID / 2, position.x - Constants::GRID / 2,
position.y - Block::GRID / 2, position.y - Constants::GRID / 2,
Block::GRID, Block::GRID Constants::GRID, Constants::GRID
)); ));
} }

View File

@ -1,23 +1,24 @@
#include "ball.hpp" #include "ball.hpp"
#include "block.hpp" #include "block.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "constants.hpp"
#include <iostream> #include <iostream>
int main() { int main() {
Engine engine; Engine engine;
Ball ball1(5 * Block::GRID, 1 * Block::GRID); Ball ball1(5 * Constants::GRID, 1 * Constants::GRID);
Ball ball2(7 * Block::GRID, 1 * Block::GRID); Ball ball2(7 * Constants::GRID, 1 * Constants::GRID);
Block block1(2 * Block::GRID, 7 * Block::GRID); Block block1(2 * Constants::GRID, 7 * Constants::GRID);
Block block2(3 * Block::GRID, 7 * Block::GRID); Block block2(3 * Constants::GRID, 7 * Constants::GRID);
Block block3(4 * Block::GRID, 7 * Block::GRID); Block block3(4 * Constants::GRID, 7 * Constants::GRID);
Block block4(5 * Block::GRID, 7 * Block::GRID); Block block4(5 * Constants::GRID, 7 * Constants::GRID);
Block block5(6 * Block::GRID, 7 * Block::GRID); Block block5(6 * Constants::GRID, 7 * Constants::GRID);
Block block6(7 * Block::GRID, 7 * Block::GRID); Block block6(7 * Constants::GRID, 7 * Constants::GRID);
Block block7(8 * Block::GRID, 7 * Block::GRID); Block block7(8 * Constants::GRID, 7 * Constants::GRID);
Block block8(9 * Block::GRID, 7 * Block::GRID); Block block8(9 * Constants::GRID, 7 * Constants::GRID);
Block block9(10 * Block::GRID, 7 * Block::GRID); Block block9(10 * Constants::GRID, 7 * Constants::GRID);
ball1.setCharge(-2); ball1.setCharge(-2);
ball2.setCharge(-2); ball2.setCharge(-2);

View File

@ -1,4 +1,5 @@
#include "physics_object.hpp" #include "physics_object.hpp"
#include "constants.hpp"
PhysicsObject::PhysicsObject(float x, float y) : PhysicsObject::PhysicsObject(float x, float y) :
Object(x, y), mass(1), accelLine(sf::LinesStrip, 2), Object(x, y), mass(1), accelLine(sf::LinesStrip, 2),
@ -32,7 +33,7 @@ sf::Vector2f PhysicsObject::getForces(EngineState& state) {
sf::Vector2f forces(0, 0); sf::Vector2f forces(0, 0);
// force de gravité // force de gravité
forces += sf::Vector2f(0, PhysicsObject::GRAVITY); forces += sf::Vector2f(0, Constants::GRAVITY);
return forces; return forces;
} }