feat(physics): riders keep their place on moving platforms

The client sends the object it stands on in its position updates (local
space: the offset from that object's position and velocity, not turned
with it); live's servers wrote it back in the controllable physics
serialization and the other clients place the rider on their own view of
the object. DLU read the block and dropped it, and wrote 0 for it.

- ControllablePhysicsComponent keeps the block (SetLocalSpace) and writes
  it as the client does (velocity only over float epsilon).
- While it is set, the server position is the platform's position plus
  the offset, on each update and every frame between them, so proximity,
  phantom volumes and ghosting follow the platform.
- A move by anything else (a teleport) takes the rider off; a platform
  the server does not have keeps the client's world position.

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 b8e383d953
commit 15c61676d6
8 changed files with 273 additions and 1 deletions

View File

@@ -16,7 +16,11 @@ struct RemoteInputInfo {
bool m_IsModified = false;
};
// The object a character stands on (the client's local space): position and linear velocity as offsets from that
// object's position and velocity, not turned with it
struct LocalSpaceInfo {
bool operator==(const LocalSpaceInfo& other) const = default;
LWOOBJID objectId = LWOOBJID_EMPTY;
NiPoint3 position = NiPoint3Constant::ZERO;
NiPoint3 linearVelocity = NiPoint3Constant::ZERO;
@@ -29,6 +33,7 @@ struct PositionUpdate {
bool onRail = false;
NiPoint3 velocity = NiPoint3Constant::ZERO;
NiPoint3 angularVelocity = NiPoint3Constant::ZERO;
bool hasLocalSpaceInfo = false;
LocalSpaceInfo localSpaceInfo;
RemoteInputInfo remoteInputInfo;
};

View File

@@ -1711,6 +1711,12 @@ void Entity::ProcessPositionUpdate(PositionUpdate& update) {
update.angularVelocity = NiPoint3Constant::ZERO;
}
// Standing on something (the client's local space): the server's position is that object's position here plus
// the offset, the way the other clients place the rider; the block is kept and written back
if (update.hasLocalSpaceInfo) {
if (const auto world = ControllablePhysicsComponent::LocalSpaceToWorld(update.localSpaceInfo)) update.position = *world;
}
// Handle statistics
auto* characterComponent = GetComponent<CharacterComponent>();
if (characterComponent) {
@@ -1723,6 +1729,7 @@ void Entity::ProcessPositionUpdate(PositionUpdate& update) {
controllablePhysicsComponent->SetIsOnRail(update.onRail);
controllablePhysicsComponent->SetVelocity(update.velocity);
controllablePhysicsComponent->SetAngularVelocity(update.angularVelocity);
controllablePhysicsComponent->SetLocalSpace(update.hasLocalSpaceInfo, update.localSpaceInfo);
auto* ghostComponent = GetComponent<GhostComponent>();
if (ghostComponent) ghostComponent->SetGhostReferencePoint(update.position);

View File

@@ -20,6 +20,9 @@
#include "StringifiedEnum.h"
#include "Amf3.h"
#include "ObjectLoadState.h"
#include "GhostComponent.h"
#include <limits>
ControllablePhysicsComponent::ControllablePhysicsComponent(Entity* entity, const int32_t componentID) : PhysicsComponent(entity, componentID) {
RegisterMsg(&ControllablePhysicsComponent::OnGetObjectReportInfo);
@@ -85,8 +88,36 @@ ControllablePhysicsComponent::~ControllablePhysicsComponent() {
}
}
// A rider goes with the object it stands on between its client's position updates, so the server's position
// (proximity, phantom volumes, ghosting) stays on the platform. Not serialized for it: each client places the rider on
// its own view of the object from the local space block.
void ControllablePhysicsComponent::Run(float deltaTime) {
if (!m_HasLocalSpaceInfo || m_Static) return;
// Put somewhere else by the server since (a teleport): off the object until the client says otherwise
if (m_Position != m_LocalSpaceWorldPosition) {
SetLocalSpace(false, {});
return;
}
const auto world = LocalSpaceToWorld(m_LocalSpaceInfo);
if (!world || *world == m_Position) return;
const bool dirty = m_DirtyPosition;
SetPosition(*world);
m_DirtyPosition = dirty;
m_LocalSpaceWorldPosition = m_Position;
if (auto* const ghostComponent = m_Parent->GetComponent<GhostComponent>()) ghostComponent->SetGhostReferencePoint(*world);
}
void ControllablePhysicsComponent::SetLocalSpace(const bool hasLocalSpaceInfo, const LocalSpaceInfo& info) {
m_HasLocalSpaceInfo = hasLocalSpaceInfo;
m_LocalSpaceInfo = hasLocalSpaceInfo ? info : LocalSpaceInfo{};
m_LocalSpaceWorldPosition = m_Position;
}
std::optional<NiPoint3> ControllablePhysicsComponent::LocalSpaceToWorld(const LocalSpaceInfo& info) {
if (info.objectId == LWOOBJID_EMPTY || !Game::entityManager) return std::nullopt;
const auto* const object = Game::entityManager->GetEntity(info.objectId);
if (!object) return std::nullopt;
return object->GetPosition() + info.position;
}
void ControllablePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
@@ -176,7 +207,22 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bo
outBitStream.Write(m_AngularVelocity.z);
}
outBitStream.Write0(); // local_space_info, always zero for now.
// The local space block as the client writes it (velocity only when its square length is over float epsilon)
outBitStream.Write(m_HasLocalSpaceInfo);
if (m_HasLocalSpaceInfo) {
outBitStream.Write(m_LocalSpaceInfo.objectId);
outBitStream.Write(m_LocalSpaceInfo.position.x);
outBitStream.Write(m_LocalSpaceInfo.position.y);
outBitStream.Write(m_LocalSpaceInfo.position.z);
const auto& velocity = m_LocalSpaceInfo.linearVelocity;
const bool writeVelocity = velocity.SquaredLength() > std::numeric_limits<float>::epsilon();
outBitStream.Write(writeVelocity);
if (writeVelocity) {
outBitStream.Write(velocity.x);
outBitStream.Write(velocity.y);
outBitStream.Write(velocity.z);
}
}
if (!bIsInitialUpdate) {
m_DirtyPosition = false;

View File

@@ -9,6 +9,9 @@
#include "NiQuaternion.h"
#include "dpCollisionChecks.h"
#include "eBubbleType.h"
#include "PositionUpdate.h"
#include <optional>
struct ObjectLoadState;
@@ -310,6 +313,22 @@ public:
void ApplyStun(const GameMessages::SetStunned& stun);
bool GetStunnedFromUsingItems() const { return m_StunnedUseItemCount > 0; }
/**
* The object this entity stands on and where on it, as the client's position updates give it (the client's local
* space). While it is set the entity goes with that object: its world position is the object's position plus the
* offset. Written back in this component's serialization, so other clients place the rider on their own view of
* the object. Call it after the position is set: a later move by anything else (a teleport) takes the entity off.
*/
void SetLocalSpace(const bool hasLocalSpaceInfo, const LocalSpaceInfo& info);
bool GetHasLocalSpaceInfo() const noexcept { return m_HasLocalSpaceInfo; }
const LocalSpaceInfo& GetLocalSpaceInfo() const noexcept { return m_LocalSpaceInfo; }
/**
* Where a local space position is in the world: the object's position plus the offset (not turned with the
* object, as the client). Nothing when the object is not there.
*/
static std::optional<NiPoint3> LocalSpaceToWorld(const LocalSpaceInfo& info);
private:
// The save's physics state (LoadFromSaveData), then where the character comes into the world
void ReadSaveData(const tinyxml2::XMLDocument& doc);
@@ -327,6 +346,12 @@ private:
*/
NiPoint3 m_Velocity;
// The object the entity stands on and where on it (SetLocalSpace)
bool m_HasLocalSpaceInfo = false;
LocalSpaceInfo m_LocalSpaceInfo;
// Where the entity was when its local space was last set or it was last carried
NiPoint3 m_LocalSpaceWorldPosition;
/**
* The current angular velocity of the entity
*/

View File

@@ -1684,6 +1684,7 @@ json ToJson(const PositionUpdate& m) {
j["onRail"] = ToJson(m.onRail);
j["velocity"] = ToJson(m.velocity);
j["angularVelocity"] = ToJson(m.angularVelocity);
j["hasLocalSpaceInfo"] = ToJson(m.hasLocalSpaceInfo);
j["localSpaceInfo"] = ToJson(m.localSpaceInfo);
j["remoteInputInfo"] = ToJson(m.remoteInputInfo);
return j;

View File

@@ -1551,6 +1551,7 @@ namespace {
struct PositionUpdatePacket final : public WorldPackets::PositionUpdate {
void Handle() override {
auto positionUpdate = update;
positionUpdate.hasLocalSpaceInfo = hasLocalSpaceInfo;
User* user = UserManager::Instance()->GetUser(sysAddr);
if (!user) {

View File

@@ -1,6 +1,7 @@
set(DCOMPONENTS_TESTS
"DeletionRestrictionTests.cpp"
"MovingPlatformComponentTests.cpp"
"PlatformRiderTests.cpp"
"ScriptComponentTests.cpp"
"InventorySaveTests.cpp"
"EquipMessagesTests.cpp"

View File

@@ -0,0 +1,186 @@
// Characters standing on moving platforms: the client's local space block in its position updates (the object it
// stands on, and its position and velocity as offsets from that object's) is kept, written back in the controllable
// physics serialization as live did, and the server's position goes with the platform.
#include "GameDependencies.h"
#include <gtest/gtest.h>
#include "BitStream.h"
#include "CDClientDatabase.h"
#include "Character.h"
#include "CharacterComponent.h"
#include "ControllablePhysicsComponent.h"
#include "Entity.h"
#include "EntityManager.h"
#include "MovingPlatformComponent.h"
#include "PositionUpdate.h"
#include "SimplePhysicsComponent.h"
#include "Zone.h"
#include "dZoneManager.h"
class PlatformRiderTest : public GameDependenciesTest {
protected:
void SetUp() override {
SetUpDependencies();
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
CDClientDatabase::ExecuteDML("CREATE TABLE MovingPlatforms (id INTEGER, platformIsSimpleMover INTEGER, platformMoveX REAL, platformMoveY REAL, platformMoveZ REAL, platformMoveTime REAL, platformStartAtEnd INTEGER, description TEXT);");
}
void TearDown() override { TearDownDependencies(); }
// A mover at (100, 0, 0) going +x at 5 units a second after its 0.1 s wait at the first waypoint
Entity* MakePlatform() {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "RiderTestPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.position = NiPoint3(100.0f, 0.0f, 0.0f);
first.speed = 5.0f;
PathWaypoint second = first;
second.position = NiPoint3(150.0f, 0.0f, 0.0f);
path.pathWaypoints = { first, second };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
auto platformInfo = info;
platformInfo.pos = first.position;
auto* const platform = Game::entityManager->CreateEntity(platformInfo, nullptr, nullptr, false, 0x40040000000aa01LL);
platform->AddComponent<SimplePhysicsComponent>(-1);
platform->AddComponent<MovingPlatformComponent>(-1, "RiderTestPath");
return platform;
}
static SystemAddress ClientAddress() {
SystemAddress address;
address.binaryAddress = 0x7f000001;
address.port = 2003;
return address;
}
static PositionUpdate Update(const NiPoint3& world, const LWOOBJID platform, const NiPoint3& offset) {
PositionUpdate update;
update.position = world;
update.onGround = true;
update.hasLocalSpaceInfo = platform != LWOOBJID_EMPTY;
update.localSpaceInfo.objectId = platform;
update.localSpaceInfo.position = offset;
return update;
}
};
// The server's position of a rider is the platform's here plus the offset, and goes with the platform between updates
TEST_F(PlatformRiderTest, RiderGoesWithThePlatform) {
auto* const platform = MakePlatform();
auto playerInfo = info;
playerInfo.lot = 1;
Entity player(32, playerInfo);
Character character(1, nullptr);
player.AddComponent<CharacterComponent>(-1, &character, ClientAddress());
auto* const physics = player.AddComponent<ControllablePhysicsComponent>(-1);
ASSERT_TRUE(player.IsPlayer());
// The client's world position is a little behind its own view of the platform; the server places it on its own
auto update = Update(NiPoint3(99.0f, 2.0f, 1.0f), platform->GetObjectID(), NiPoint3(-1.0f, 2.0f, 1.0f));
player.ProcessPositionUpdate(update);
EXPECT_EQ(physics->GetPosition(), NiPoint3(99.0f, 2.0f, 1.0f));
EXPECT_TRUE(physics->GetHasLocalSpaceInfo());
auto* const mover = platform->GetComponent<MovingPlatformComponent>();
mover->Run(0.11f);
mover->Run(1.0f);
const auto platformAt = platform->GetPosition();
ASSERT_GT(platformAt.x, 103.0f);
physics->Run(0.0f);
EXPECT_EQ(physics->GetPosition(), platformAt + NiPoint3(-1.0f, 2.0f, 1.0f));
// Off the platform: the update's own world position
auto off = Update(NiPoint3(10.0f, 0.0f, 10.0f), LWOOBJID_EMPTY, NiPoint3Constant::ZERO);
player.ProcessPositionUpdate(off);
EXPECT_FALSE(physics->GetHasLocalSpaceInfo());
mover->Run(1.0f);
physics->Run(0.0f);
EXPECT_EQ(physics->GetPosition(), NiPoint3(10.0f, 0.0f, 10.0f));
}
// The platform is not on this server (not loaded, or gone): the client's world position is used
TEST_F(PlatformRiderTest, UnknownPlatformKeepsTheClientsPosition) {
auto playerInfo = info;
playerInfo.lot = 1;
Entity player(32, playerInfo);
Character character(1, nullptr);
player.AddComponent<CharacterComponent>(-1, &character, ClientAddress());
auto* const physics = player.AddComponent<ControllablePhysicsComponent>(-1);
ASSERT_TRUE(player.IsPlayer());
auto update = Update(NiPoint3(5.0f, 6.0f, 7.0f), 0x40040000000aa99LL, NiPoint3(1.0f, 1.0f, 1.0f));
player.ProcessPositionUpdate(update);
EXPECT_EQ(physics->GetPosition(), NiPoint3(5.0f, 6.0f, 7.0f));
physics->Run(0.0f);
EXPECT_EQ(physics->GetPosition(), NiPoint3(5.0f, 6.0f, 7.0f));
EXPECT_TRUE(physics->GetHasLocalSpaceInfo());
}
// Moved by the server (a teleport) while riding: it comes off the platform
TEST_F(PlatformRiderTest, TeleportTakesTheRiderOff) {
auto* const platform = MakePlatform();
Entity npc(0x40040000000aa02LL, info);
auto* const physics = npc.AddComponent<ControllablePhysicsComponent>(-1);
LocalSpaceInfo onPlatform;
onPlatform.objectId = platform->GetObjectID();
onPlatform.position = NiPoint3(0.0f, 1.0f, 0.0f);
physics->SetPosition(NiPoint3(100.0f, 1.0f, 0.0f));
physics->SetLocalSpace(true, onPlatform);
auto* const mover = platform->GetComponent<MovingPlatformComponent>();
mover->Run(0.11f);
mover->Run(1.0f);
physics->Run(0.0f);
EXPECT_EQ(physics->GetPosition(), platform->GetPosition() + NiPoint3(0.0f, 1.0f, 0.0f));
physics->SetPosition(NiPoint3(-50.0f, 0.0f, 0.0f));
physics->Run(0.0f);
EXPECT_FALSE(physics->GetHasLocalSpaceInfo());
mover->Run(1.0f);
physics->Run(0.0f);
EXPECT_EQ(physics->GetPosition(), NiPoint3(-50.0f, 0.0f, 0.0f));
}
// The block as the client writes it, after the angular velocity bit: 1, object id, offset, then the velocity bit and
// the velocity only when its square length is over float epsilon
TEST_F(PlatformRiderTest, LocalSpaceBlockSerializedAsTheClientWritesIt) {
Entity entity(0x40040000000aa03LL, info);
auto* const physics = entity.AddComponent<ControllablePhysicsComponent>(-1);
RakNet::BitStream none;
physics->Serialize(none, true);
LocalSpaceInfo block;
block.objectId = 0x40040000000aa01LL;
block.position = NiPoint3(-4.78f, 1.88f, -0.65f);
block.linearVelocity = NiPoint3(1e-5f, 0.0f, 0.0f); // square length under epsilon: not written
physics->SetLocalSpace(true, block);
RakNet::BitStream still;
physics->Serialize(still, true);
EXPECT_EQ(still.GetNumberOfBitsUsed(), none.GetNumberOfBitsUsed() + 64 + 3 * 32 + 1);
block.linearVelocity = NiPoint3(11.44f, 10.55f, -3.0f);
physics->SetLocalSpace(true, block);
RakNet::BitStream moving;
physics->Serialize(moving, true);
ASSERT_EQ(moving.GetNumberOfBitsUsed(), none.GetNumberOfBitsUsed() + 64 + 6 * 32 + 1);
// Read back from where the block starts (a construction ends with it, so the no-block one ends with its 0 bit)
moving.SetReadOffset(none.GetNumberOfBitsUsed() - 1);
bool has{};
LWOOBJID id{};
NiPoint3 offset, velocity;
bool hasVelocity{};
ASSERT_TRUE(moving.Read(has));
EXPECT_TRUE(has);
moving.Read(id);
moving.Read(offset.x); moving.Read(offset.y); moving.Read(offset.z);
moving.Read(hasVelocity);
moving.Read(velocity.x); moving.Read(velocity.y); moving.Read(velocity.z);
EXPECT_EQ(id, block.objectId);
EXPECT_EQ(offset, block.position);
EXPECT_TRUE(hasVelocity);
EXPECT_EQ(velocity, block.linearVelocity);
}