From 28b7a0c0593d62f127b36f0559be20bbbd7de91c Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 22 Apr 2015 20:54:17 +0200 Subject: [PATCH] fix issue with interpolation in testbed application --- testbed/common/Box.cpp | 1 + testbed/common/Capsule.cpp | 1 + testbed/common/Cone.cpp | 2 ++ testbed/common/ConvexMesh.cpp | 1 + testbed/common/Cylinder.cpp | 1 + testbed/common/Dumbbell.cpp | 1 + testbed/common/Sphere.cpp | 1 + testbed/src/Scene.h | 5 +++++ testbed/src/TestbedApplication.cpp | 12 +++++++++--- testbed/src/TestbedApplication.h | 3 +++ testbed/src/Timer.h | 15 ++++++--------- 11 files changed, 31 insertions(+), 12 deletions(-) diff --git a/testbed/common/Box.cpp b/testbed/common/Box.cpp index 4a95aab0..68d213e7 100644 --- a/testbed/common/Box.cpp +++ b/testbed/common/Box.cpp @@ -216,6 +216,7 @@ void Box::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the box rp3d::decimal matrix[16]; diff --git a/testbed/common/Capsule.cpp b/testbed/common/Capsule.cpp index 7df79f85..81b6fcbe 100644 --- a/testbed/common/Capsule.cpp +++ b/testbed/common/Capsule.cpp @@ -172,6 +172,7 @@ void Capsule::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the sphere rp3d::decimal matrix[16]; diff --git a/testbed/common/Cone.cpp b/testbed/common/Cone.cpp index c6cbc070..274c18e2 100644 --- a/testbed/common/Cone.cpp +++ b/testbed/common/Cone.cpp @@ -173,6 +173,8 @@ void Cone::updateTransform(float interpolationFactor) { transform, interpolationFactor); + mPreviousTransform = transform; + // Compute the transform used for rendering the cone rp3d::decimal matrix[16]; interpolatedTransform.getOpenGLMatrix(matrix); diff --git a/testbed/common/ConvexMesh.cpp b/testbed/common/ConvexMesh.cpp index b7d9a59d..91351ece 100644 --- a/testbed/common/ConvexMesh.cpp +++ b/testbed/common/ConvexMesh.cpp @@ -211,6 +211,7 @@ void ConvexMesh::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the sphere rp3d::decimal matrix[16]; diff --git a/testbed/common/Cylinder.cpp b/testbed/common/Cylinder.cpp index a66d4664..5d11764d 100644 --- a/testbed/common/Cylinder.cpp +++ b/testbed/common/Cylinder.cpp @@ -172,6 +172,7 @@ void Cylinder::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the cylinder rp3d::decimal matrix[16]; diff --git a/testbed/common/Dumbbell.cpp b/testbed/common/Dumbbell.cpp index 3059d974..ca35377f 100644 --- a/testbed/common/Dumbbell.cpp +++ b/testbed/common/Dumbbell.cpp @@ -208,6 +208,7 @@ void Dumbbell::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the sphere rp3d::decimal matrix[16]; diff --git a/testbed/common/Sphere.cpp b/testbed/common/Sphere.cpp index 280ddd73..3fd64a98 100644 --- a/testbed/common/Sphere.cpp +++ b/testbed/common/Sphere.cpp @@ -172,6 +172,7 @@ void Sphere::updateTransform(float interpolationFactor) { rp3d::Transform interpolatedTransform = rp3d::Transform::interpolateTransforms(mPreviousTransform, transform, interpolationFactor); + mPreviousTransform = transform; // Compute the transform used for rendering the sphere rp3d::decimal matrix[16]; diff --git a/testbed/src/Scene.h b/testbed/src/Scene.h index 501aa970..e37a9e20 100644 --- a/testbed/src/Scene.h +++ b/testbed/src/Scene.h @@ -37,6 +37,11 @@ struct EngineSettings { float elapsedTime; // Elapsed time (in seconds) float timeStep; // Current time step (in seconds) + + /// Constructor + EngineSettings() : elapsedTime(0.0f), timeStep(0.0f) { + + } }; // Class Scene diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index a4ca82f8..914a28a8 100644 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -50,8 +50,10 @@ TestbedApplication& TestbedApplication::getInstance() { } // 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; mWidth = 1000; mHeight = 800; @@ -174,22 +176,26 @@ void TestbedApplication::updatePhysics() { mTimer.update(); // While the time accumulator is not empty - while(mTimer.isPossibleToTakeStep()) { + while(mTimer.isPossibleToTakeStep(mEngineSettings.timeStep)) { // Take a physics simulation step mCurrentScene->updatePhysics(); // Update the timer - mTimer.nextStep(); + mTimer.nextStep(mEngineSettings.timeStep); } } } void TestbedApplication::update() { + // Update the physics + updatePhysics(); + // Compute the interpolation factor float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep); assert(factor >= 0.0f && factor <= 1.0f); + std::cout << "Factor : " << factor << std::endl; // Notify the scene about the interpolation factor mCurrentScene->setInterpolationFactor(factor); diff --git a/testbed/src/TestbedApplication.h b/testbed/src/TestbedApplication.h index e3c5121a..f928f21e 100644 --- a/testbed/src/TestbedApplication.h +++ b/testbed/src/TestbedApplication.h @@ -33,6 +33,9 @@ #include "Timer.h" #include +// Constants +const float DEFAULT_TIMESTEP = 1.0f / 60.0f; + /// Class TestbedApplication /// Singleton class representing the application. class TestbedApplication { diff --git a/testbed/src/Timer.h b/testbed/src/Timer.h index f551ec1f..0a75d010 100644 --- a/testbed/src/Timer.h +++ b/testbed/src/Timer.h @@ -52,9 +52,6 @@ class Timer { // -------------------- Attributes -------------------- // - /// Timestep dt of the physics engine (timestep > 0.0) - double mTimeStep; - /// Last time the timer has been updated long double mLastUpdateTime; @@ -98,13 +95,13 @@ class Timer { bool isRunning() const; /// 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 void update(); /// 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 float computeInterpolationFactor(float timeStep); @@ -141,16 +138,16 @@ inline void Timer::stop() { } // True if it's possible to take a new step -inline bool Timer::isPossibleToTakeStep() const { - return (mAccumulator >= mTimeStep); +inline bool Timer::isPossibleToTakeStep(float timeStep) const { + return (mAccumulator >= timeStep); } // 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); // Update the accumulator value - mAccumulator -= mTimeStep; + mAccumulator -= timeStep; } // Compute the interpolation factor