2010-09-09 19:41:14 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal *
|
|
|
|
* in the Software without restriction, including without limitation the rights *
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included in *
|
|
|
|
* all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
|
|
|
* THE SOFTWARE. *
|
|
|
|
********************************************************************************/
|
2010-08-18 14:50:36 +00:00
|
|
|
|
|
|
|
#ifndef TIMER_H
|
|
|
|
#define TIMER_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
// Namespace ReactPhysics3D
|
|
|
|
namespace reactphysics3d {
|
2009-02-10 11:54:56 +00:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
Class Timer :
|
|
|
|
This class will take care of the time in the physics engine.
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Timer {
|
|
|
|
private :
|
2010-07-28 11:19:00 +00:00
|
|
|
double timeStep; // Timestep dt of the physics engine (timestep > 0.0)
|
|
|
|
long double time; // Current time of the physics engine
|
2010-08-18 14:50:36 +00:00
|
|
|
long double lastUpdateTime; // Last time the timer has been updated
|
|
|
|
long double deltaTime; // Time difference between the two last timer update() calls
|
2010-07-28 11:19:00 +00:00
|
|
|
double accumulator; // Used to fix the time step and avoid strange time effects
|
|
|
|
bool isRunning; // True if the timer is running
|
2009-02-10 11:54:56 +00:00
|
|
|
|
|
|
|
public :
|
2010-08-18 14:50:36 +00:00
|
|
|
Timer(double timeStep); // Constructor
|
|
|
|
virtual ~Timer(); // Destructor
|
|
|
|
|
|
|
|
double getTimeStep() const; // Return the timestep of the physics engine
|
|
|
|
void setTimeStep(double timeStep) throw(std::invalid_argument); // Set the timestep of the physics engine
|
|
|
|
long double getTime() const; // Return the current time
|
|
|
|
void start(); // Start the timer
|
|
|
|
void stop(); // Stop the timer
|
|
|
|
bool getIsRunning() const; // Return true if the timer is running
|
|
|
|
bool isPossibleToTakeStep() const; // True if it's possible to take a new step
|
|
|
|
void update(); // Compute the time since the last update() call and add it to the accumulator
|
|
|
|
void nextStep(); // Take a new step => update the timer by adding the timeStep value to the current time
|
2010-09-01 20:59:35 +00:00
|
|
|
double computeInterpolationFactor(); // Compute the interpolation factor
|
2009-02-10 11:54:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Inline functions --- //
|
|
|
|
|
|
|
|
// Return the timestep of the physics engine
|
2010-07-28 11:19:00 +00:00
|
|
|
inline double Timer::getTimeStep() const {
|
2009-02-10 11:54:56 +00:00
|
|
|
return timeStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the timestep of the physics engine
|
2010-07-28 11:19:00 +00:00
|
|
|
inline void Timer::setTimeStep(double timeStep) throw(std::invalid_argument) {
|
2009-02-10 11:54:56 +00:00
|
|
|
// Check if the timestep is different from zero
|
2010-07-28 11:19:00 +00:00
|
|
|
if (timeStep != 0.0) {
|
2009-02-10 11:54:56 +00:00
|
|
|
this->timeStep = timeStep;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We throw an exception
|
|
|
|
throw std::invalid_argument("Exception in Timer : the timestep has to be different from zero");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current time
|
2010-07-28 11:19:00 +00:00
|
|
|
inline long double Timer::getTime() const {
|
2009-02-10 11:54:56 +00:00
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if the timer is running
|
|
|
|
inline bool Timer::getIsRunning() const {
|
|
|
|
return isRunning;
|
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Start the timer
|
|
|
|
inline void Timer::start() {
|
|
|
|
if (!isRunning) {
|
|
|
|
// Initialize the lastUpdateTime with the current time in seconds
|
|
|
|
lastUpdateTime = std::clock() / double(CLOCKS_PER_SEC);
|
|
|
|
accumulator = 0.0;
|
|
|
|
isRunning = true;
|
|
|
|
}
|
2009-02-10 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Stop the timer
|
|
|
|
inline void Timer::stop() {
|
|
|
|
if (isRunning) {
|
|
|
|
isRunning = false;
|
|
|
|
}
|
2009-02-10 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// True if it's possible to take a new step
|
|
|
|
inline bool Timer::isPossibleToTakeStep() const {
|
2010-09-01 20:59:35 +00:00
|
|
|
return (accumulator >= timeStep);
|
2009-02-13 11:26:15 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Take a new step => update the timer by adding the timeStep value to the current time
|
|
|
|
inline void Timer::nextStep() {
|
|
|
|
assert(isRunning);
|
2009-02-12 12:38:27 +00:00
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Update the current time of the physics engine
|
|
|
|
time += timeStep;
|
|
|
|
|
|
|
|
// Update the accumulator value
|
|
|
|
accumulator -= timeStep;
|
2009-02-12 12:38:27 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 20:59:35 +00:00
|
|
|
// Compute the interpolation factor
|
|
|
|
inline double Timer::computeInterpolationFactor() {
|
2010-07-28 11:19:00 +00:00
|
|
|
return (accumulator / timeStep);
|
2009-02-12 12:38:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Compute the time since the last update() call and add it to the accumulator
|
|
|
|
inline void Timer::update() {
|
|
|
|
|
|
|
|
// Compute the current time is seconds
|
|
|
|
long double currentTime = std::clock() / double(CLOCKS_PER_SEC);
|
|
|
|
|
2009-02-12 12:38:27 +00:00
|
|
|
// Compute the delta display time between two display frames
|
2010-08-18 14:50:36 +00:00
|
|
|
deltaTime = currentTime - lastUpdateTime;
|
2009-02-12 12:38:27 +00:00
|
|
|
|
|
|
|
// Update the current display time
|
2010-08-18 14:50:36 +00:00
|
|
|
lastUpdateTime = currentTime;
|
2009-02-12 12:38:27 +00:00
|
|
|
|
|
|
|
// Update the accumulator value
|
2010-08-18 14:50:36 +00:00
|
|
|
accumulator += deltaTime;
|
2009-02-12 12:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of the ReactPhysics3D namespace
|
2009-02-10 11:54:56 +00:00
|
|
|
|
|
|
|
#endif
|