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
|
// We want to use the ReactPhysics3D namespace
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
PhysicsEngine::PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument)
|
PhysicsEngine::PhysicsEngine(PhysicsWorld* world, double timeStep) throw (invalid_argument)
|
||||||
: world(world), timer(0.0, timeStep), collisionDetection(world), constraintSolver(world) {
|
: world(world), timer(timeStep), collisionDetection(world), constraintSolver(world) {
|
||||||
// Check if the pointer to the world is not NULL
|
// Check if the pointer to the world is not NULL
|
||||||
if (world == 0) {
|
if (world == 0) {
|
||||||
// Throw an exception
|
// 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;
|
bool existCollision = false;
|
||||||
|
|
||||||
|
// Check that the timer is running
|
||||||
if (timer.getIsRunning()) {
|
if (timer.getIsRunning()) {
|
||||||
|
|
||||||
|
// Compute the time since the last update() call and update the timer
|
||||||
|
timer.update();
|
||||||
|
|
||||||
// Apply the gravity force to all bodies
|
// Apply the gravity force to all bodies
|
||||||
applyGravity();
|
applyGravity();
|
||||||
|
|
||||||
// While the time accumulator is not empty
|
// While the time accumulator is not empty
|
||||||
while(timer.getAccumulator() >= timer.getTimeStep()) {
|
while(timer.isPossibleToTakeStep()) {
|
||||||
existCollision = false;
|
existCollision = false;
|
||||||
// Compute the collision detection
|
// Compute the collision detection
|
||||||
if (collisionDetection.computeCollisionDetection()) {
|
if (collisionDetection.computeCollisionDetection()) {
|
||||||
|
@ -57,7 +70,7 @@ void PhysicsEngine::update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the timer
|
// Update the timer
|
||||||
timer.update();
|
timer.nextStep();
|
||||||
|
|
||||||
// Update the position and orientation of each body
|
// Update the position and orientation of each body
|
||||||
updateAllBodiesMotion();
|
updateAllBodiesMotion();
|
||||||
|
@ -71,6 +84,10 @@ void PhysicsEngine::update() {
|
||||||
world->clearAddedAndRemovedBodies();
|
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
|
// Compute the motion of all bodies and update their positions and orientations
|
||||||
|
@ -88,7 +105,7 @@ void PhysicsEngine::updateAllBodiesMotion() {
|
||||||
Vector3D newAngularVelocity;
|
Vector3D newAngularVelocity;
|
||||||
|
|
||||||
// For each body of thephysics world
|
// 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);
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
||||||
assert(rigidBody);
|
assert(rigidBody);
|
||||||
|
@ -155,7 +172,7 @@ void PhysicsEngine::updateAllBodiesMotion() {
|
||||||
void PhysicsEngine::applyGravity() {
|
void PhysicsEngine::applyGravity() {
|
||||||
|
|
||||||
// For each body of the physics world
|
// 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);
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
||||||
assert(rigidBody);
|
assert(rigidBody);
|
||||||
|
|
|
@ -52,30 +52,18 @@ class PhysicsEngine {
|
||||||
|
|
||||||
void start(); // Start the physics simulation
|
void start(); // Start the physics simulation
|
||||||
void stop(); // Stop the physics simulation
|
void stop(); // Stop the physics simulation
|
||||||
void update(); // Update the physics simulation
|
void update() throw (std::logic_error); // Update the physics simulation
|
||||||
void initializeDisplayTime(long double displayTime); // Initialize the display time
|
|
||||||
void updateDisplayTime(long double newDisplayTime); // Update the display time
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Inline functions --- //
|
// --- Inline functions --- //
|
||||||
|
|
||||||
// Start the physics simulation
|
// Start the physics simulation
|
||||||
inline void PhysicsEngine::start() {
|
inline void PhysicsEngine::start() {
|
||||||
timer.setIsRunning(true);
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void PhysicsEngine::stop() {
|
inline void PhysicsEngine::stop() {
|
||||||
timer.setIsRunning(false);
|
timer.stop();
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Copyright (C) 2009 Daniel Chappuis *
|
* Copyright (C) 2009 Daniel Chappuis *
|
||||||
****************************************************************************
|
****************************************************************************
|
||||||
* This file is part of ReactPhysics3D. *
|
* This file is part of ReactPhysics3D. *
|
||||||
* *
|
* *
|
||||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||||
* it under the terms of the GNU Lesser General Public License as published *
|
* 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 *
|
* by the Free Software Foundation, either version 3 of the License, or *
|
||||||
* (at your option) any later version. *
|
* (at your option) any later version. *
|
||||||
* *
|
* *
|
||||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
* GNU Lesser General Public License for more details. *
|
* GNU Lesser General Public License for more details. *
|
||||||
* *
|
* *
|
||||||
* You should have received a copy of the GNU Lesser General Public License *
|
* You should have received a copy of the GNU Lesser General Public License *
|
||||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
|
@ -24,25 +24,8 @@
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Timer::Timer(long double initialTime, double timeStep) throw(std::invalid_argument)
|
Timer::Timer(double timeStep) : timeStep(timeStep), isRunning(false) {
|
||||||
: timeStep(timeStep), time(initialTime), currentDisplayTime(0.0), deltaDisplayTime(0.0) {
|
assert(timeStep > 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
|
|
|
@ -1,31 +1,33 @@
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Copyright (C) 2009 Daniel Chappuis *
|
* Copyright (C) 2009 Daniel Chappuis *
|
||||||
****************************************************************************
|
****************************************************************************
|
||||||
* This file is part of ReactPhysics3D. *
|
* This file is part of ReactPhysics3D. *
|
||||||
* *
|
* *
|
||||||
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
||||||
* it under the terms of the GNU Lesser General Public License as published *
|
* 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 *
|
* by the Free Software Foundation, either version 3 of the License, or *
|
||||||
* (at your option) any later version. *
|
* (at your option) any later version. *
|
||||||
* *
|
* *
|
||||||
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||||
* GNU Lesser General Public License for more details. *
|
* GNU Lesser General Public License for more details. *
|
||||||
* *
|
* *
|
||||||
* You should have received a copy of the GNU Lesser General Public License *
|
* You should have received a copy of the GNU Lesser General Public License *
|
||||||
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
***************************************************************************/
|
***************************************************************************/
|
||||||
|
|
||||||
#ifndef TIMER_H
|
#ifndef TIMER_H
|
||||||
#define TIMER_H
|
#define TIMER_H
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ctime>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
// Namespace ReactPhysics3D
|
// Namespace ReactPhysics3D
|
||||||
namespace reactphysics3d {
|
namespace reactphysics3d {
|
||||||
|
|
||||||
/* -------------------------------------------------------------------
|
/* -------------------------------------------------------------------
|
||||||
Class Timer :
|
Class Timer :
|
||||||
|
@ -36,28 +38,25 @@ class Timer {
|
||||||
private :
|
private :
|
||||||
double timeStep; // Timestep dt of the physics engine (timestep > 0.0)
|
double timeStep; // Timestep dt of the physics engine (timestep > 0.0)
|
||||||
long double time; // Current time of the physics engine
|
long double time; // Current time of the physics engine
|
||||||
long double currentDisplayTime; // Current display time
|
long double lastUpdateTime; // Last time the timer has been updated
|
||||||
long double deltaDisplayTime; // Current time difference between two display frames
|
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
|
double accumulator; // Used to fix the time step and avoid strange time effects
|
||||||
bool isRunning; // True if the timer is running
|
bool isRunning; // True if the timer is running
|
||||||
|
|
||||||
public :
|
public :
|
||||||
Timer(long double initialTime, double timeStep) throw(std::invalid_argument); // Constructor
|
Timer(double timeStep); // Constructor
|
||||||
Timer(const Timer& timer); // Copy-constructor
|
|
||||||
virtual ~Timer(); // Destructor
|
virtual ~Timer(); // Destructor
|
||||||
|
|
||||||
double getTimeStep() const; // Return the timestep of the physics engine
|
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
|
void setTimeStep(double timeStep) throw(std::invalid_argument); // Set the timestep of the physics engine
|
||||||
long double getTime() const; // Return the current time
|
long double getTime() const; // Return the current time
|
||||||
void setTime(long double time); // Set the current time
|
void start(); // Start the timer
|
||||||
bool getIsRunning() const; // Return if the timer is running
|
void stop(); // Stop the timer
|
||||||
void setIsRunning(bool isRunning); // Set if the timer is running
|
bool getIsRunning() const; // Return true if the timer is running
|
||||||
double getAccumulator() const; // Return the accumulator value
|
bool isPossibleToTakeStep() const; // True if it's possible to take a new step
|
||||||
void setCurrentDisplayTime(long double displayTime); // Set the current display time
|
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
|
||||||
void update(); // Update the timer by adding some time value (or timeStep by default) to the current time
|
|
||||||
double getInterpolationFactor() const; // Compute and return the interpolation factor between two body states
|
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 --- //
|
// --- Inline functions --- //
|
||||||
|
@ -84,41 +83,42 @@ inline long double Timer::getTime() const {
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the current time
|
|
||||||
inline void Timer::setTime(long double time) {
|
|
||||||
this->time = time;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return if the timer is running
|
// Return if the timer is running
|
||||||
inline bool Timer::getIsRunning() const {
|
inline bool Timer::getIsRunning() const {
|
||||||
return isRunning;
|
return isRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set if the timer is running
|
// Start the timer
|
||||||
inline void Timer::setIsRunning(bool isRunning) {
|
inline void Timer::start() {
|
||||||
this->isRunning = isRunning;
|
if (!isRunning) {
|
||||||
|
// Initialize the lastUpdateTime with the current time in seconds
|
||||||
|
lastUpdateTime = std::clock() / double(CLOCKS_PER_SEC);
|
||||||
|
accumulator = 0.0;
|
||||||
|
isRunning = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the accumulator value
|
// Stop the timer
|
||||||
inline double Timer::getAccumulator() const {
|
inline void Timer::stop() {
|
||||||
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) {
|
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
|
// Update the current time of the physics engine
|
||||||
time += timeStep;
|
time += timeStep;
|
||||||
|
|
||||||
// Update the accumulator value
|
// Update the accumulator value
|
||||||
accumulator -= timeStep;
|
accumulator -= timeStep;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute and return the interpolation factor between two body states
|
// Compute and return the interpolation factor between two body states
|
||||||
|
@ -127,16 +127,20 @@ inline double Timer::getInterpolationFactor() const {
|
||||||
return (accumulator / timeStep);
|
return (accumulator / timeStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the new currentDisplayTime value
|
// Compute the time since the last update() call and add it to the accumulator
|
||||||
inline void Timer::updateDisplayTime(long double newDisplayTime) {
|
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
|
// Compute the delta display time between two display frames
|
||||||
deltaDisplayTime = newDisplayTime - currentDisplayTime;
|
deltaTime = currentTime - lastUpdateTime;
|
||||||
|
|
||||||
// Update the current display time
|
// Update the current display time
|
||||||
currentDisplayTime = newDisplayTime;
|
lastUpdateTime = currentTime;
|
||||||
|
|
||||||
// Update the accumulator value
|
// Update the accumulator value
|
||||||
accumulator += deltaDisplayTime;
|
accumulator += deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // End of the ReactPhysics3D namespace
|
} // End of the ReactPhysics3D namespace
|
||||||
|
|
Loading…
Reference in New Issue
Block a user