Centralisation des constantes dans un en-tête constants
This commit is contained in:
parent
cb0dd83817
commit
a25a637a35
|
@ -16,9 +16,6 @@ protected:
|
|||
*/
|
||||
virtual sf::Vector2f getForces(EngineState& state);
|
||||
|
||||
static constexpr float ATTRACTION = 25000;
|
||||
static constexpr float MOVE = 100;
|
||||
|
||||
public:
|
||||
Ball(float x, float y);
|
||||
|
||||
|
|
|
@ -12,8 +12,6 @@ protected:
|
|||
sf::RectangleShape shape;
|
||||
|
||||
public:
|
||||
static constexpr float GRID = 32;
|
||||
|
||||
Block(float x, float y);
|
||||
|
||||
/**
|
||||
|
|
|
@ -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
|
|
@ -19,8 +19,6 @@ protected:
|
|||
*/
|
||||
virtual sf::Vector2f getForces(EngineState& state);
|
||||
|
||||
static constexpr float GRAVITY = 20;
|
||||
|
||||
public:
|
||||
PhysicsObject(float x, float y);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "ball.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
Ball::Ball(float x, float y) : PhysicsObject(x, y), shape(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
|
||||
if (state.keys[sf::Keyboard::Left]) {
|
||||
forces += sf::Vector2f(-Ball::MOVE, 0);
|
||||
forces += sf::Vector2f(-Constants::MOVE, 0);
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -66,7 +67,7 @@ sf::Vector2f Ball::getForces(EngineState& state) {
|
|||
// normalisation du vecteur direction qui porte
|
||||
// la force d'attraction, puis application de la norme
|
||||
attraction /= std::sqrt(distanceSquared);
|
||||
attraction *= Ball::ATTRACTION * (
|
||||
attraction *= Constants::ATTRACTION * (
|
||||
(charge * attractive->getCharge()) /
|
||||
distanceSquared
|
||||
);
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include "block.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Block::GRID, Block::GRID)) {
|
||||
shape.setOrigin(sf::Vector2f(Block::GRID / 2, Block::GRID / 2));
|
||||
Block::Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Constants::GRID, Constants::GRID)) {
|
||||
shape.setOrigin(sf::Vector2f(Constants::GRID / 2, Constants::GRID / 2));
|
||||
}
|
||||
|
||||
void Block::draw(sf::RenderWindow& window) {
|
||||
|
@ -30,9 +31,9 @@ void Block::update(EngineState& state) {
|
|||
|
||||
std::unique_ptr<sf::FloatRect> Block::getAABB() {
|
||||
return std::unique_ptr<sf::FloatRect>(new sf::FloatRect(
|
||||
position.x - Block::GRID / 2,
|
||||
position.y - Block::GRID / 2,
|
||||
Block::GRID, Block::GRID
|
||||
position.x - Constants::GRID / 2,
|
||||
position.y - Constants::GRID / 2,
|
||||
Constants::GRID, Constants::GRID
|
||||
));
|
||||
}
|
||||
|
||||
|
|
23
src/main.cpp
23
src/main.cpp
|
@ -1,23 +1,24 @@
|
|||
#include "ball.hpp"
|
||||
#include "block.hpp"
|
||||
#include "engine.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
Engine engine;
|
||||
|
||||
Ball ball1(5 * Block::GRID, 1 * Block::GRID);
|
||||
Ball ball2(7 * Block::GRID, 1 * Block::GRID);
|
||||
Ball ball1(5 * Constants::GRID, 1 * Constants::GRID);
|
||||
Ball ball2(7 * Constants::GRID, 1 * Constants::GRID);
|
||||
|
||||
Block block1(2 * Block::GRID, 7 * Block::GRID);
|
||||
Block block2(3 * Block::GRID, 7 * Block::GRID);
|
||||
Block block3(4 * Block::GRID, 7 * Block::GRID);
|
||||
Block block4(5 * Block::GRID, 7 * Block::GRID);
|
||||
Block block5(6 * Block::GRID, 7 * Block::GRID);
|
||||
Block block6(7 * Block::GRID, 7 * Block::GRID);
|
||||
Block block7(8 * Block::GRID, 7 * Block::GRID);
|
||||
Block block8(9 * Block::GRID, 7 * Block::GRID);
|
||||
Block block9(10 * Block::GRID, 7 * Block::GRID);
|
||||
Block block1(2 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block2(3 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block3(4 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block4(5 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block5(6 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block6(7 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block7(8 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block8(9 * Constants::GRID, 7 * Constants::GRID);
|
||||
Block block9(10 * Constants::GRID, 7 * Constants::GRID);
|
||||
|
||||
ball1.setCharge(-2);
|
||||
ball2.setCharge(-2);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "physics_object.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
PhysicsObject::PhysicsObject(float x, float y) :
|
||||
Object(x, y), mass(1), accelLine(sf::LinesStrip, 2),
|
||||
|
@ -32,7 +33,7 @@ sf::Vector2f PhysicsObject::getForces(EngineState& state) {
|
|||
sf::Vector2f forces(0, 0);
|
||||
|
||||
// force de gravité
|
||||
forces += sf::Vector2f(0, PhysicsObject::GRAVITY);
|
||||
forces += sf::Vector2f(0, Constants::GRAVITY);
|
||||
|
||||
return forces;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue