diff --git a/src/engine/DynamicsWorld.h b/src/engine/DynamicsWorld.h index 975bf661..e9e6bae6 100644 --- a/src/engine/DynamicsWorld.h +++ b/src/engine/DynamicsWorld.h @@ -373,7 +373,7 @@ inline Vector3 DynamicsWorld::getGravity() const { /** * @param gravity The gravity vector (in meter per seconds squared) */ -inline Vector3 DynamicsWorld::setGravity(Vector3& gravity) { +inline void DynamicsWorld::setGravity(Vector3& gravity) { mGravity = gravity; } diff --git a/testbed/src/Gui.cpp b/testbed/src/Gui.cpp index 6d5ad0eb..7a1ac83f 100644 --- a/testbed/src/Gui.cpp +++ b/testbed/src/Gui.cpp @@ -107,6 +107,8 @@ void Gui::init() { void Gui::displayHeader() { + TestbedApplication& app = TestbedApplication::getInstance(); + ImVec2 buttonSize(120, 40); int display_w, display_h; @@ -122,10 +124,23 @@ void Gui::displayHeader() { ImGui::Begin("Header", NULL, ImVec2(display_w, HEADER_HEIGHT), 1.0f, window_flags); ImGui::SetWindowPos(ImVec2(0, 0)); - ImGui::Button("Play", buttonSize); ImGui::SameLine(); - ImGui::Button("Pause", buttonSize); ImGui::SameLine(); - ImGui::Button("Step", buttonSize); ImGui::SameLine(); - ImGui::Button("Restart", buttonSize); ImGui::SameLine(); + + bool isRunning = app.mTimer.isRunning(); + if (ImGui::Button(isRunning ? "Pause" : "Play", buttonSize)) { + app.togglePlayPauseSimulation(); + } + ImGui::SameLine(); + + if (ImGui::Button("Step", buttonSize)) { + app.toggleTakeSinglePhysicsStep(); + } + ImGui::SameLine(); + + if (ImGui::Button("Restart", buttonSize)) { + app.restartSimulation(); + } + ImGui::SameLine(); + ImGui::End(); ImGui::PopStyleColor(1); @@ -152,8 +167,16 @@ void Gui::displayLeftPane() { ImGui::SetWindowPos(ImVec2(0, HEADER_HEIGHT)); // ----- Left Pane Header ----- // - ImGui::Button("Scenes", buttonSize); ImGui::SameLine(); - ImGui::Button("Physics", buttonSize); ImGui::SameLine(); + if (ImGui::Button("Scenes", buttonSize)) { + mLeftPane = SCENES; + } + ImGui::SameLine(); + + if (ImGui::Button("Physics", buttonSize)) { + mLeftPane = PHYSICS; + } + ImGui::SameLine(); + ImGui::Button("Rendering", buttonSize); ImGui::SameLine(); ImGui::Button("Profiling", buttonSize); @@ -194,6 +217,11 @@ void Gui::displayScenesPane() { void Gui::displayPhysicsPane() { + TestbedApplication& app = TestbedApplication::getInstance(); + + // Physics time step + //float timestep = app.ge; + //ImGui::InputFloat("Timestep", ×tep, 0.01f, 1.0f); } void Gui::displayRenderingPane() { @@ -241,10 +269,6 @@ void Gui::render() { //glfwPollEvents(); beginNewFrame(); - bool show_test_window = true; - bool show_another_window = false; - ImVec4 clear_color = ImColor(255, 255, 255); - displayHeader(); displayLeftPane(); diff --git a/testbed/src/Scene.h b/testbed/src/Scene.h index 2f269a36..e6460805 100644 --- a/testbed/src/Scene.h +++ b/testbed/src/Scene.h @@ -35,8 +35,16 @@ struct EngineSettings { public: - float elapsedTime; // Elapsed time (in seconds) - float timeStep; // Current time step (in seconds) + float elapsedTime; // Elapsed time (in seconds) + float timeStep; // Current time step (in seconds) + int nbVelocitySolverIterations; // Nb of velocity solver iterations + int nbPositionSolverIterations; // Nb of position solver iterations + bool isSleepingEnabled; // True if sleeping technique is enabled + float timeBeforeSleep; // Time of inactivity before a body sleep + float sleepLinearVelocity; // Sleep linear velocity + float sleepAngularVelocity; // Sleep angular velocity + bool isGravityEnabled; // True if gravity is enabled + openglframework::Vector3 gravity; // Gravity vector /// Constructor EngineSettings() : elapsedTime(0.0f), timeStep(0.0f) { diff --git a/testbed/src/TestbedApplication.cpp b/testbed/src/TestbedApplication.cpp index cdc31397..d972aeb3 100644 --- a/testbed/src/TestbedApplication.cpp +++ b/testbed/src/TestbedApplication.cpp @@ -50,13 +50,15 @@ 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 = 1280; mHeight = 720; + mSinglePhysicsStepEnabled = false; + mSinglePhysicsStepDone = false; } // Destructor @@ -175,6 +177,13 @@ void TestbedApplication::destroyScenes() { mCurrentScene = NULL; } +void TestbedApplication::updateSinglePhysicsStep() { + + assert(!mTimer.isRunning()); + + mCurrentScene->updatePhysics(); +} + // Update the physics of the current scene void TestbedApplication::updatePhysics() { @@ -202,7 +211,13 @@ void TestbedApplication::updatePhysics() { void TestbedApplication::update() { // Update the physics - updatePhysics(); + if (mSinglePhysicsStepEnabled && !mSinglePhysicsStepDone) { + updateSinglePhysicsStep(); + mSinglePhysicsStepDone = true; + } + else { + updatePhysics(); + } // Compute the interpolation factor float factor = mTimer.computeInterpolationFactor(mEngineSettings.timeStep); diff --git a/testbed/src/TestbedApplication.h b/testbed/src/TestbedApplication.h index 9843653c..eb5d2276 100644 --- a/testbed/src/TestbedApplication.h +++ b/testbed/src/TestbedApplication.h @@ -81,6 +81,12 @@ class TestbedApplication { /// Width and height of the window int mWidth, mHeight; + /// True if the next simulation update is a single physics step + bool mSinglePhysicsStepEnabled; + + /// True if the single physics step has been taken already + bool mSinglePhysicsStepDone; + // -------------------- Methods -------------------- // /// Private constructor (for the singleton class) @@ -98,6 +104,9 @@ class TestbedApplication { /// Update void update(); + /// Update the simulation by taking a single physics step + void updateSinglePhysicsStep(); + /// Called when the windows is reshaped void reshape(); @@ -140,6 +149,15 @@ class TestbedApplication { /// Return the list of the scenes std::vector getScenes(); + /// Start/stop the simulation + void togglePlayPauseSimulation(); + + /// Restart the simulation + void restartSimulation(); + + /// Set the variable to know if we need to take a single physics step + void toggleTakeSinglePhysicsStep(); + public : // -------------------- Methods -------------------- // @@ -169,5 +187,31 @@ inline std::vector TestbedApplication::getScenes() { return mScenes; } +// Start the simulation +inline void TestbedApplication::togglePlayPauseSimulation() { + + if (mTimer.isRunning()) { + mTimer.stop(); + } + else { + mTimer.start(); + } +} + +// Restart the simulation +inline void TestbedApplication::restartSimulation() { + mCurrentScene->reset(); + mTimer.start(); +} + +// Take a single step of simulation +inline void TestbedApplication::toggleTakeSinglePhysicsStep() { + mSinglePhysicsStepEnabled = true; + mSinglePhysicsStepDone = false; + + if (mTimer.isRunning()) { + mSinglePhysicsStepEnabled = false; + } +} #endif