Files
DarkflameServer/tests/dGameTests/dComponentsTests/PlatformRiderTests.cpp
Aaron Kimbrell 15c61676d6 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>
2026-11-27 20:55:23 -06:00

187 lines
7.6 KiB
C++

// 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);
}