Files
claudeandClaude Opus 5.5 f3bb0753c7
Ashlar checks / tooling (push) Successful in 6s
Add movement acceleration and deceleration
New [Game] movement acceleration setting, on by default, with
movement acceleration time (0.15 s), movement deceleration time (0.1 s)
and movement air control (0.3). Movement ramps towards the requested
movement in the actor's own frame, so turning is never delayed.
Reversing slows down through zero before speeding up the other way,
and sideways movement left over from a direction change dies away at
the deceleration rate. NPCs and creatures stop at once so they never
overshoot a path point. With the setting off, behavior is unchanged.

The ramp is in mwmechanics/movementramp.cpp with unit tests, including
frame-rate independence from 1/30 to 1/240 s. A new headless
SCENARIO=movement logs the player's speed as it starts, stops and
reverses.

Refs #26

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 03:35:19 -05:00

125 lines
3.9 KiB
C++

#include "movementramp.hpp"
#include <algorithm>
#include <limits>
#include <components/settings/values.hpp>
namespace MWMechanics
{
namespace
{
constexpr float sEpsilon = 1e-5f;
float rate(float time)
{
return time > 0.f ? 1.f / time : std::numeric_limits<float>::infinity();
}
// Moves a non-negative length towards zero.
float shrink(float length, float rate, float dt)
{
if (rate == std::numeric_limits<float>::infinity())
return 0.f;
return std::max(0.f, length - rate * dt);
}
}
osg::Vec2f rampMovement(
const osg::Vec2f& current, const osg::Vec2f& target, float dt, const MovementRampTimes& times)
{
if (dt <= 0.f)
return current;
const float up = rate(times.mAccelerationTime);
const float down = rate(times.mDecelerationTime);
const float targetLength = target.length();
if (targetLength < sEpsilon)
{
const float length = current.length();
if (length < sEpsilon)
return osg::Vec2f();
return current * (shrink(length, down, dt) / length);
}
const osg::Vec2f dir = target / targetLength;
float along = current * dir;
osg::Vec2f across = current - dir * along;
// Sideways movement left over from the old direction dies away.
const float acrossLength = across.length();
if (acrossLength < sEpsilon)
across = osg::Vec2f();
else
across *= shrink(acrossLength, down, dt) / acrossLength;
float remaining = dt;
if (along < 0.f)
{
// Moving away from the target direction: slow down to zero first, then use the rest of the time.
if (down == std::numeric_limits<float>::infinity())
along = 0.f;
else
{
const float needed = -along / down;
if (needed >= remaining)
{
along += down * remaining;
remaining = 0.f;
}
else
{
along = 0.f;
remaining -= needed;
}
}
}
if (remaining > 0.f)
{
if (along < targetLength)
along = up == std::numeric_limits<float>::infinity() ? targetLength
: std::min(targetLength, along + up * remaining);
else if (along > targetLength)
along = down == std::numeric_limits<float>::infinity()
? targetLength
: std::max(targetLength, along - down * remaining);
}
osg::Vec2f result = dir * along + across;
const float length = result.length();
if (length > 1.f)
result /= length;
return result;
}
MovementRampTimes getMovementRampTimes(bool inAir)
{
MovementRampTimes times;
times.mAccelerationTime = Settings::game().mMovementAccelerationTime;
times.mDecelerationTime = Settings::game().mMovementDecelerationTime;
if (inAir)
{
const float airControl = Settings::game().mMovementAirControl;
if (airControl <= 0.f)
{
times.mAccelerationTime = std::numeric_limits<float>::infinity();
times.mDecelerationTime = std::numeric_limits<float>::infinity();
}
else
{
// A time of 0 stays instant on the ground and in the air.
times.mAccelerationTime /= airControl;
times.mDecelerationTime /= airControl;
}
}
return times;
}
bool usesMovementAcceleration()
{
return Settings::game().mMovementAcceleration;
}
}