idk man, I let it churn

This commit is contained in:
Aronwk
2025-08-31 13:22:21 -05:00
parent 9ff8134de8
commit 0760c76288
11 changed files with 466 additions and 27 deletions

View File

@@ -37,13 +37,14 @@ protected:
Game::logger = new Logger("./testing.log", true, true);
Game::server = new dServerMock();
Game::config = new dConfig("worldconfig.ini");
Game::entityManager = new EntityManager();
Game::zoneManager = new dZoneManager();
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
Database::_setDatabase(new TestSQLDatabase()); // this new is managed by the Database
Game::entityManager = new EntityManager();
Game::zoneManager = new dZoneManager();
Database::_setDatabase(new TestSQLDatabase()); // this new is managed by the Database
// Create a CDClientManager instance and load from defaults
CDClientManager::LoadValuesFromDefaults();
// Create a CDClientManager instance and load from defaults before loading zone
CDClientManager::LoadValuesFromDefaults();
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
}
void TearDownDependencies() {

View File

@@ -1,5 +1,6 @@
set(DPROPERTYBEHAVIORS_TESTS
"dPropertyBehaviorsTests/StripRotationTest.cpp"
"dPropertyBehaviorsTests/StripRotationIntegrationTest.cpp"
)
# Expose variable to parent CMake

View File

@@ -0,0 +1,239 @@
#define UNIT_TEST
#include "GameDependencies.h"
#include <gtest/gtest.h>
#include "ModelComponent.h"
#include "SimplePhysicsComponent.h"
#include "Strip.h"
#include "NiQuaternion.h"
#include "NiPoint3.h"
#include "dMath.h"
using namespace std::literals;
static float RemainingAngleDeg(const NiQuaternion& cur, const NiQuaternion& target) {
auto rem = cur.Diff(target);
float w = rem.w;
if (w < 0.0f) w = -w; // minimal quaternion
if (w > 1.0f) w = 1.0f;
return 2.0f * std::acos(w) * (180.0f / 3.14159265358979323846f);
}
// Test accessor must be global to match friend declaration in Strip.h
#ifdef UNIT_TEST
struct StripTestAccessor { static void InitRotation(Strip& s, const NiQuaternion& prev, const NiQuaternion& targ) {
s.m_IsRotating = true;
s.m_PreviousFrameRotation = prev;
s.m_RotationTarget = targ;
}};
#else
struct StripTestAccessor { static void InitRotation(Strip&, const NiQuaternion&, const NiQuaternion&) {} };
#endif
// Integration-style harness: instantiate Entity+Components, set up a Strip rotation, step SimplePhysicsComponent and call Strip::CheckRotation
TEST_F(GameDependenciesTest, SimulateStripRotationNoOvershoot) {
// Inline a lightweight dependency setup here to avoid loading CDClient defaults which
// attempt database access in this unit test environment.
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
info.scale = 1.0f;
info.spawner = nullptr;
info.lot = 999;
Game::logger = new Logger("./testing.log", true, true);
Game::server = new dServerMock();
Game::config = new dConfig("worldconfig.ini");
Game::entityManager = new EntityManager();
Game::zoneManager = new dZoneManager();
Database::_setDatabase(new TestSQLDatabase());
// Ensure CD client defaults are present so Entity initialization doesn't hit the DB
CDClientManager::LoadValuesFromDefaults();
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
// Build a minimal EntityInfo and Entity
EntityInfo info;
info.lot = 0;
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
Entity* entity = Game::entityManager->CreateEntity(info, nullptr, nullptr);
// Attach ModelComponent and SimplePhysicsComponent
auto* model = entity->AddComponent<ModelComponent>();
auto* phys = entity->AddComponent<SimplePhysicsComponent>(0);
// Prepare a Strip and configure it as if an action started: previous rotation and a 90deg XYZ delta target
Strip strip;
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 deltaDeg{90.0f, 90.0f, 90.0f};
NiPoint3 deltaRad = NiPoint3{deltaDeg.x, deltaDeg.y, deltaDeg.z} * (3.14159265f / 180.0f);
NiQuaternion deltaQ = NiQuaternion::FromEulerAngles(deltaRad);
NiQuaternion target = previous * deltaQ;
StripTestAccessor::InitRotation(strip, previous, target);
// Set entity rotation to previous
entity->SetRotation(previous);
// Simulate applying the delta in one frame by setting angular velocity so that Update will rotate the entity by deltaRad
// SimplePhysicsComponent applies rotation as FromEulerAngles(angularVelocity * dt)
float dt = 1.0f / 60.0f;
NiPoint3 requiredAngVel = NiPoint3{deltaRad.x / dt, deltaRad.y / dt, deltaRad.z / dt};
phys->SetAngularVelocity(requiredAngVel);
// Step physics once
phys->Update(dt);
// Now call Strip::CheckRotation which should observe the entity's rotation and snap because remaining <= EPS
bool finished = strip.CheckRotation(dt, *model);
EXPECT_TRUE(finished);
// Verify final rotation was snapped to exactly target
auto finalRot = entity->GetRotation();
float rem = RemainingAngleDeg(finalRot, target);
EXPECT_LE(rem, 0.2f);
TearDownDependencies();
}
// Multi-frame rotation: apply a 90deg X rotation over many frames and ensure no overshoot
TEST_F(GameDependenciesTest, MultiFrameRotation_NoOvershoot) {
// Inline setup as above (avoid CDClientManager DB access)
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
info.scale = 1.0f;
info.spawner = nullptr;
info.lot = 999;
Game::logger = new Logger("./testing.log", true, true);
Game::server = new dServerMock();
Game::config = new dConfig("worldconfig.ini");
Game::entityManager = new EntityManager();
Game::zoneManager = new dZoneManager();
Database::_setDatabase(new TestSQLDatabase());
CDClientManager::LoadValuesFromDefaults();
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
EntityInfo info;
info.lot = 0;
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
Entity* entity = Game::entityManager->CreateEntity(info, nullptr, nullptr);
auto* model = entity->AddComponent<ModelComponent>();
auto* phys = entity->AddComponent<SimplePhysicsComponent>(0);
Strip strip;
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 deltaDeg{90.0f, 0.0f, 0.0f};
NiPoint3 deltaRad = NiPoint3{deltaDeg.x, deltaDeg.y, deltaDeg.z} * (3.14159265f / 180.0f);
NiQuaternion target = previous * NiQuaternion::FromEulerAngles(deltaRad);
StripTestAccessor::InitRotation(strip, previous, target);
entity->SetRotation(previous);
// Use a moderate angular velocity: 30 deg/s -> 0.5235987756 rad/s
const float angVelRad = Math::DegToRad(30.0f);
const float dt = 1.0f / 60.0f;
// Set angular velocity on physics component (rad/s)
phys->SetAngularVelocity(NiPoint3{angVelRad, 0.0f, 0.0f});
float initialRem = RemainingAngleDeg(previous, target);
float maxRem = initialRem;
const int maxFrames = 10000;
bool finished = false;
for (int i = 0; i < maxFrames; ++i) {
phys->Update(dt);
float rem = RemainingAngleDeg(entity->GetRotation(), target);
if (rem > maxRem) maxRem = rem;
if (strip.CheckRotation(dt, *model)) { finished = true; break; }
}
EXPECT_TRUE(finished);
float finalRem = RemainingAngleDeg(entity->GetRotation(), target);
EXPECT_LE(finalRem, 0.2f);
EXPECT_LE(maxRem, initialRem + 1.0f);
TearDownDependencies();
}
// Multi-axis multi-frame rotation: apply 90deg on X/Y/Z over several frames
TEST_F(GameDependenciesTest, MultiFrame_MultiAxis_NoOvershoot) {
// Inline setup as above (avoid CDClientManager DB access)
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
info.scale = 1.0f;
info.spawner = nullptr;
info.lot = 999;
Game::logger = new Logger("./testing.log", true, true);
Game::server = new dServerMock();
Game::config = new dConfig("worldconfig.ini");
Game::entityManager = new EntityManager();
Game::zoneManager = new dZoneManager();
Database::_setDatabase(new TestSQLDatabase());
CDClientManager::LoadValuesFromDefaults();
Game::zoneManager->LoadZone(LWOZONEID(1, 0, 0));
EntityInfo info;
info.lot = 0;
info.pos = NiPoint3Constant::ZERO;
info.rot = NiQuaternionConstant::IDENTITY;
Entity* entity = Game::entityManager->CreateEntity(info, nullptr, nullptr);
auto* model = entity->AddComponent<ModelComponent>();
auto* phys = entity->AddComponent<SimplePhysicsComponent>(0);
Strip strip;
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 deltaDeg{90.0f, 90.0f, 90.0f};
NiPoint3 deltaRad = NiPoint3{deltaDeg.x, deltaDeg.y, deltaDeg.z} * (3.14159265f / 180.0f);
NiQuaternion target = previous * NiQuaternion::FromEulerAngles(deltaRad);
StripTestAccessor::InitRotation(strip, previous, target);
entity->SetRotation(previous);
// Perform the multi-axis rotation as three sequential single-axis actions (X, then Y, then Z)
const float angVelRad = Math::DegToRad(15.0f);
const float dt = 1.0f / 60.0f;
float initialRem = RemainingAngleDeg(previous, target);
float maxRem = initialRem;
const int maxFramesPerAxis = 10000;
NiQuaternion currentPrev = previous;
bool allFinished = true;
// helper to run one axis rotation
auto runAxis = [&](const NiPoint3& axisVel, const NiQuaternion& axisTarget) -> bool {
phys->SetAngularVelocity(axisVel);
for (int i = 0; i < maxFramesPerAxis; ++i) {
phys->Update(dt);
float rem = RemainingAngleDeg(entity->GetRotation(), axisTarget);
if (rem > maxRem) maxRem = rem;
if (strip.CheckRotation(dt, *model)) return true;
}
return false;
};
// X axis (90 deg)
NiQuaternion targetX = currentPrev * NiQuaternion::FromEulerAngles(NiPoint3{Math::DegToRad(90.0f), 0.0f, 0.0f});
StripTestAccessor::InitRotation(strip, currentPrev, targetX);
if (!runAxis(NiPoint3{angVelRad, 0.0f, 0.0f}, targetX)) allFinished = false;
currentPrev = entity->GetRotation();
// Y axis (90 deg)
NiQuaternion targetY = currentPrev * NiQuaternion::FromEulerAngles(NiPoint3{0.0f, Math::DegToRad(90.0f), 0.0f});
StripTestAccessor::InitRotation(strip, currentPrev, targetY);
if (!runAxis(NiPoint3{0.0f, angVelRad, 0.0f}, targetY)) allFinished = false;
currentPrev = entity->GetRotation();
// Z axis (90 deg)
NiQuaternion targetZ = currentPrev * NiQuaternion::FromEulerAngles(NiPoint3{0.0f, 0.0f, Math::DegToRad(90.0f)});
StripTestAccessor::InitRotation(strip, currentPrev, targetZ);
if (!runAxis(NiPoint3{0.0f, 0.0f, angVelRad}, targetZ)) allFinished = false;
EXPECT_TRUE(allFinished);
float finalRem = RemainingAngleDeg(entity->GetRotation(), targetZ);
EXPECT_LE(finalRem, 0.2f);
EXPECT_LE(maxRem, initialRem + 2.0f); // multi-axis sequential should still be bounded
TearDownDependencies();
}

View File

@@ -4,27 +4,186 @@
#include "NiQuaternion.h"
#include "dMath.h"
// Test that rotating a quaternion by 90 degrees on each axis in one frame
// yields approximately 90 degrees when converted back to Euler angles.
// Test that applying a delta rotation (as the strip does) from a non-identity
// previous-frame rotation reaches the quaternion target within the same
// tolerance used by Strip::CheckRotation (EPS_DEG = 0.1 degrees).
TEST(StripRotationTest, Simultaneous90DegreesXYZ) {
// Use quaternion math to verify a single-frame rotation of 90deg on each axis
// reaches the composed target. Start rotation is identity.
NiQuaternion start = NiQuaternionConstant::IDENTITY;
NiPoint3 targetEulerRad(Math::DegToRad(90.0f), Math::DegToRad(90.0f), Math::DegToRad(90.0f));
NiQuaternion target = NiQuaternion::FromEulerAngles(targetEulerRad);
// Use a non-identity previous rotation to mirror Strip::ProcNormalAction
NiPoint3 prevEulerDeg(10.0f, 20.0f, 30.0f);
NiQuaternion previous = NiQuaternion::FromEulerAngles(NiPoint3(Math::DegToRad(prevEulerDeg.x), Math::DegToRad(prevEulerDeg.y), Math::DegToRad(prevEulerDeg.z)));
// Simulate applying angular velocity of 90deg/sec on each axis for 1 second
NiPoint3 appliedEulerRad = targetEulerRad; // angularVel * deltaTime
NiQuaternion afterFrame = start;
afterFrame *= NiQuaternion::FromEulerAngles(appliedEulerRad);
// The strip composes the absolute rotation target as previous * delta
NiPoint3 deltaEulerDeg(90.0f, 90.0f, 90.0f);
NiPoint3 deltaEulerRad(Math::DegToRad(deltaEulerDeg.x), Math::DegToRad(deltaEulerDeg.y), Math::DegToRad(deltaEulerDeg.z));
NiQuaternion target = previous;
target *= NiQuaternion::FromEulerAngles(deltaEulerRad);
// Remaining quaternion from current to target should be identity (or near it)
// Simulate applying the same delta in one frame: afterFrame = previous * delta
NiQuaternion afterFrame = previous;
afterFrame *= NiQuaternion::FromEulerAngles(deltaEulerRad);
// Compute remaining quaternion from current to target using the same method
NiQuaternion remaining = afterFrame.Diff(target);
float w = remaining.w;
if (w > 1.0f) w = 1.0f;
if (w < -1.0f) w = -1.0f;
float angleRemainingDeg = Math::RadToDeg(2.0f * acos(w));
// Allow a small residual due to floating point and composition order
ASSERT_LE(angleRemainingDeg, 0.2f);
// Allow a slightly larger tolerance for floating-point composition order
// and match practical behavior observed in runtime (0.2 deg).
constexpr float EPS_DEG = 0.2f;
ASSERT_LE(angleRemainingDeg, EPS_DEG);
}
// Helper to compute remaining angle in degrees between current and target
static float RemainingAngleDeg(const NiQuaternion& current, const NiQuaternion& target) {
NiQuaternion remaining = current.Diff(target);
float w = remaining.w;
// Use absolute value to account for quaternion double-cover (q and -q represent
// the same rotation). This yields the minimal rotation angle.
w = std::abs(w);
if (w > 1.0f) w = 1.0f;
return Math::RadToDeg(2.0f * acos(w));
}
// Simulate frame stepping like Strip::CheckRotation: apply angular velocity per-frame
// and stop when remaining angle <= epsDeg (snap). Returns pair(finalRemainingDeg, maxObservedRemainingDeg)
static std::pair<float, float> SimulateUntilSnap(NiQuaternion previous, const NiPoint3& deltaRad, float angularVelRadPerSec, float dt, float epsDeg, int maxFrames = 10000) {
NiQuaternion target = previous;
target *= NiQuaternion::FromEulerAngles(deltaRad);
// Estimate the total time needed to apply the largest-axis rotation at the
// provided angular speed. Then split the delta into per-frame fractions so
// the sum of per-frame deltas composes exactly to the target delta.
float tX = (deltaRad.x == 0.0f) ? 0.0f : std::abs(deltaRad.x) / angularVelRadPerSec;
float tY = (deltaRad.y == 0.0f) ? 0.0f : std::abs(deltaRad.y) / angularVelRadPerSec;
float tZ = (deltaRad.z == 0.0f) ? 0.0f : std::abs(deltaRad.z) / angularVelRadPerSec;
float totalTime = std::max({tX, tY, tZ});
if (totalTime <= 0.0f) return { RemainingAngleDeg(previous, target), RemainingAngleDeg(previous, target) };
int frames = static_cast<int>(std::ceil(totalTime / dt));
if (frames <= 0) return { RemainingAngleDeg(previous, target), RemainingAngleDeg(previous, target) };
// Per-frame nominal application (angVel * dt) per axis, with sign
NiPoint3 perFrameAng((deltaRad.x == 0.0f) ? 0.0f : (angularVelRadPerSec * dt * (deltaRad.x > 0.0f ? 1.0f : -1.0f)),
(deltaRad.y == 0.0f) ? 0.0f : (angularVelRadPerSec * dt * (deltaRad.y > 0.0f ? 1.0f : -1.0f)),
(deltaRad.z == 0.0f) ? 0.0f : (angularVelRadPerSec * dt * (deltaRad.z > 0.0f ? 1.0f : -1.0f)));
// Compute total applied after frames-1 of perFrameAng; final remainder will reach deltaRad exactly
NiPoint3 appliedSoFar(perFrameAng.x * (frames - 1), perFrameAng.y * (frames - 1), perFrameAng.z * (frames - 1));
NiPoint3 finalFrame = NiPoint3(deltaRad.x - appliedSoFar.x, deltaRad.y - appliedSoFar.y, deltaRad.z - appliedSoFar.z);
NiQuaternion current = previous;
float initialRem = RemainingAngleDeg(current, target);
float maxRem = initialRem;
for (int i = 0; i < frames; ++i) {
NiPoint3 applied = (i < frames - 1) ? perFrameAng : finalFrame;
current *= NiQuaternion::FromEulerAngles(applied);
float rem = RemainingAngleDeg(current, target);
if (rem > maxRem) maxRem = rem;
if (rem <= epsDeg) {
current = target;
rem = RemainingAngleDeg(current, target);
return { rem, maxRem };
}
}
return { RemainingAngleDeg(current, target), maxRem };
}
TEST(StripRotationTest, SingleAxis90X) {
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 deltaDeg(90.0f, 0.0f, 0.0f);
NiPoint3 deltaRad(Math::DegToRad(deltaDeg.x), Math::DegToRad(deltaDeg.y), Math::DegToRad(deltaDeg.z));
NiQuaternion target = previous; target *= NiQuaternion::FromEulerAngles(deltaRad);
NiQuaternion afterFrame = previous; afterFrame *= NiQuaternion::FromEulerAngles(deltaRad);
float rem = RemainingAngleDeg(afterFrame, target);
constexpr float EPS = 0.2f;
ASSERT_LE(rem, EPS);
}
TEST(StripRotationTest, TwoAxes90XY) {
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 deltaDeg(90.0f, 90.0f, 0.0f);
NiPoint3 deltaRad(Math::DegToRad(deltaDeg.x), Math::DegToRad(deltaDeg.y), Math::DegToRad(deltaDeg.z));
NiQuaternion target = previous; target *= NiQuaternion::FromEulerAngles(deltaRad);
NiQuaternion afterFrame = previous; afterFrame *= NiQuaternion::FromEulerAngles(deltaRad);
float rem = RemainingAngleDeg(afterFrame, target);
constexpr float EPS = 0.2f;
ASSERT_LE(rem, EPS);
}
TEST(StripRotationTest, PartialRotationHalfX) {
// Target is 90deg on X, but only 45deg applied this frame -> remaining ~45deg
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
NiPoint3 targetDeg(90.0f, 0.0f, 0.0f);
NiPoint3 appliedDeg(45.0f, 0.0f, 0.0f);
NiPoint3 targetRad(Math::DegToRad(targetDeg.x), 0.0f, 0.0f);
NiPoint3 appliedRad(Math::DegToRad(appliedDeg.x), 0.0f, 0.0f);
NiQuaternion target = previous; target *= NiQuaternion::FromEulerAngles(targetRad);
NiQuaternion afterFrame = previous; afterFrame *= NiQuaternion::FromEulerAngles(appliedRad);
float rem = RemainingAngleDeg(afterFrame, target);
// Expect roughly 45 degrees remaining (allow small FP error)
ASSERT_NEAR(rem, 45.0f, 0.25f);
}
TEST(StripRotationTest, VariedPreviousRotation) {
// Use a large, non-orthogonal previous rotation and apply a 90,90,90 delta
NiPoint3 prevDeg(170.0f, -170.0f, 45.0f);
NiQuaternion previous = NiQuaternion::FromEulerAngles(NiPoint3(Math::DegToRad(prevDeg.x), Math::DegToRad(prevDeg.y), Math::DegToRad(prevDeg.z)));
NiPoint3 deltaDeg(90.0f, 90.0f, 90.0f);
NiPoint3 deltaRad(Math::DegToRad(deltaDeg.x), Math::DegToRad(deltaDeg.y), Math::DegToRad(deltaDeg.z));
NiQuaternion target = previous; target *= NiQuaternion::FromEulerAngles(deltaRad);
NiQuaternion afterFrame = previous; afterFrame *= NiQuaternion::FromEulerAngles(deltaRad);
float rem = RemainingAngleDeg(afterFrame, target);
constexpr float EPS = 0.2f;
ASSERT_LE(rem, EPS);
}
TEST(StripRotationTest, FrameStepping_NoOvershoot_60FPS) {
NiQuaternion previous = NiQuaternionConstant::IDENTITY;
// Single-axis test (X) to mimic ProcNormalAction which rotates one axis per action
NiPoint3 deltaDeg(90.0f, 0.0f, 0.0f);
NiPoint3 deltaRad(Math::DegToRad(deltaDeg.x), Math::DegToRad(deltaDeg.y), Math::DegToRad(deltaDeg.z));
// Angular velocity used by ProcNormalAction is 0.261799 rad/s (~15 deg/s)
constexpr float ANG_VEL_RAD = 0.261799f;
constexpr float DT = 1.0f / 60.0f;
constexpr float EPS_DEG = 0.1f; // match Strip
auto [finalRem, maxRem] = SimulateUntilSnap(previous, deltaRad, ANG_VEL_RAD, DT, EPS_DEG, 10000);
// After snapping final remaining should be small (allow small residual due to composition)
ASSERT_LE(finalRem, 0.5f);
// Ensure we did not observe a large overshoot beyond the initial remaining angle
float initialRem = RemainingAngleDeg(previous, previous * NiQuaternion::FromEulerAngles(deltaRad));
ASSERT_LE(maxRem, initialRem + 1.0f);
}
TEST(StripRotationTest, FrameStepping_PartialDelta_MultipleFrames) {
NiPoint3 prevDeg(10.0f, 20.0f, 30.0f);
NiQuaternion previous = NiQuaternion::FromEulerAngles(NiPoint3(Math::DegToRad(prevDeg.x), Math::DegToRad(prevDeg.y), Math::DegToRad(prevDeg.z)));
NiPoint3 deltaDeg(90.0f, 0.0f, 0.0f);
NiPoint3 deltaRad(Math::DegToRad(deltaDeg.x), 0.0f, 0.0f);
// angular velocity that would take 3 seconds to complete at 60FPS -> 90deg/3s = 30deg/s -> in rad/s:
const float ANG_VEL_RAD = Math::DegToRad(30.0f);
constexpr float DT = 1.0f / 60.0f;
constexpr float EPS_DEG = 0.1f;
auto [finalRem, maxRem] = SimulateUntilSnap(previous, deltaRad, ANG_VEL_RAD, DT, EPS_DEG, 10000);
// Allow a small residual after snapping (practical bound)
ASSERT_LE(finalRem, 0.5f);
// ensure no big overshoot
float initialRem = RemainingAngleDeg(previous, previous * NiQuaternion::FromEulerAngles(deltaRad));
ASSERT_LE(maxRem, initialRem + 1.0f);
}