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

145 lines
6.2 KiB
C++

#include <apps/openmw/mwmechanics/movementramp.hpp>
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
namespace
{
using namespace MWMechanics;
constexpr float sTolerance = 1e-4f;
const MovementRampTimes sTimes{ 0.15f, 0.1f };
const osg::Vec2f sForward(0.f, 1.f);
const osg::Vec2f sBack(0.f, -1.f);
const osg::Vec2f sRight(1.f, 0.f);
osg::Vec2f simulate(osg::Vec2f current, const osg::Vec2f& target, float seconds, float dt,
const MovementRampTimes& times = sTimes)
{
const int steps = static_cast<int>(seconds / dt + 0.5f);
for (int i = 0; i < steps; ++i)
current = rampMovement(current, target, dt, times);
return current;
}
TEST(MWMechanicsMovementRampTest, rampsUpLinearlyToFullSpeed)
{
const osg::Vec2f firstFrame = rampMovement(osg::Vec2f(), sForward, 1.f / 60.f, sTimes);
EXPECT_NEAR(firstFrame.y(), (1.f / 60.f) / 0.15f, sTolerance);
EXPECT_NEAR(firstFrame.x(), 0.f, sTolerance);
EXPECT_NEAR(simulate(osg::Vec2f(), sForward, 0.075f, 1.f / 200.f).y(), 0.5f, 1e-3f);
EXPECT_NEAR(simulate(osg::Vec2f(), sForward, 0.15f, 1.f / 200.f).y(), 1.f, 1e-3f);
// Never past the requested speed.
EXPECT_NEAR(simulate(osg::Vec2f(), sForward, 1.f, 1.f / 60.f).y(), 1.f, sTolerance);
}
TEST(MWMechanicsMovementRampTest, rampsDownToStanding)
{
EXPECT_NEAR(rampMovement(sForward, osg::Vec2f(), 0.05f, sTimes).y(), 0.5f, sTolerance);
EXPECT_EQ(rampMovement(sForward, osg::Vec2f(), 0.1f, sTimes), osg::Vec2f());
EXPECT_EQ(rampMovement(sForward, osg::Vec2f(), 1.f, sTimes), osg::Vec2f());
// A diagonal slows down along its own direction.
const osg::Vec2f diagonal = osg::Vec2f(1.f, 1.f) / std::sqrt(2.f);
const osg::Vec2f half = rampMovement(diagonal, osg::Vec2f(), 0.05f, sTimes);
EXPECT_NEAR(half.length(), 0.5f, sTolerance);
EXPECT_NEAR(half.x(), half.y(), sTolerance);
}
TEST(MWMechanicsMovementRampTest, slowingToAPartialSpeedUsesTheDecelerationRate)
{
const osg::Vec2f walk = sForward * 0.4f;
EXPECT_NEAR(rampMovement(sForward, walk, 0.03f, sTimes).y(), 0.7f, sTolerance);
EXPECT_NEAR(rampMovement(sForward, walk, 1.f, sTimes).y(), 0.4f, sTolerance);
}
TEST(MWMechanicsMovementRampTest, reversalSlowsThroughZeroThenSpeedsUp)
{
// 0.1 s to stop, then the remaining 0.03 s accelerates backwards.
const osg::Vec2f reversed = rampMovement(sForward, sBack, 0.13f, sTimes);
EXPECT_NEAR(reversed.y(), -0.03f / 0.15f, sTolerance);
EXPECT_NEAR(reversed.x(), 0.f, sTolerance);
// The same split into frames gives the same answer.
EXPECT_NEAR(simulate(sForward, sBack, 0.13f, 0.13f / 26.f).y(), -0.03f / 0.15f, 1e-3f);
// Halfway through stopping it is still moving forward.
EXPECT_NEAR(rampMovement(sForward, sBack, 0.05f, sTimes).y(), 0.5f, sTolerance);
EXPECT_NEAR(simulate(sForward, sBack, 0.25f, 1.f / 120.f).y(), -1.f, 1e-3f);
}
TEST(MWMechanicsMovementRampTest, turningNinetyDegreesBlendsBothDirections)
{
const osg::Vec2f mid = rampMovement(sForward, sRight, 0.05f, sTimes);
// Forward dies away at the deceleration rate while sideways builds at the acceleration rate.
EXPECT_NEAR(mid.y(), 0.5f, sTolerance);
EXPECT_NEAR(mid.x(), 0.05f / 0.15f, sTolerance);
const osg::Vec2f done = simulate(sForward, sRight, 0.2f, 1.f / 60.f);
EXPECT_NEAR(done.x(), 1.f, 1e-3f);
EXPECT_NEAR(done.y(), 0.f, 1e-3f);
}
TEST(MWMechanicsMovementRampTest, neverFasterThanFullSpeed)
{
const osg::Vec2f diagonal = osg::Vec2f(1.f, 1.f) / std::sqrt(2.f);
for (float t = 0.f; t < 0.3f; t += 0.01f)
EXPECT_LE(simulate(sForward, diagonal, t, 1.f / 100.f).length(), 1.f + sTolerance);
}
TEST(MWMechanicsMovementRampTest, standingStillStaysStill)
{
EXPECT_EQ(rampMovement(osg::Vec2f(), osg::Vec2f(), 1.f / 60.f, sTimes), osg::Vec2f());
}
TEST(MWMechanicsMovementRampTest, zeroOrNegativeFrameTimeChangesNothing)
{
const osg::Vec2f current(0.2f, 0.3f);
EXPECT_EQ(rampMovement(current, sForward, 0.f, sTimes), current);
EXPECT_EQ(rampMovement(current, sForward, -1.f, sTimes), current);
}
TEST(MWMechanicsMovementRampTest, zeroTimesAreInstant)
{
const MovementRampTimes instant{ 0.f, 0.f };
EXPECT_EQ(rampMovement(osg::Vec2f(), sForward, 1.f / 60.f, instant), sForward);
EXPECT_EQ(rampMovement(sForward, osg::Vec2f(), 1.f / 60.f, instant), osg::Vec2f());
const osg::Vec2f reversed = rampMovement(sForward, sBack, 1.f / 60.f, instant);
EXPECT_NEAR(reversed.y(), -1.f, sTolerance);
EXPECT_NEAR(reversed.x(), 0.f, sTolerance);
}
TEST(MWMechanicsMovementRampTest, infiniteTimesFreezeMovement)
{
const float inf = std::numeric_limits<float>::infinity();
const MovementRampTimes frozen{ inf, inf };
const osg::Vec2f current(0.f, 0.6f);
EXPECT_EQ(rampMovement(current, sBack, 0.5f, frozen), current);
EXPECT_EQ(rampMovement(current, osg::Vec2f(), 0.5f, frozen), current);
}
class MWMechanicsMovementRampFrameRateTest : public ::testing::TestWithParam<float>
{
};
TEST_P(MWMechanicsMovementRampFrameRateTest, resultDoesNotDependOnFrameRate)
{
const float dt = GetParam();
// Samples at whole multiples of 1/30 s, so every tested rate lands on them exactly.
const float frame = 1.f / 30.f;
EXPECT_NEAR(simulate(osg::Vec2f(), sForward, 3 * frame, dt).y(), 0.1f / 0.15f, 2e-3f);
EXPECT_NEAR(simulate(osg::Vec2f(), sForward, 6 * frame, dt).y(), 1.f, 2e-3f);
EXPECT_NEAR(simulate(sForward, osg::Vec2f(), frame, dt).length(), 1.f - frame / 0.1f, 2e-3f);
EXPECT_NEAR(simulate(sForward, sBack, 6 * frame, dt).y(), -0.1f / 0.15f, 2e-3f);
const osg::Vec2f turned = simulate(sForward, sRight, frame, dt);
EXPECT_NEAR(turned.y(), 1.f - frame / 0.1f, 2e-3f);
EXPECT_NEAR(turned.x(), frame / 0.15f, 2e-3f);
}
INSTANTIATE_TEST_SUITE_P(FrameRates, MWMechanicsMovementRampFrameRateTest,
::testing::Values(1.f / 30.f, 1.f / 60.f, 1.f / 120.f, 1.f / 240.f));
}