mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-12-21 11:59:37 -06:00
Compare commits
3 Commits
rotation-b
...
test-fix-f
| Author | SHA1 | Date | |
|---|---|---|---|
| 0760c76288 | |||
| 9ff8134de8 | |||
| 721a85932a |
@@ -153,6 +153,10 @@ void CDClientManager::LoadValuesFromDatabase() {
|
||||
|
||||
void CDClientManager::LoadValuesFromDefaults() {
|
||||
LOG("Loading default CDClient tables!");
|
||||
|
||||
// Only call table default loaders that actually exist. Tests don't need
|
||||
// the full CDClient database; add additional table default loaders here
|
||||
// if/when those tables implement LoadValuesFromDefaults().
|
||||
CDPetComponentTable::Instance().LoadValuesFromDefaults();
|
||||
CDComponentsRegistryTable::Instance().LoadValuesFromDefaults();
|
||||
CDZoneTableTable::LoadValuesFromDefaults();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,13 @@ void CDComponentsRegistryTable::LoadValuesFromDatabase() {
|
||||
tableData.finalize();
|
||||
}
|
||||
|
||||
void CDComponentsRegistryTable::LoadValuesFromDefaults() {
|
||||
// Provide minimal mappings for tests: no components for default template IDs.
|
||||
auto& entries = GetEntriesMutable();
|
||||
// Ensure a default empty mapping for template id 0 (used in some tests)
|
||||
entries.insert_or_assign(0, 0);
|
||||
}
|
||||
|
||||
int32_t CDComponentsRegistryTable::GetByIDAndType(uint32_t id, eReplicaComponentType componentType, int32_t defaultValue) {
|
||||
auto& entries = GetEntriesMutable();
|
||||
auto exists = entries.find(id);
|
||||
|
||||
@@ -16,5 +16,6 @@ struct CDComponentsRegistry {
|
||||
class CDComponentsRegistryTable : public CDTable<CDComponentsRegistryTable, std::unordered_map<uint64_t, uint32_t>> {
|
||||
public:
|
||||
void LoadValuesFromDatabase();
|
||||
void LoadValuesFromDefaults();
|
||||
int32_t GetByIDAndType(uint32_t id, eReplicaComponentType componentType, int32_t defaultValue = 0);
|
||||
};
|
||||
|
||||
@@ -50,4 +50,19 @@ namespace CDZoneTableTable {
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void LoadValuesFromDefaults() {
|
||||
// Provide a minimal default zone entry so zone-dependent startup paths don't crash during tests.
|
||||
CDZoneTable defaultZone{};
|
||||
defaultZone.zoneID = 1;
|
||||
defaultZone.zoneName = "testzone";
|
||||
defaultZone.zoneControlTemplate = 2365;
|
||||
defaultZone.ghostdistance_min = 100.0f;
|
||||
defaultZone.ghostdistance = 100.0f;
|
||||
defaultZone.PlayerLoseCoinsOnDeath = false;
|
||||
defaultZone.disableSaveLoc = false;
|
||||
defaultZone.mountsAllowed = false;
|
||||
defaultZone.petsAllowed = false;
|
||||
entries[defaultZone.zoneID] = defaultZone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ struct CDZoneTable {
|
||||
namespace CDZoneTableTable {
|
||||
using Table = std::map<uint32_t, CDZoneTable>;
|
||||
void LoadValuesFromDatabase();
|
||||
void LoadValuesFromDefaults();
|
||||
|
||||
// Queries the table with a zoneID to find.
|
||||
const CDZoneTable* Query(uint32_t zoneID);
|
||||
|
||||
@@ -160,7 +160,6 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
|
||||
auto valueStr = nextAction.GetValueParameterString();
|
||||
auto numberAsInt = static_cast<int32_t>(number);
|
||||
auto nextActionType = GetNextAction().GetType();
|
||||
LOG("~number: %f, nextActionType: %s", static_cast<float>(number), nextActionType.data());
|
||||
|
||||
// TODO replace with switch case and nextActionType with enum
|
||||
/* BEGIN Move */
|
||||
@@ -209,6 +208,10 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
|
||||
m_IsRotating = true;
|
||||
m_InActionTranslation.y = isSpinNegative ? -number : number;
|
||||
m_PreviousFrameRotation = entity.GetRotation();
|
||||
// compute the absolute rotation target quaternion
|
||||
NiPoint3 deltaEuler = NiPoint3(0.0f, Math::DegToRad(m_InActionTranslation.y), 0.0f);
|
||||
m_RotationTarget = m_PreviousFrameRotation;
|
||||
m_RotationTarget *= NiQuaternion::FromEulerAngles(deltaEuler);
|
||||
// d/vi = t
|
||||
// radians/velocity = time
|
||||
// only care about the time, direction is irrelevant here
|
||||
@@ -223,6 +226,9 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
|
||||
m_IsRotating = true;
|
||||
m_InActionTranslation.x = isRotateLeft ? -number : number;
|
||||
m_PreviousFrameRotation = entity.GetRotation();
|
||||
NiPoint3 deltaEuler = NiPoint3(Math::DegToRad(m_InActionTranslation.x), 0.0f, 0.0f);
|
||||
m_RotationTarget = m_PreviousFrameRotation;
|
||||
m_RotationTarget *= NiQuaternion::FromEulerAngles(deltaEuler);
|
||||
}
|
||||
} else if (nextActionType == "Roll" || nextActionType == "RollNegative") {
|
||||
const float radians = Math::DegToRad(number);
|
||||
@@ -234,6 +240,9 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
|
||||
m_IsRotating = true;
|
||||
m_InActionTranslation.z = isRotateDown ? -number : number;
|
||||
m_PreviousFrameRotation = entity.GetRotation();
|
||||
NiPoint3 deltaEuler = NiPoint3(0.0f, 0.0f, Math::DegToRad(m_InActionTranslation.z));
|
||||
m_RotationTarget = m_PreviousFrameRotation;
|
||||
m_RotationTarget *= NiQuaternion::FromEulerAngles(deltaEuler);
|
||||
}
|
||||
}
|
||||
/* END Rotate */
|
||||
@@ -374,60 +383,49 @@ bool Strip::CheckRotation(float deltaTime, ModelComponent& modelComponent) {
|
||||
getAngVel.target = modelComponent.GetParent()->GetObjectID();
|
||||
getAngVel.Send();
|
||||
const auto curRotation = modelComponent.GetParent()->GetRotation();
|
||||
const auto diff = m_PreviousFrameRotation.Diff(curRotation).GetEulerAngles();
|
||||
LOG("Diff: x=%f, y=%f, z=%f", std::abs(Math::RadToDeg(diff.x)), std::abs(Math::RadToDeg(diff.y)), std::abs(Math::RadToDeg(diff.z)));
|
||||
LOG("Velocity: x=%f, y=%f, z=%f", Math::RadToDeg(getAngVel.angVelocity.x) * deltaTime, Math::RadToDeg(getAngVel.angVelocity.y) * deltaTime, Math::RadToDeg(getAngVel.angVelocity.z) * deltaTime);
|
||||
// Compute the actual frame delta rotation using quaternions instead of
|
||||
// extracting Euler angles (which is non-unique and can be incorrect when
|
||||
// multiple axes rotate simultaneously).
|
||||
NiQuaternion frameDelta = m_PreviousFrameRotation.Diff(curRotation);
|
||||
float fw_frame = frameDelta.w;
|
||||
if (fw_frame > 1.0f) fw_frame = 1.0f;
|
||||
if (fw_frame < -1.0f) fw_frame = -1.0f;
|
||||
// angle (radians) = 2 * acos(w)
|
||||
float angleFrameRad = 2.0f * acos(fw_frame);
|
||||
float angleFrameDeg = Math::RadToDeg(angleFrameRad);
|
||||
m_PreviousFrameRotation = curRotation;
|
||||
auto angVel = diff;
|
||||
angVel.x = std::abs(Math::RadToDeg(angVel.x));
|
||||
angVel.y = std::abs(Math::RadToDeg(angVel.y));
|
||||
angVel.z = std::abs(Math::RadToDeg(angVel.z));
|
||||
const auto [rotateX, rotateY, rotateZ] = m_InActionTranslation;
|
||||
bool rotateFinished = true;
|
||||
NiPoint3 finalRotationAdjustment = NiPoint3Constant::ZERO;
|
||||
if (rotateX != 0.0f) {
|
||||
m_InActionTranslation.x -= angVel.x;
|
||||
rotateFinished = std::signbit(m_InActionTranslation.x) != std::signbit(rotateX);
|
||||
finalRotationAdjustment.x = Math::DegToRad(m_InActionTranslation.x);
|
||||
} else if (rotateY != 0.0f) {
|
||||
m_InActionTranslation.y -= angVel.y;
|
||||
rotateFinished = std::signbit(m_InActionTranslation.y) != std::signbit(rotateY);
|
||||
finalRotationAdjustment.y = Math::DegToRad(m_InActionTranslation.y);
|
||||
} else if (rotateZ != 0.0f) {
|
||||
m_InActionTranslation.z -= angVel.z;
|
||||
rotateFinished = std::signbit(m_InActionTranslation.z) != std::signbit(rotateZ);
|
||||
finalRotationAdjustment.z = Math::DegToRad(m_InActionTranslation.z);
|
||||
}
|
||||
|
||||
if (rotateFinished && m_InActionTranslation != NiPoint3Constant::ZERO) {
|
||||
LOG("Rotation finished, zeroing angVel");
|
||||
|
||||
angVel.x = Math::DegToRad(angVel.x);
|
||||
angVel.y = Math::DegToRad(angVel.y);
|
||||
angVel.z = Math::DegToRad(angVel.z);
|
||||
|
||||
if (rotateX != 0.0f) getAngVel.angVelocity.x = 0.0f;
|
||||
else if (rotateY != 0.0f) getAngVel.angVelocity.y = 0.0f;
|
||||
else if (rotateZ != 0.0f) getAngVel.angVelocity.z = 0.0f;
|
||||
// Use quaternion remaining angle to decide completion. Compute the quaternion
|
||||
// that rotates from the current rotation to the target rotation. If the
|
||||
// rotation angle of that quaternion is below an epsilon, we're finished.
|
||||
NiQuaternion remaining = modelComponent.GetParent()->GetRotation().Diff(m_RotationTarget);
|
||||
float w = remaining.w;
|
||||
if (w > 1.0f) w = 1.0f; // clamp
|
||||
if (w < -1.0f) w = -1.0f;
|
||||
// angle (radians) = 2 * acos(w)
|
||||
float angleRemainingRad = 2.0f * acos(w);
|
||||
float angleRemainingDeg = Math::RadToDeg(angleRemainingRad);
|
||||
constexpr float EPS_DEG = 0.2f; // finish when less than 0.2 degree remains (numeric residual tolerance)
|
||||
|
||||
if (angleRemainingDeg <= EPS_DEG) {
|
||||
LOG("Rotation finished by quaternion remaining angle (%f deg)", angleRemainingDeg);
|
||||
// Zero angular velocity on axes that were part of this action (safe to zero all)
|
||||
getAngVel.angVelocity = NiPoint3Constant::ZERO;
|
||||
GameMessages::SetAngularVelocity setAngVel{};
|
||||
setAngVel.target = modelComponent.GetParent()->GetObjectID();
|
||||
setAngVel.angVelocity = getAngVel.angVelocity;
|
||||
setAngVel.Send();
|
||||
|
||||
// Do the final adjustment so we will have rotated exactly the requested units
|
||||
auto currentRot = modelComponent.GetParent()->GetRotation();
|
||||
NiQuaternion finalAdjustment = NiQuaternion::FromEulerAngles(finalRotationAdjustment);
|
||||
currentRot *= finalAdjustment;
|
||||
currentRot.Normalize();
|
||||
modelComponent.GetParent()->SetRotation(currentRot);
|
||||
|
||||
// Snap to exact target to avoid tiny residual error
|
||||
modelComponent.GetParent()->SetRotation(m_RotationTarget);
|
||||
m_InActionTranslation = NiPoint3Constant::ZERO;
|
||||
m_IsRotating = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
LOG("angVel: x=%f, y=%f, z=%f", m_InActionTranslation.x, m_InActionTranslation.y, m_InActionTranslation.z);
|
||||
return rotateFinished;
|
||||
// minimal logging retained elsewhere; per-frame verbose logs removed
|
||||
// Not finished yet
|
||||
return false;
|
||||
}
|
||||
|
||||
void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
|
||||
|
||||
@@ -78,7 +78,15 @@ private:
|
||||
|
||||
NiQuaternion m_PreviousFrameRotation{};
|
||||
|
||||
// The absolute target rotation for the current rotation action
|
||||
NiQuaternion m_RotationTarget{};
|
||||
|
||||
NiPoint3 m_SavedVelocity{};
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
// Test-only accessors
|
||||
friend struct StripTestAccessor;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif //!__STRIP__H__
|
||||
|
||||
@@ -8,6 +8,9 @@ list(APPEND DGAMETEST_SOURCES ${DCOMPONENTS_TESTS})
|
||||
add_subdirectory(dGameMessagesTests)
|
||||
list(APPEND DGAMETEST_SOURCES ${DGAMEMESSAGES_TESTS})
|
||||
|
||||
add_subdirectory(dPropertyBehaviorsTests)
|
||||
list(APPEND DGAMETEST_SOURCES ${DPROPERTYBEHAVIORS_TESTS})
|
||||
|
||||
file(COPY ${GAMEMESSAGE_TESTBITSTREAMS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
file(COPY ${COMPONENT_TEST_DATA} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
7
tests/dGameTests/dPropertyBehaviorsTests/CMakeLists.txt
Normal file
7
tests/dGameTests/dPropertyBehaviorsTests/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
set(DPROPERTYBEHAVIORS_TESTS
|
||||
"dPropertyBehaviorsTests/StripRotationTest.cpp"
|
||||
"dPropertyBehaviorsTests/StripRotationIntegrationTest.cpp"
|
||||
)
|
||||
|
||||
# Expose variable to parent CMake
|
||||
set(DPROPERTYBEHAVIORS_TESTS ${DPROPERTYBEHAVIORS_TESTS} PARENT_SCOPE)
|
||||
@@ -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();
|
||||
}
|
||||
189
tests/dGameTests/dPropertyBehaviorsTests/StripRotationTest.cpp
Normal file
189
tests/dGameTests/dPropertyBehaviorsTests/StripRotationTest.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
#include "GameDependencies.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "NiQuaternion.h"
|
||||
#include "dMath.h"
|
||||
|
||||
// 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 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)));
|
||||
|
||||
// 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);
|
||||
|
||||
// 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 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);
|
||||
}
|
||||
Reference in New Issue
Block a user