fix issue with interpolation in testbed application
This commit is contained in:
parent
e35db3f150
commit
28b7a0c059
|
@ -216,6 +216,7 @@ void Box::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the box
|
// Compute the transform used for rendering the box
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -172,6 +172,7 @@ void Capsule::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
// Compute the transform used for rendering the sphere
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -173,6 +173,8 @@ void Cone::updateTransform(float interpolationFactor) {
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the cone
|
// Compute the transform used for rendering the cone
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
interpolatedTransform.getOpenGLMatrix(matrix);
|
interpolatedTransform.getOpenGLMatrix(matrix);
|
||||||
|
|
|
@ -211,6 +211,7 @@ void ConvexMesh::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
// Compute the transform used for rendering the sphere
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -172,6 +172,7 @@ void Cylinder::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the cylinder
|
// Compute the transform used for rendering the cylinder
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -208,6 +208,7 @@ void Dumbbell::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
// Compute the transform used for rendering the sphere
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -172,6 +172,7 @@ void Sphere::updateTransform(float interpolationFactor) {
|
||||||
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform,
|
||||||
transform,
|
transform,
|
||||||
interpolationFactor);
|
interpolationFactor);
|
||||||
|
mPreviousTransform = transform;
|
||||||
|
|
||||||
// Compute the transform used for rendering the sphere
|
// Compute the transform used for rendering the sphere
|
||||||
rp3d::decimal matrix[16];
|
rp3d::decimal matrix[16];
|
||||||
|
|
|
@ -37,6 +37,11 @@ struct EngineSettings {
|
||||||
|
|
||||||
float elapsedTime; // Elapsed time (in seconds)
|
float elapsedTime; // Elapsed time (in seconds)
|
||||||
float timeStep; // Current time step (in seconds)
|
float timeStep; // Current time step (in seconds)
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
EngineSettings() : elapsedTime(0.0f), timeStep(0.0f) {
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Class Scene
|
// Class Scene
|
||||||
|
|
|
@ -50,8 +50,10 @@ TestbedApplication& TestbedApplication::getInstance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
TestbedApplication::TestbedApplication() : mFPS(0), mNbFrames(0), mPreviousTime(0) {
|
TestbedApplication::TestbedApplication() : mFPS(0), mNbFrames(0), mPreviousTime(0){
|
||||||
|
|
||||||
|
mCurrentScene = NULL;
|
||||||
|
mEngineSettings.timeStep = DEFAULT_TIMESTEP;
|
||||||
mIsMultisamplingActive = true;
|
mIsMultisamplingActive = true;
|
||||||
mWidth = 1000;
|
mWidth = 1000;
|
||||||
mHeight = 800;
|
mHeight = 800;
|
||||||
|
@ -174,22 +176,26 @@ void TestbedApplication::updatePhysics() {
|
||||||
mTimer.update();
|
mTimer.update();
|
||||||
|
|
||||||
// While the time accumulator is not empty
|
// While the time accumulator is not empty
|
||||||
while(mTimer.isPossibleToTakeStep()) {
|
while(mTimer.isPossibleToTakeStep(mEngineSettings.timeStep)) {
|
||||||
|
|
||||||
// Take a physics simulation step
|
// Take a physics simulation step
|
||||||
mCurrentScene->updatePhysics();
|
mCurrentScene->updatePhysics();
|
||||||
|
|
||||||
// Update the timer
|
// Update the timer
|
||||||
mTimer.nextStep();
|
mTimer.nextStep(mEngineSettings.timeStep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestbedApplication::update() {
|
void TestbedApplication::update() {
|
||||||
|
|
||||||
|
// Update the physics
|
||||||
|
updatePhysics();
|
||||||
|
|
||||||
// Compute the interpolation factor
|
// Compute the interpolation factor
|
||||||
float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep);
|
float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep);
|
||||||
assert(factor >= 0.0f && factor <= 1.0f);
|
assert(factor >= 0.0f && factor <= 1.0f);
|
||||||
|
std::cout << "Factor : " << factor << std::endl;
|
||||||
|
|
||||||
// Notify the scene about the interpolation factor
|
// Notify the scene about the interpolation factor
|
||||||
mCurrentScene->setInterpolationFactor(factor);
|
mCurrentScene->setInterpolationFactor(factor);
|
||||||
|
|
|
@ -33,6 +33,9 @@
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const float DEFAULT_TIMESTEP = 1.0f / 60.0f;
|
||||||
|
|
||||||
/// Class TestbedApplication
|
/// Class TestbedApplication
|
||||||
/// Singleton class representing the application.
|
/// Singleton class representing the application.
|
||||||
class TestbedApplication {
|
class TestbedApplication {
|
||||||
|
|
|
@ -52,9 +52,6 @@ class Timer {
|
||||||
|
|
||||||
// -------------------- Attributes -------------------- //
|
// -------------------- Attributes -------------------- //
|
||||||
|
|
||||||
/// Timestep dt of the physics engine (timestep > 0.0)
|
|
||||||
double mTimeStep;
|
|
||||||
|
|
||||||
/// Last time the timer has been updated
|
/// Last time the timer has been updated
|
||||||
long double mLastUpdateTime;
|
long double mLastUpdateTime;
|
||||||
|
|
||||||
|
@ -98,13 +95,13 @@ class Timer {
|
||||||
bool isRunning() const;
|
bool isRunning() const;
|
||||||
|
|
||||||
/// True if it's possible to take a new step
|
/// True if it's possible to take a new step
|
||||||
bool isPossibleToTakeStep() const;
|
bool isPossibleToTakeStep(float timeStep) const;
|
||||||
|
|
||||||
/// Compute the time since the last update() call and add it to the accumulator
|
/// Compute the time since the last update() call and add it to the accumulator
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
/// Take a new step => update the timer by adding the timeStep value to the current time
|
/// Take a new step => update the timer by adding the timeStep value to the current time
|
||||||
void nextStep();
|
void nextStep(float timeStep);
|
||||||
|
|
||||||
/// Compute the interpolation factor
|
/// Compute the interpolation factor
|
||||||
float computeInterpolationFactor(float timeStep);
|
float computeInterpolationFactor(float timeStep);
|
||||||
|
@ -141,16 +138,16 @@ inline void Timer::stop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// True if it's possible to take a new step
|
// True if it's possible to take a new step
|
||||||
inline bool Timer::isPossibleToTakeStep() const {
|
inline bool Timer::isPossibleToTakeStep(float timeStep) const {
|
||||||
return (mAccumulator >= mTimeStep);
|
return (mAccumulator >= timeStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take a new step => update the timer by adding the timeStep value to the current time
|
// Take a new step => update the timer by adding the timeStep value to the current time
|
||||||
inline void Timer::nextStep() {
|
inline void Timer::nextStep(float timeStep) {
|
||||||
assert(mIsRunning);
|
assert(mIsRunning);
|
||||||
|
|
||||||
// Update the accumulator value
|
// Update the accumulator value
|
||||||
mAccumulator -= mTimeStep;
|
mAccumulator -= timeStep;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute the interpolation factor
|
// Compute the interpolation factor
|
||||||
|
|
Loading…
Reference in New Issue
Block a user