Effectue la conversion du temps en secondes en dernière instance
Pour une meilleure précision
This commit is contained in:
		
							parent
							
								
									b3d2c50314
								
							
						
					
					
						commit
						53c4573edb
					
				|  | @ -27,7 +27,7 @@ namespace Constants { | |||
|     /**
 | ||||
|      * Durée fixe d'une étape de simulation physique | ||||
|      */ | ||||
|     static const float PHYSICS_TIME = 1.f / 60; | ||||
|     static const sf::Time PHYSICS_TIME = sf::seconds(1.f / 60); | ||||
| 
 | ||||
|     /**
 | ||||
|      * Correction positionnelle : pourcentage de correction | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ private: | |||
|     std::string level_name; | ||||
|     sf::Sprite background; | ||||
| 
 | ||||
|     float accumulator; | ||||
|     sf::Time accumulator; | ||||
| 
 | ||||
|     std::vector<ObjectPtr> objects; | ||||
|     std::vector<std::pair<float, float>> level_zone; | ||||
|  |  | |||
|  | @ -14,7 +14,7 @@ private: | |||
|     sf::RenderWindow window; | ||||
|     sf::Clock clock; | ||||
| 
 | ||||
|     float elapsed_time; | ||||
|     sf::Time elapsed_time; | ||||
|     ResourceManager resource_manager; | ||||
|     std::array<bool, sf::Keyboard::KeyCount> keys; | ||||
| 
 | ||||
|  | @ -42,7 +42,7 @@ public: | |||
|      * Renvoie le temps écoulé entre la frame précédente | ||||
|      * et la frame actuelle | ||||
|      */ | ||||
|     float getElapsedTime() const; | ||||
|     sf::Time getElapsedTime() const; | ||||
| 
 | ||||
|     /**
 | ||||
|      * Renvoie le gestionnaire de ressources | ||||
|  |  | |||
							
								
								
									
										13
									
								
								src/game.cpp
								
								
								
								
							
							
						
						
									
										13
									
								
								src/game.cpp
								
								
								
								
							|  | @ -19,7 +19,7 @@ std::map<unsigned int, std::function<std::shared_ptr<Object>(std::ifstream&)>> o | |||
|     {Block::TYPE_ID, Block::load} | ||||
| }; | ||||
| 
 | ||||
| Game::Game(Manager& manager) : View(manager), accumulator(0.f) {} | ||||
| Game::Game(Manager& manager) : View(manager), accumulator(sf::Time::Zero) {} | ||||
| Game::~Game() { | ||||
|     objects.clear(); | ||||
| } | ||||
|  | @ -109,16 +109,15 @@ void Game::save() { | |||
| } | ||||
| 
 | ||||
| void Game::frame() { | ||||
|     accumulator += manager.getElapsedTime(); | ||||
| 
 | ||||
|     // tant qu'il reste du temps à passer,
 | ||||
|     // effectuer la simulation physique étape par étape
 | ||||
|     while (accumulator >= Constants::PHYSICS_TIME) { | ||||
|     while (accumulator > Constants::PHYSICS_TIME) { | ||||
|         accumulator -= Constants::PHYSICS_TIME; | ||||
|         update(); | ||||
|     } | ||||
| 
 | ||||
|     draw(); | ||||
|     accumulator += manager.getElapsedTime(); | ||||
| } | ||||
| 
 | ||||
| void Game::update() { | ||||
|  | @ -140,7 +139,7 @@ void Game::update() { | |||
| 
 | ||||
|     // intégration des forces dans la vitesse (première moitié)
 | ||||
|     for (unsigned int i = 0; i < objects.size(); i++) { | ||||
|         objects[i]->updateVelocity(manager, objects, Constants::PHYSICS_TIME / 2); | ||||
|         objects[i]->updateVelocity(manager, objects, Constants::PHYSICS_TIME.asSeconds() / 2); | ||||
|     } | ||||
| 
 | ||||
|     // résolution des collisions détectées
 | ||||
|  | @ -151,7 +150,7 @@ void Game::update() { | |||
| 
 | ||||
|     // intégration de la vitesse dans la position
 | ||||
|     for (unsigned int i = 0; i < objects.size(); i++) { | ||||
|         objects[i]->updatePosition(Constants::PHYSICS_TIME); | ||||
|         objects[i]->updatePosition(Constants::PHYSICS_TIME.asSeconds()); | ||||
|     } | ||||
| 
 | ||||
|     // application de la correction positionnelle
 | ||||
|  | @ -164,7 +163,7 @@ void Game::update() { | |||
| 
 | ||||
|     // intégration des forces dans la vitesse (seconde moitié)
 | ||||
|     for (unsigned int i = 0; i < objects.size(); i++) { | ||||
|         objects[i]->updateVelocity(manager, objects, Constants::PHYSICS_TIME / 2); | ||||
|         objects[i]->updateVelocity(manager, objects, Constants::PHYSICS_TIME.asSeconds() / 2); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -3,7 +3,7 @@ | |||
| Manager::Manager() : window( | ||||
|     sf::VideoMode(704, 480), "Projet CMI", sf::Style::Default, | ||||
|     sf::ContextSettings(0, 0, 2) | ||||
| ), elapsed_time(0.f), view(NULL) { | ||||
| ), elapsed_time(sf::Time::Zero), view(NULL) { | ||||
|     keys.fill(false); | ||||
| } | ||||
| 
 | ||||
|  | @ -29,11 +29,11 @@ void Manager::start() { | |||
|             } | ||||
| 
 | ||||
|             // lorsque la fenêtre est redimensionnée par l'utilisateur
 | ||||
|             if (event.type == sf::Event::Resized){ | ||||
|             if (event.type == sf::Event::Resized) { | ||||
|                 // mise à jour de la caméra en fonction de la taille de la fenêtre
 | ||||
|                 sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height); | ||||
|       			window.setView(sf::View(visibleArea)); | ||||
|   		  	} | ||||
|                 window.setView(sf::View(visibleArea)); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // demande à la vue de se mettre à jour sur
 | ||||
|  | @ -42,7 +42,7 @@ void Manager::start() { | |||
|             throw std::runtime_error("Aucune vue à afficher pour le jeu"); | ||||
|         } | ||||
| 
 | ||||
|         elapsed_time = clock.restart().asSeconds(); | ||||
|         elapsed_time = clock.restart(); | ||||
|         view->frame(); | ||||
|     } | ||||
| } | ||||
|  | @ -55,7 +55,7 @@ sf::RenderWindow& Manager::getWindow() { | |||
|     return window; | ||||
| } | ||||
| 
 | ||||
| float Manager::getElapsedTime() const { | ||||
| sf::Time Manager::getElapsedTime() const { | ||||
|     return elapsed_time; | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -65,7 +65,7 @@ void Player::draw(Manager& manager) { | |||
|     ); | ||||
| 
 | ||||
|     // déplacement du sprite à la position de la balle
 | ||||
|     sprite.rotate(getVelocity().x * Constants::PHYSICS_TIME * .5f); | ||||
|     sprite.rotate(getVelocity().x * Constants::PHYSICS_TIME.asSeconds() * .5f); | ||||
|     sprite.setPosition(getPosition()); | ||||
|     manager.getWindow().draw(sprite); | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue