Add option to immediately set delta time step, without interpolation

This commit is contained in:
Emma Broman
2020-08-13 13:41:44 +02:00
parent 4293fad3e9
commit a636faeaac
5 changed files with 108 additions and 27 deletions

View File

@@ -114,7 +114,15 @@ openspace.setPropertyValueSingle("RenderEngine.ShowCamera", not isEnabled)]],
Key = "Right",
Name = "Next Delta Time Step (Interpolate)",
Command = "openspace.time.interpolateNextDeltaTimeStep()",
Documentation = "Smoothly interpolates the simulation speed to the next value in the list of delta time steps, if one exists.",
Documentation = "Smoothly interpolates the simulation speed to the next delta time step, if one exists.",
GuiPath = "/Simulation Speed",
Local = true
},
{
Key = "Shift+Right",
Name = "Next Delta Time Step (Immediate)",
Command = "openspace.time.setNextDeltaTimeStep()",
Documentation = "Immediately set the simulation speed to the next delta time step, if one exists.",
GuiPath = "/Simulation Speed",
Local = true
},
@@ -122,10 +130,18 @@ openspace.setPropertyValueSingle("RenderEngine.ShowCamera", not isEnabled)]],
Key = "Left",
Name = "Previous Delta Time Step (Interpolate)",
Command = "openspace.time.interpolatePreviousDeltaTimeStep()",
Documentation = "Smoothly interpolates the simulation speed to the previous value in the list of delta time steps, if one exists.",
Documentation = "Smoothly interpolates the simulation speed to the previous delta time step, if one exists.",
GuiPath = "/Simulation Speed",
Local = true
}
},
{
Key = "Shift+Left",
Name = "Previous Delta Time Step (Immediate)",
Command = "openspace.time.setPreviousDeltaTimeStep()",
Documentation = "Immediately set the simulation speed to the previous delta time step, if one exists.",
GuiPath = "/Simulation Speed",
Local = true
},
}
local DeltaTimeKeys

View File

@@ -95,11 +95,12 @@ public:
void interpolateDeltaTime(double targetDeltaTime, double durationSeconds);
void interpolatePause(bool pause, double durationSeconds);
// TEST
std::optional<double> nextDeltaTimeStep();
std::optional<double> previousDeltaTimeStep();
bool hasNextDeltaTimeStep();
bool hasPreviousDeltaTimeStep();
bool hasNextDeltaTimeStep() const;
bool hasPreviousDeltaTimeStep() const;
void setNextDeltaTimeStep();
void setPreviousDeltaTimeStep();
void interpolateNextDeltaTimeStep(double durationSeconds);
void interpolatePreviousDeltaTimeStep(double durationSeconds);

View File

@@ -186,23 +186,43 @@ scripting::LuaLibrary Time::luaLibrary() {
"Sets the amount of simulation time that happens "
"in one second of real time"
},
{
"setNextDeltaTimeStep",
&luascriptfunctions::time_setNextDeltaTimeStep,
{},
"",
"Immediately set the simulation speed to the first delta time step in "
"the list that is larger than the current choice of simulation speed, "
"if any."
},
{
"setPreviousDeltaTimeStep",
&luascriptfunctions::time_setPreviousDeltaTimeStep,
{},
"",
"Immediately set the simulation speed to the first delta time step in "
"the list that is smaller than the current choice of simulation speed. "
"if any."
},
{
"interpolateNextDeltaTimeStep",
&luascriptfunctions::time_interpolateNextDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the next delta time step in the "
"list. If an input value is given, the interpolation is done over the "
"specified number of seconds."
"Interpolate the simulation speed to the first delta time step in the "
"list that is larger than the current simulation speed, if any. If an "
"input value is given, the interpolation is done over the specified "
"number of seconds."
},
{
"interpolatePreviousDeltaTimeStep",
&luascriptfunctions::time_interpolatePreviousDeltaTimeStep,
{},
"[number]",
"Interpolate the simulation speed to the previous delta time step in "
"the list. If an input value is given, the interpolation is done over "
"the specified number of seconds."
"Interpolate the simulation speed to the first delta time step in the "
"list that is smaller than the current simulation speed, if any. If an "
"input value is given, the interpolation is done over the specified "
"number of seconds."
},
{
"interpolatePause",

View File

@@ -98,11 +98,45 @@ int time_setDeltaTimeSteps(lua_State* L) {
return 0;
}
/**
* \ingroup LuaScripts
* setNextDeltaTimeStep():
* Immediately set the simulation speed to the first delta time step in the list that is
* larger than the current choice of simulation speed, if any.
*/
int time_setNextDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::time_setNextDeltaTimeStep");
global::timeManager.setNextDeltaTimeStep();
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* setPreviousDeltaTimeStep():
* Immediately set the simulation speed to the first delta time step in the list that is
* smaller than the current choice of simulation speed, if any.
*/
int time_setPreviousDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::time_setPreviousDeltaTimeStep");
global::timeManager.setPreviousDeltaTimeStep();
lua_settop(L, 0);
ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack");
return 0;
}
/**
* \ingroup LuaScripts
* interpolateNextDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the next delta time step in the list. If an input
* value is given, the interpolation is done over the specified number of seconds.
* If interpolationDuration is not provided, the interpolation time will be based on the
* `defaultDeltaTimeInterpolationDuration` property of the TimeManager.
*/
int time_interpolateNextDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(
@@ -142,6 +176,8 @@ int time_interpolateNextDeltaTimeStep(lua_State* L) {
* interpolatePreviousDeltaTimeStep([interpolationDuration]):
* Interpolate the simulation speed to the previous delta time step in the list. If an
* input value is given, the interpolation is done over the specified number of seconds.
* If interpolationDuration is not provided, the interpolation time will be based on the
* `defaultDeltaTimeInterpolationDuration` property of the TimeManager.
*/
int time_interpolatePreviousDeltaTimeStep(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(

View File

@@ -596,7 +596,10 @@ void TimeManager::interpolateDeltaTime(double newDeltaTime, double interpolation
addKeyframe(now + interpolationDuration, futureKeyframe);
}
double TimeManager::nextDeltaTimeStep() {
std::optional<double> TimeManager::nextDeltaTimeStep() {
if (!hasNextDeltaTimeStep()) {
return std::nullopt;
}
std::vector<double>::iterator nextStepIterator = std::upper_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
@@ -604,13 +607,16 @@ double TimeManager::nextDeltaTimeStep() {
);
if (nextStepIterator == _deltaTimeSteps.end()) {
return _targetDeltaTime; // not found, but gotta return something
return std::nullopt; // should not get here
}
return *nextStepIterator;
}
double TimeManager::previousDeltaTimeStep() {
std::optional<double> TimeManager::previousDeltaTimeStep() {
if (!hasPreviousDeltaTimeStep()) {
return std::nullopt;
}
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
@@ -618,32 +624,40 @@ double TimeManager::previousDeltaTimeStep() {
);
if (lowerBoundIterator == _deltaTimeSteps.begin()) {
return _targetDeltaTime; // not found, but gotta return something
return std::nullopt; // should not get here
}
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
return *prevStepIterator;
}
bool TimeManager::hasNextDeltaTimeStep() {
bool TimeManager::hasNextDeltaTimeStep() const {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime < _deltaTimeSteps.back();
}
bool TimeManager::hasPreviousDeltaTimeStep() {
bool TimeManager::hasPreviousDeltaTimeStep() const {
if (_deltaTimeSteps.empty())
return false;
return _targetDeltaTime > _deltaTimeSteps.front();
}
void TimeManager::setNextDeltaTimeStep() {
interpolateNextDeltaTimeStep(0);
}
void TimeManager::setPreviousDeltaTimeStep() {
interpolatePreviousDeltaTimeStep(0);
}
void TimeManager::interpolateNextDeltaTimeStep(double durationSeconds) {
if (!hasNextDeltaTimeStep())
return;
double nextDeltaTime = nextDeltaTimeStep();
double nextDeltaTime = nextDeltaTimeStep().value();
interpolateDeltaTime(nextDeltaTime, durationSeconds);
}
@@ -651,14 +665,8 @@ void TimeManager::interpolatePreviousDeltaTimeStep(double durationSeconds) {
if (!hasPreviousDeltaTimeStep())
return;
std::vector<double>::iterator lowerBoundIterator = std::lower_bound(
_deltaTimeSteps.begin(),
_deltaTimeSteps.end(),
_targetDeltaTime
);
std::vector<double>::iterator prevStepIterator = lowerBoundIterator - 1;
interpolateDeltaTime(*prevStepIterator, durationSeconds);
double previousDeltaTime = previousDeltaTimeStep().value();
interpolateDeltaTime(previousDeltaTime, durationSeconds);
}
void TimeManager::setPause(bool pause) {