mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-21 18:38:20 -05:00
Speed function for acceleration and deacceleration
This commit is contained in:
@@ -138,20 +138,20 @@ void AutoNavigationHandler::updateCamera(double deltaTime) {
|
||||
}
|
||||
|
||||
double prevDistance = _distanceAlongCurrentSegment;
|
||||
double displacement = deltaTime * cps.speedAt(prevDistance / cps.length());
|
||||
double displacement = deltaTime * cps.speedAtTime(_currentTime - cps.startTime());
|
||||
_distanceAlongCurrentSegment += displacement;
|
||||
|
||||
double t = _distanceAlongCurrentSegment / cps.length();
|
||||
t = std::max(0.0, std::min(t, 1.0));
|
||||
double relativeDisplacement = _distanceAlongCurrentSegment / cps.length();
|
||||
relativeDisplacement = std::max(0.0, std::min(relativeDisplacement, 1.0));
|
||||
|
||||
// TODO: don't set every frame
|
||||
// Set anchor node in orbitalNavigator, to render visible nodes and
|
||||
// add possibility to navigate when we reach the end.
|
||||
CameraState cs = (t < 0.5) ? cps.start() : cps.end();
|
||||
CameraState cs = (relativeDisplacement < 0.5) ? cps.start() : cps.end();
|
||||
global::navigationHandler.orbitalNavigator().setAnchorNode(cs.referenceNode);
|
||||
|
||||
glm::dvec3 cameraPosition = cps.getPositionAt(t);
|
||||
glm::dquat cameraRotation = cps.getRotationAt(t);
|
||||
glm::dvec3 cameraPosition = cps.getPositionAt(relativeDisplacement);
|
||||
glm::dquat cameraRotation = cps.getRotationAt(relativeDisplacement);
|
||||
|
||||
camera()->setPositionVec3(cameraPosition);
|
||||
camera()->setRotation(cameraRotation);
|
||||
|
||||
@@ -79,12 +79,28 @@ const std::vector<glm::dvec3> PathSegment::getControlPoints() const {
|
||||
return _curve->getPoints();
|
||||
}
|
||||
|
||||
// Speed function for a segment, per curve parameter t in range [0,1],
|
||||
// which is the relative arc length. OBS! If integrated over the full
|
||||
// length of the curve it must match the total duration
|
||||
const double PathSegment::speedAt(double t) const {
|
||||
// TODO: implement dampening near start and end
|
||||
return length() / _duration;
|
||||
|
||||
// Speed function for the segment, where time is a value in the range [0, duration]
|
||||
// OBS! If integrated over the curve it must match the total length or the curve
|
||||
const double PathSegment::speedAtTime(double time) const {
|
||||
|
||||
// TODO: validate time
|
||||
|
||||
double speed = speedFunction(time / _duration);
|
||||
|
||||
// apply duration constraint (eq. 14 in Eberly)
|
||||
double speedSum = 0.0;
|
||||
const int steps = 100;
|
||||
double dt = duration() / steps;
|
||||
for (double t = 0.0; t <= 1.0; t += 1.0 / steps) {
|
||||
speedSum += dt * speedFunction(t);
|
||||
}
|
||||
|
||||
// TODO: save speed sum value somewhere
|
||||
|
||||
speed = (length() * speedFunction(time / _duration)) / speedSum;
|
||||
|
||||
return speed;
|
||||
}
|
||||
|
||||
glm::dvec3 PathSegment::getPositionAt(double t) const {
|
||||
@@ -172,4 +188,28 @@ void PathSegment::initCurve() {
|
||||
}
|
||||
}
|
||||
|
||||
// The speed function, describing the shape of the speed curve with values in range [0,1]
|
||||
// OBS! The value must later be normalised so that the speed matches the duration of the segment.
|
||||
double PathSegment::speedFunction(double t) const {
|
||||
const double t1 = 0.25;
|
||||
const double t2 = 0.75; // > t1
|
||||
// TODO: more advanced computation of limits
|
||||
|
||||
double speed = 1.0;
|
||||
double tScaled;
|
||||
|
||||
// accelerate
|
||||
if (t < t1) {
|
||||
tScaled = t / t1;
|
||||
speed = ghoul::cubicEaseInOut(tScaled);
|
||||
}
|
||||
// deaccelerate
|
||||
else if (t > t2) {
|
||||
tScaled = (t - t2) / (1.0 - t2);
|
||||
speed = 1.0 - ghoul::cubicEaseInOut(tScaled);
|
||||
}
|
||||
|
||||
return speed;
|
||||
}
|
||||
|
||||
} // namespace openspace::autonavigation
|
||||
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
|
||||
const std::vector<glm::dvec3> getControlPoints() const; // TODO: remove this debugging function
|
||||
|
||||
const double speedAt(double t) const;
|
||||
const double speedAtTime(double time) const;
|
||||
|
||||
glm::dvec3 getPositionAt(double t) const;
|
||||
glm::dquat getRotationAt(double t) const;
|
||||
@@ -68,6 +68,8 @@ private:
|
||||
const glm::dquat piecewiseSlerpRotation(double t) const;
|
||||
void initCurve();
|
||||
|
||||
double speedFunction(double t) const;
|
||||
|
||||
CameraState _start;
|
||||
CameraState _end;
|
||||
double _startTime;
|
||||
|
||||
Reference in New Issue
Block a user