feat(moving platform): move in live's fixed 1/15 s physics steps

The live captures show platform movement in whole 1/15 s steps in every
zone (departure drift past a waypoint is always a whole number of them,
exact to 0.1 ms on about 260 departures), whatever the server loop ran
at, while the waypoint waits count the loop's time. Platforms now move
their object by the whole steps that fall in a frame; timers keep the
frame's time. The world server's frame rate is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 20:46:13 -06:00
parent 15c61676d6
commit a39b410b56
3 changed files with 86 additions and 13 deletions

View File

@@ -4,6 +4,9 @@
*/
#include "MovingPlatformComponent.h"
#include <cmath>
#include "BitStream.h"
#include "GeneralUtils.h"
#include "dZoneManager.h"
@@ -911,7 +914,7 @@ void MovingPlatformComponent::PlatformSetAngularVelocity(PlatformSubComponent& p
// A platform on its path. The client's is moved by its physics, at the velocity the platform gives its object; the
// server moves it here by the same speed. False when a script told of an arrival put the platform on another path.
bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const float deltaTime) {
bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const float deltaTime, const float stepTime) {
if (platform.mPath == nullptr) return true;
if (!platform.mActive) {
// An open quick build's platform loses its velocities
@@ -922,8 +925,8 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
const bool travelling = (platform.mState & Travelling) != 0;
if (platform.mTimeBased && travelling) platform.mMoveTimeElapsed += deltaTime;
// The object's physics: it goes at its velocity, travelling or not
platform.mPosition = platform.mPosition + platform.mVelocity * deltaTime;
// The object's physics: it goes at its velocity, travelling or not, in whole physics steps
platform.mPosition = platform.mPosition + platform.mVelocity * stepTime;
if (platform.mTimeBased) {
// mCurrentSpeed is the seconds to the next waypoint
@@ -972,16 +975,22 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
// Every platform of the object runs, in the client's order: a mover or simple mover moves the object, a rotator turns it
void MovingPlatformComponent::Run(const float deltaTime) {
if (m_SubComponents.empty()) return;
// The physics steps that fall in this frame (several in a long frame, none in most short ones)
m_StepTimeLeft += deltaTime;
// (a frame of exactly one step must not lose it to rounding)
const float steps = std::floor(m_StepTimeLeft / PhysicsStep + 0.0001f);
const float stepTime = steps * PhysicsStep;
m_StepTimeLeft = std::max(0.0f, m_StepTimeLeft - stepTime);
UpdateActive();
const auto generation = m_SetupGeneration;
for (size_t i = 0; i < m_SubComponents.size(); i++) {
auto& platform = *m_SubComponents[i];
if (platform.GetType() == eMoverSubComponentType::rotater) RunRotator(static_cast<RotatorSubComponent&>(platform), deltaTime);
else if (!RunPlatform(platform, deltaTime)) return;
else if (!RunPlatform(platform, deltaTime, stepTime)) return;
// A script told of an arrival may have put the platforms on another path
if (generation != m_SetupGeneration) return;
}
if (HasRotatorOnPath()) TurnObject(deltaTime);
if (HasRotatorOnPath() && stepTime > 0.0f) TurnObject(stepTime);
}
// What live answered a client's request with: the state of the platform (a mover's, a simple mover's on its own path,

View File

@@ -402,8 +402,9 @@ private:
// Puts the object where the platform is, without a serialization of its physics
void MoveObject(const PlatformSubComponent& platform, bool atWaypoint = false);
void SetObjectTransform(const NiPoint3& position, const NiQuaternion& rotation);
// A platform (mover or simple mover) on its path
bool RunPlatform(PlatformSubComponent& platform, float deltaTime);
// A platform (mover or simple mover) on its path: its timers count the frame's time, its object moves by the
// physics steps run this frame
bool RunPlatform(PlatformSubComponent& platform, float deltaTime, float stepTime);
// Turns a travelling rotator's object by its angular velocity
void RunRotator(RotatorSubComponent& rotator, float deltaTime);
// Whether the platforms of a quick build run yet
@@ -425,7 +426,14 @@ private:
// next waypoint's rotation, or the rotator's, which puts its own back whenever it is turning
NiPoint3 m_AngularVelocity{};
// Frame time not yet run as a physics step (PhysicsStep)
float m_StepTimeLeft = 0.0f;
public:
// Live's world servers moved platforms in fixed physics steps of 1/15 s whatever their loop rate (departure drift
// past a waypoint is always a whole number of them), while the waits count the loop's time
static constexpr float PhysicsStep = 1.0f / 15.0f;
void Run(float deltaTime) override;
// The platform's state as a PlatformResync gives it (the answer to RequestPlatformResync); false without a platform

View File

@@ -186,8 +186,9 @@ TEST_F(MovingPlatformComponentTest, ResyncAnswersLikeLive) {
EXPECT_EQ(static_cast<uint32_t>(travelling.eState), 2u);
EXPECT_EQ(travelling.iIndex, 0);
EXPECT_EQ(travelling.iNextIndex, 1);
EXPECT_NEAR(travelling.fPercentBetweenPoints, 0.5f, 0.001f);
EXPECT_NEAR(travelling.ptUnexpectedLocation.y, info.pos.y - 1.25f, 0.001f);
// 8 physics steps of 1/15 s since it left
EXPECT_NEAR(travelling.fPercentBetweenPoints, 8.0f / 15.0f, 0.001f);
EXPECT_NEAR(travelling.ptUnexpectedLocation.y, info.pos.y - 2.5f * 8.0f / 15.0f, 0.001f);
info.lot = 2349;
info.pos = NiPoint3(10.0f, 20.0f, 30.0f);
@@ -239,8 +240,9 @@ TEST_F(MovingPlatformComponentTest, SimpleMoverTripTiming) {
platform->Run(0.5f);
EXPECT_EQ(mover->mState, ePlatformStateFlag::Travelling);
EXPECT_NEAR(counterweight.GetPosition().y, 18.75f, 0.001f);
EXPECT_NEAR(mover->mPercentBetweenPoints, 0.5f, 0.001f);
// 8 physics steps of 1/15 s since it left
EXPECT_NEAR(counterweight.GetPosition().y, 20.0f - 2.5f * 8.0f / 15.0f, 0.001f);
EXPECT_NEAR(mover->mPercentBetweenPoints, 8.0f / 15.0f, 0.001f);
platform->Run(0.6f);
EXPECT_EQ(mover->mState, ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | ePlatformStateFlag::ReachedFinalDestination);
EXPECT_EQ(mover->mCurrentWaypointIndex, 1u);
@@ -823,7 +825,7 @@ TEST_F(MovingPlatformComponentTest, MoverAndRotatorBothRun) {
platform->Run(0.11f);
EXPECT_EQ(mover->mState, 2u);
platform->Run(0.89f);
EXPECT_NEAR(entity.GetPosition().x, 4.45f, 0.001f);
EXPECT_NEAR(entity.GetPosition().x, 5.0f * 14.0f / 15.0f, 0.001f); // 14 physics steps since it left
EXPECT_NEAR(entity.GetRotation().w, 0.7071f, 0.001f);
EXPECT_NEAR(entity.GetRotation().y, -0.7071f, 0.001f);
@@ -904,6 +906,60 @@ TEST_F(MovingPlatformComponentTest, MoverKeepsItsVelocityThroughAWaitLikeLive) {
EXPECT_EQ(entity.GetPosition(), NiPoint3(30.0f, 0.0f, 30.0f));
}
// Live moved platforms in fixed physics steps of 1/15 s while its loop ran faster or slower: on a 60 a second loop
// the platform moves every fourth frame, a whole step at a time, and the waypoint wait counts the loop's frames, so it
// leaves a waypoint (after the 0.1 s wait) a whole number of steps past it: 2 here, as live's subs at speed 15 (2.0)
TEST_F(MovingPlatformComponentTest, MoverMovesInLivesPhysicsSteps) {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestStepPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.position = NiPoint3(0.0f, 0.0f, 0.0f);
first.speed = 15.0f;
PathWaypoint second = first;
second.position = NiPoint3(3.0f, 0.0f, 0.0f);
PathWaypoint third = first;
third.position = NiPoint3(3.0f, 0.0f, 30.0f);
path.pathWaypoints = { first, second, third };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
info.pos = NiPoint3(0.0f, 0.0f, 0.0f);
Entity entity(288300744895900130, info);
entity.AddComponent<SimplePhysicsComponent>(-1);
auto* const platform = entity.AddComponent<MovingPlatformComponent>(-1, "TestStepPath");
auto* const mover = platform->GetMoverSubComponent();
constexpr float frame = 1.0f / 60.0f;
int frames = 0;
while (mover->mState != 2u && frames < 60) { platform->Run(frame); frames++; }
ASSERT_EQ(mover->mState, 2u);
// Travelling: the object moves only on the frames a step falls in, by speed / 15
std::vector<float> xs;
for (int i = 0; i < 8; i++) {
platform->Run(frame);
xs.push_back(mover->mPosition.x);
}
int moves = 0;
for (size_t i = 1; i < xs.size(); i++) {
if (xs[i] == xs[i - 1]) continue;
moves++;
EXPECT_NEAR(xs[i] - xs[i - 1], 1.0f, 0.001f);
}
EXPECT_EQ(moves, 2);
// At waypoint 1 (3 units on): waiting, then off the next stretch's way from a whole number of steps past it
frames = 0;
while (!(mover->mState == 2u && mover->mCurrentWaypointIndex == 1u) && frames < 120) { platform->Run(frame); frames++; }
ASSERT_EQ(mover->mCurrentWaypointIndex, 1u);
ASSERT_EQ(mover->mState, 2u);
const float drift = mover->mPosition.x - 3.0f;
const float steps = drift / 1.0f;
EXPECT_NEAR(steps, std::round(steps), 0.001f);
EXPECT_GE(std::round(steps), 1.0f);
EXPECT_LE(std::round(steps), 3.0f);
}
// A simple mover without a quick build runs from when it is loaded, as a mover does: waiting at its waypoint (9) for the
// shortest wait, then off to its other end, where it has stopped (28). With startPathingOnLoad false it stays as it was
// made (12), and so does one its script stops when it starts up (live's NJ spinner script, L_NJ_SPINNER_SERVER).
@@ -1049,7 +1105,7 @@ TEST_F(MovingPlatformComponentTest, MoverTurnsItsObjectWhileItsRotatorIsStopped)
EXPECT_EQ(mover->mState, 2u);
EXPECT_NEAR(platform->GetAngularVelocity().y, 3.14159265f / 4.0f, 0.0001f);
platform->Run(0.89f);
EXPECT_NEAR(entity.GetPosition().x, 4.45f, 0.001f);
EXPECT_NEAR(entity.GetPosition().x, 5.0f * 14.0f / 15.0f, 0.001f); // 14 physics steps since it left
EXPECT_NEAR(entity.GetRotation().w, std::cos(3.14159265f / 8.0f), 0.001f);
EXPECT_NEAR(entity.GetRotation().y, std::sin(3.14159265f / 8.0f), 0.001f);