Fixation de l'étape temporelle de la simulation

This commit is contained in:
Mattéo Delabre 2016-03-19 23:47:23 +01:00
parent ecc6a3d006
commit 4d3fc1851d
4 changed files with 20 additions and 6 deletions

View File

@ -34,6 +34,11 @@ namespace Constants {
*/
static constexpr bool DEBUG_MODE = false;
/**
* Durée fixe d'une étape de simulation physique
*/
static constexpr float PHYSICS_TIME = 1.f / 60;
/**
* Correction positionnelle : pourcentage de correction
* et seuil de correction

View File

@ -16,9 +16,8 @@ class Object;
struct EngineState {
std::vector<Object*> objects;
std::array<bool, sf::Keyboard::KeyCount> keys;
float delta;
EngineState() : delta(0.f) {
EngineState() {
// aucune touche n'est enfoncée au démarrage
keys.fill(false);
}

View File

@ -1,4 +1,5 @@
#include "engine.hpp"
#include "constants.hpp"
#include <cmath>
#include <queue>
@ -11,6 +12,8 @@ Engine::Engine() : window(
}
void Engine::start() {
float accumulator = 0;
// boucle d'événements sur la fenêtre
while (window.isOpen()) {
sf::Event event;
@ -32,9 +35,16 @@ void Engine::start() {
}
}
state.delta = clock.restart().asSeconds();
float frame = clock.restart().asSeconds();
accumulator += frame;
// tant qu'il reste du temps à passer,
// effectuer la simulation physique étape par étape
while (accumulator >= Constants::PHYSICS_TIME) {
accumulator -= Constants::PHYSICS_TIME;
update();
}
update();
draw();
}
}

View File

@ -80,11 +80,11 @@ void Object::update(EngineState& state) {
}
// intégration de la vitesse dans la position
position += velocity * state.delta;
position += velocity * Constants::PHYSICS_TIME;
// intégration des forces appliquées sur l'objet dans la vitesse
acceleration = getForces(state) / mass;
velocity += acceleration * state.delta;
velocity += acceleration * Constants::PHYSICS_TIME;
}
bool Object::getCollisionInfo(Object& obj, sf::Vector2f& normal, float& depth) {