Modifications of the Timer class to allow the user to start and stop the simulation correctly
git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@375 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
parent
859d8e409b
commit
5277426ff8
|
@ -22,14 +22,21 @@
|
|||
|
||||
// We want to use the ReactPhysics3D namespace
|
||||
using namespace reactphysics3d;
|
||||
using namespace std;
|
||||
|
||||
// Constructor
|
||||
PhysicsEngine::PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument)
|
||||
: world(world), timer(0.0, timeStep), collisionDetection(world), constraintSolver(world) {
|
||||
PhysicsEngine::PhysicsEngine(PhysicsWorld* world, double timeStep) throw (invalid_argument)
|
||||
: world(world), timer(timeStep), collisionDetection(world), constraintSolver(world) {
|
||||
// Check if the pointer to the world is not NULL
|
||||
if (world == 0) {
|
||||
// Throw an exception
|
||||
throw std::invalid_argument("Exception in PhysicsEngine constructor : World pointer cannot be NULL");
|
||||
throw invalid_argument("Error : The argument world to the PhysicsEngine constructor cannot be NULL");
|
||||
}
|
||||
|
||||
// Check if the timeStep is positive
|
||||
if (timeStep <= 0.0) {
|
||||
// Throw an exception
|
||||
throw invalid_argument("Error : The timeStep argument to the PhysicsEngine constructor have to be greater than zero");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,15 +45,21 @@ PhysicsEngine::~PhysicsEngine() {
|
|||
|
||||
}
|
||||
|
||||
void PhysicsEngine::update() {
|
||||
// Update the physics simulation
|
||||
void PhysicsEngine::update() throw (logic_error) {
|
||||
bool existCollision = false;
|
||||
|
||||
// Check that the timer is running
|
||||
if (timer.getIsRunning()) {
|
||||
|
||||
// Compute the time since the last update() call and update the timer
|
||||
timer.update();
|
||||
|
||||
// Apply the gravity force to all bodies
|
||||
applyGravity();
|
||||
|
||||
// While the time accumulator is not empty
|
||||
while(timer.getAccumulator() >= timer.getTimeStep()) {
|
||||
while(timer.isPossibleToTakeStep()) {
|
||||
existCollision = false;
|
||||
// Compute the collision detection
|
||||
if (collisionDetection.computeCollisionDetection()) {
|
||||
|
@ -57,7 +70,7 @@ void PhysicsEngine::update() {
|
|||
}
|
||||
|
||||
// Update the timer
|
||||
timer.update();
|
||||
timer.nextStep();
|
||||
|
||||
// Update the position and orientation of each body
|
||||
updateAllBodiesMotion();
|
||||
|
@ -71,6 +84,10 @@ void PhysicsEngine::update() {
|
|||
world->clearAddedAndRemovedBodies();
|
||||
}
|
||||
}
|
||||
else { // Call to update() but the timer is not running
|
||||
// Throw an exception
|
||||
throw logic_error("Error : The PhysicsEngine::start() method have to be called before calling PhysicsEngine::update()");
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the motion of all bodies and update their positions and orientations
|
||||
|
@ -88,7 +105,7 @@ void PhysicsEngine::updateAllBodiesMotion() {
|
|||
Vector3D newAngularVelocity;
|
||||
|
||||
// For each body of thephysics world
|
||||
for (std::vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
||||
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
||||
|
||||
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
||||
assert(rigidBody);
|
||||
|
@ -155,7 +172,7 @@ void PhysicsEngine::updateAllBodiesMotion() {
|
|||
void PhysicsEngine::applyGravity() {
|
||||
|
||||
// For each body of the physics world
|
||||
for (std::vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
||||
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
||||
|
||||
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
||||
assert(rigidBody);
|
||||
|
|
|
@ -47,35 +47,23 @@ class PhysicsEngine {
|
|||
void updatePositionAndOrientationOfBody(Body* body, const Vector3D& newLinVelocity, const Vector3D& newAngVelocity); // Update the position and orientation of a body
|
||||
void applyGravity(); // Apply the gravity force to all bodies
|
||||
public :
|
||||
PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument); // Constructor
|
||||
~PhysicsEngine(); // Destructor
|
||||
PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument); // Constructor
|
||||
~PhysicsEngine(); // Destructor
|
||||
|
||||
void start(); // Start the physics simulation
|
||||
void stop(); // Stop the physics simulation
|
||||
void update(); // Update the physics simulation
|
||||
void initializeDisplayTime(long double displayTime); // Initialize the display time
|
||||
void updateDisplayTime(long double newDisplayTime); // Update the display time
|
||||
void start(); // Start the physics simulation
|
||||
void stop(); // Stop the physics simulation
|
||||
void update() throw (std::logic_error); // Update the physics simulation
|
||||
};
|
||||
|
||||
// --- Inline functions --- //
|
||||
|
||||
// Start the physics simulation
|
||||
inline void PhysicsEngine::start() {
|
||||
timer.setIsRunning(true);
|
||||
timer.start();
|
||||
}
|
||||
|
||||
inline void PhysicsEngine::stop() {
|
||||
timer.setIsRunning(false);
|
||||
}
|
||||
|
||||
// Initialize the display time
|
||||
inline void PhysicsEngine::initializeDisplayTime(long double displayTime) {
|
||||
timer.setCurrentDisplayTime(displayTime);
|
||||
}
|
||||
|
||||
// Update the display time
|
||||
inline void PhysicsEngine::updateDisplayTime(long double newDisplayTime) {
|
||||
timer.updateDisplayTime(newDisplayTime);
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as published *
|
||||
* by the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as published *
|
||||
* by the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "Timer.h"
|
||||
|
@ -24,25 +24,8 @@
|
|||
using namespace reactphysics3d;
|
||||
|
||||
// Constructor
|
||||
Timer::Timer(long double initialTime, double timeStep) throw(std::invalid_argument)
|
||||
: timeStep(timeStep), time(initialTime), currentDisplayTime(0.0), deltaDisplayTime(0.0) {
|
||||
// Check if the timestep is different from zero
|
||||
if (timeStep != 0.0) {
|
||||
accumulator = 0.0;
|
||||
isRunning = false;
|
||||
}
|
||||
else {
|
||||
// We throw an exception
|
||||
throw std::invalid_argument("Exception in Timer constructor : The timestep has to be different from zero");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Copy-constructor
|
||||
Timer::Timer(const Timer& timer)
|
||||
: timeStep(timer.timeStep), time(timer.time), currentDisplayTime(timer.currentDisplayTime),
|
||||
deltaDisplayTime(timer.deltaDisplayTime), accumulator(timer.accumulator) {
|
||||
isRunning = timer.isRunning;
|
||||
Timer::Timer(double timeStep) : timeStep(timeStep), isRunning(false) {
|
||||
assert(timeStep > 0.0);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
|
|
|
@ -1,31 +1,33 @@
|
|||
/****************************************************************************
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as published *
|
||||
* by the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
* Copyright (C) 2009 Daniel Chappuis *
|
||||
****************************************************************************
|
||||
* This file is part of ReactPhysics3D. *
|
||||
* *
|
||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Lesser General Public License as published *
|
||||
* by the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public License *
|
||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
// Libraries
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
// Libraries
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cassert>
|
||||
|
||||
// Namespace ReactPhysics3D
|
||||
namespace reactphysics3d {
|
||||
// Namespace ReactPhysics3D
|
||||
namespace reactphysics3d {
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
Class Timer :
|
||||
|
@ -36,28 +38,25 @@ class Timer {
|
|||
private :
|
||||
double timeStep; // Timestep dt of the physics engine (timestep > 0.0)
|
||||
long double time; // Current time of the physics engine
|
||||
long double currentDisplayTime; // Current display time
|
||||
long double deltaDisplayTime; // Current time difference between two display frames
|
||||
long double lastUpdateTime; // Last time the timer has been updated
|
||||
long double deltaTime; // Time difference between the two last timer update() calls
|
||||
double accumulator; // Used to fix the time step and avoid strange time effects
|
||||
bool isRunning; // True if the timer is running
|
||||
|
||||
public :
|
||||
Timer(long double initialTime, double timeStep) throw(std::invalid_argument); // Constructor
|
||||
Timer(const Timer& timer); // Copy-constructor
|
||||
virtual ~Timer(); // Destructor
|
||||
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 setTime(long double time); // Set the current time
|
||||
bool getIsRunning() const; // Return if the timer is running
|
||||
void setIsRunning(bool isRunning); // Set if the timer is running
|
||||
double getAccumulator() const; // Return the accumulator value
|
||||
void setCurrentDisplayTime(long double displayTime); // Set the current display time
|
||||
|
||||
void update(); // Update the timer by adding some time value (or timeStep by default) to the current time
|
||||
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
|
||||
double getInterpolationFactor() const; // Compute and return the interpolation factor between two body states
|
||||
void updateDisplayTime(long double newDisplayTime); // Set the new currentDisplayTime value
|
||||
};
|
||||
|
||||
// --- Inline functions --- //
|
||||
|
@ -84,59 +83,64 @@ inline long double Timer::getTime() const {
|
|||
return time;
|
||||
}
|
||||
|
||||
// Set the current time
|
||||
inline void Timer::setTime(long double time) {
|
||||
this->time = time;
|
||||
}
|
||||
|
||||
// Return if the timer is running
|
||||
inline bool Timer::getIsRunning() const {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
// Set if the timer is running
|
||||
inline void Timer::setIsRunning(bool isRunning) {
|
||||
this->isRunning = isRunning;
|
||||
}
|
||||
|
||||
// Return the accumulator value
|
||||
inline double Timer::getAccumulator() const {
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
// Set the current display time
|
||||
inline void Timer::setCurrentDisplayTime(long double currentDisplayTime) {
|
||||
this->currentDisplayTime = currentDisplayTime;
|
||||
}
|
||||
|
||||
// Update the timer by adding the timeStep to the current time
|
||||
inline void Timer::update() {
|
||||
// Check if the timer is running
|
||||
if (isRunning) {
|
||||
// Update the current time of the physics engine
|
||||
time += timeStep;
|
||||
|
||||
// Update the accumulator value
|
||||
accumulator -= timeStep;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Stop the timer
|
||||
inline void Timer::stop() {
|
||||
if (isRunning) {
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
// True if it's possible to take a new step
|
||||
inline bool Timer::isPossibleToTakeStep() const {
|
||||
return accumulator >= timeStep;
|
||||
}
|
||||
|
||||
// Take a new step => update the timer by adding the timeStep value to the current time
|
||||
inline void Timer::nextStep() {
|
||||
assert(isRunning);
|
||||
|
||||
// Update the current time of the physics engine
|
||||
time += timeStep;
|
||||
|
||||
// Update the accumulator value
|
||||
accumulator -= timeStep;
|
||||
}
|
||||
|
||||
// Compute and return the interpolation factor between two body states
|
||||
inline double Timer::getInterpolationFactor() const {
|
||||
// Compute and return the interpolation factor
|
||||
return (accumulator / timeStep);
|
||||
}
|
||||
|
||||
// Set the new currentDisplayTime value
|
||||
inline void Timer::updateDisplayTime(long double newDisplayTime) {
|
||||
// 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);
|
||||
|
||||
// Compute the delta display time between two display frames
|
||||
deltaDisplayTime = newDisplayTime - currentDisplayTime;
|
||||
deltaTime = currentTime - lastUpdateTime;
|
||||
|
||||
// Update the current display time
|
||||
currentDisplayTime = newDisplayTime;
|
||||
lastUpdateTime = currentTime;
|
||||
|
||||
// Update the accumulator value
|
||||
accumulator += deltaDisplayTime;
|
||||
accumulator += deltaTime;
|
||||
}
|
||||
|
||||
} // End of the ReactPhysics3D namespace
|
||||
|
|
Loading…
Reference in New Issue
Block a user