Compare commits

...

4 Commits

Author SHA1 Message Date
0760c76288 idk man, I let it churn 2025-08-31 13:22:21 -05:00
9ff8134de8 test fix 2 2025-08-31 03:47:58 -05:00
721a85932a test fix 2025-08-31 03:36:21 -05:00
David Markowitz
4d043398ab add rotation behavior logic (not working correctly for multiple axes) 2025-08-31 00:13:56 -07:00
21 changed files with 824 additions and 42 deletions

View File

@@ -14,13 +14,14 @@ Vector3 NiQuaternion::GetEulerAngles() const {
angles.x = std::atan2(sinr_cosp, cosr_cosp);
// pitch (y-axis rotation)
const float sinp = 2 * (w * y - z * x);
const float t2 = 2 * (w * y - z * x);
angles.y = std::asin(std::clamp(t2, -1.0f, 1.0f)); // clamp to avoid NaN
if (std::abs(sinp) >= 1) {
angles.y = std::copysign(3.14 / 2, sinp); // use 90 degrees if out of range
} else {
angles.y = std::asin(sinp);
}
// if (std::abs(p) >= 1) {
// angles.y = std::copysign(3.14 / 2, p); // use 90 degrees if out of range
// } else {
// angles.y = std::asin(p);
// }
// yaw (z-axis rotation)
const float siny_cosp = 2 * (w * z + x * y);
@@ -30,6 +31,65 @@ Vector3 NiQuaternion::GetEulerAngles() const {
return angles;
}
NiQuaternion NiQuaternion::operator*(const float scalar) const noexcept {
return NiQuaternion(this->w * scalar, this->x * scalar, this->y * scalar, this->z * scalar);
}
NiQuaternion& NiQuaternion::operator*=(const NiQuaternion& q) {
auto& [ow, ox, oy, oz] = q;
auto [cw, cx, cy, cz] = *this; // Current rotation copied because otherwise it screws up the math
this->w = cw * ow - cx * ox - cy * oy - cz * oz;
this->x = cw * ox + cx * ow + cy * oz - cz * oy;
this->y = cw * oy + cy * ow + cz * ox - cx * oz;
this->z = cw * oz + cz * ow + cx * oy - cy * ox;
return *this;
}
NiQuaternion NiQuaternion::operator* (const NiQuaternion& q) const {
auto& [ow, ox, oy, oz] = q;
return NiQuaternion
(
/* w */w * ow - x * ox - y * oy - z * oz,
/* x */w * ox + x * ow + y * oz - z * oy,
/* y */w * oy + y * ow + z * ox - x * oz,
/* z */w * oz + z * ow + x * oy - y * ox
);
}
NiQuaternion NiQuaternion::operator/(const float& q) const noexcept {
return NiQuaternion(this->w / q, this->x / q, this->y / q, this->z / q);
}
void NiQuaternion::Normalize() {
float length = Dot(*this);
float invLength = 1.0f / std::sqrt(length);
*this = *this * invLength;
}
float NiQuaternion::Dot(const NiQuaternion& q) const noexcept {
return (this->w * q.w) + (this->x * q.x) + (this->y * q.y) + (this->z * q.z);
}
void NiQuaternion::Inverse() noexcept {
NiQuaternion copy = *this;
copy.Conjugate();
const float inv = 1.0f / Dot(*this);
*this = copy / inv;
}
void NiQuaternion::Conjugate() noexcept {
x = -x;
y = -y;
z = -z;
}
NiQuaternion NiQuaternion::Diff(const NiQuaternion& q) const noexcept {
NiQuaternion inv = *this;
inv.Inverse();
return inv * q;
}
// MARK: Helper Functions
//! Look from a specific point in space to another point in space (Y-locked)

View File

@@ -110,6 +110,18 @@ public:
[[nodiscard]] Vector3 GetEulerAngles() const;
NiQuaternion operator*(const float scalar) const noexcept;
NiQuaternion operator*(const NiQuaternion& q) const noexcept;
NiQuaternion operator/(const float& q) const noexcept;
NiQuaternion& operator*=(const NiQuaternion& q) noexcept;
float Dot(const NiQuaternion& q) const noexcept;
void Inverse() noexcept;
void Conjugate() noexcept;
NiQuaternion Diff(const NiQuaternion& q) const noexcept;
void Normalize();
// MARK: Operators
//! Operator to check for equality

View File

@@ -59,7 +59,7 @@ constexpr LWOINSTANCEID LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID
constexpr LWOMAPID LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID
constexpr uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID
constexpr float PI = 3.14159f;
constexpr float PI = 3.14159265358979323846264338327950288f;
//============ STRUCTS ==============

23
dCommon/dMath.h Normal file
View File

@@ -0,0 +1,23 @@
// Darkflame Universe
// Copyright 2025
#ifndef DMATH_H
#define DMATH_H
#include <cmath>
namespace Math {
constexpr float PI = 3.14159265358979323846264338327950288f;
constexpr float RATIO_DEG_TO_RAD = PI / 180.0f;
constexpr float RATIO_RAD_TO_DEG = 180.0f / PI;
inline float DegToRad(float degrees) {
return degrees * RATIO_DEG_TO_RAD;
}
inline float RadToDeg(float radians) {
return radians * RATIO_RAD_TO_DEG;
}
};
#endif //!DMATH_H

View File

@@ -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();
}

View File

@@ -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);

View File

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

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -18,6 +18,7 @@ ModelComponent::ModelComponent(Entity* parent) : Component(parent) {
using namespace GameMessages;
m_OriginalPosition = m_Parent->GetDefaultPosition();
m_OriginalRotation = m_Parent->GetDefaultRotation();
LOG("%f %f %f %f", m_OriginalRotation.x, m_OriginalRotation.y, m_OriginalRotation.z, m_OriginalRotation.w);
m_IsPaused = false;
m_NumListeningInteract = 0;
@@ -37,6 +38,10 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::GameMsg& msg) {
m_Parent->SetPosition(m_OriginalPosition);
m_Parent->SetRotation(m_OriginalRotation);
m_Parent->SetVelocity(NiPoint3Constant::ZERO);
GameMessages::SetAngularVelocity setAngVel;
setAngVel.target = m_Parent->GetObjectID();
setAngVel.angVelocity = NiPoint3Constant::ZERO;
setAngVel.Send();
m_Speed = 3.0f;
m_NumListeningInteract = 0;
@@ -303,6 +308,38 @@ void ModelComponent::SetVelocity(const NiPoint3& velocity) const {
m_Parent->SetVelocity(velocity);
}
bool ModelComponent::TrySetAngularVelocity(const NiPoint3& angularVelocity) const {
GameMessages::GetAngularVelocity getAngVel{};
getAngVel.target = m_Parent->GetObjectID();
if (!getAngVel.Send()) {
LOG("Couldn't get angular velocity for %llu", m_Parent->GetObjectID());
return false;
}
GameMessages::SetAngularVelocity setAngVel{};
setAngVel.target = m_Parent->GetObjectID();
if (angularVelocity != NiPoint3Constant::ZERO) {
setAngVel.angVelocity = getAngVel.angVelocity;
const auto [x, y, z] = angularVelocity * m_Speed;
if (x != 0.0f) {
if (getAngVel.angVelocity.x != 0.0f) return false;
setAngVel.angVelocity.x = x;
} else if (y != 0.0f) {
if (getAngVel.angVelocity.y != 0.0f) return false;
setAngVel.angVelocity.y = y;
} else if (z != 0.0f) {
if (getAngVel.angVelocity.z != 0.0f) return false;
setAngVel.angVelocity.z = z;
}
} else {
setAngVel.angVelocity = angularVelocity;
}
LOG("Setting angular velocity to %f %f %f", setAngVel.angVelocity.x, setAngVel.angVelocity.y, setAngVel.angVelocity.z);
setAngVel.Send();
return true;
}
void ModelComponent::OnChatMessageReceived(const std::string& sMessage) {
for (auto& behavior : m_Behaviors) behavior.OnChatMessageReceived(sMessage);
}

View File

@@ -144,6 +144,11 @@ public:
// Force sets the velocity to a value.
void SetVelocity(const NiPoint3& velocity) const;
// Attempts to set the angular velocity of the model.
// If the axis currently has a velocity of zero, returns true.
// If the axis is currently controlled by a behavior, returns false.
bool TrySetAngularVelocity(const NiPoint3& angularVelocity) const;
void OnChatMessageReceived(const std::string& sMessage);
void OnHit();
@@ -161,6 +166,8 @@ public:
// Decrements the number of strips listening for an attack.
// If this is the last strip removing an attack, it will reset the factions to the default of -1.
void RemoveAttack();
float GetSpeed() const noexcept { return m_Speed; }
private:
// Loads a behavior from the database.

View File

@@ -16,7 +16,10 @@
#include "Amf3.h"
SimplePhysicsComponent::SimplePhysicsComponent(Entity* parent, int32_t componentID) : PhysicsComponent(parent, componentID) {
RegisterMsg(MessageType::Game::GET_OBJECT_REPORT_INFO, this, &SimplePhysicsComponent::OnGetObjectReportInfo);
using namespace GameMessages;
RegisterMsg<GetObjectReportInfo>(this, &SimplePhysicsComponent::OnGetObjectReportInfo);
RegisterMsg<GameMessages::GetAngularVelocity>(this, &SimplePhysicsComponent::OnGetAngularVelocity);
RegisterMsg<GameMessages::SetAngularVelocity>(this, &SimplePhysicsComponent::OnSetAngularVelocity);
m_Position = m_Parent->GetDefaultPosition();
m_Rotation = m_Parent->GetDefaultRotation();
@@ -38,10 +41,20 @@ SimplePhysicsComponent::~SimplePhysicsComponent() {
}
void SimplePhysicsComponent::Update(const float deltaTime) {
if (m_Velocity == NiPoint3Constant::ZERO) return;
m_Position += m_Velocity * deltaTime;
m_DirtyPosition = true;
Game::entityManager->SerializeEntity(m_Parent);
if (m_Velocity != NiPoint3Constant::ZERO) {
m_Position += m_Velocity * deltaTime;
m_DirtyPosition = true;
Game::entityManager->SerializeEntity(m_Parent);
}
if (m_AngularVelocity != NiPoint3Constant::ZERO) {
m_Rotation.Normalize();
const auto vel = NiQuaternion::FromEulerAngles(m_AngularVelocity * deltaTime);
m_Rotation *= vel;
const auto euler = m_Rotation.GetEulerAngles();
m_DirtyPosition = true;
Game::entityManager->SerializeEntity(m_Parent);
}
}
void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
@@ -52,8 +65,12 @@ void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
outBitStream.Write(m_DirtyVelocity || bIsInitialUpdate);
if (m_DirtyVelocity || bIsInitialUpdate) {
outBitStream.Write(m_Velocity);
outBitStream.Write(m_AngularVelocity);
outBitStream.Write(m_Velocity.x);
outBitStream.Write(m_Velocity.y);
outBitStream.Write(m_Velocity.z);
outBitStream.Write(m_AngularVelocity.x);
outBitStream.Write(m_AngularVelocity.y);
outBitStream.Write(m_AngularVelocity.z);
m_DirtyVelocity = false;
}
@@ -92,3 +109,18 @@ bool SimplePhysicsComponent::OnGetObjectReportInfo(GameMessages::GameMsg& msg) {
info.PushDebug<AMFStringValue>("Climbable Type") = StringifiedEnum::ToString(m_ClimbableType).data();
return true;
}
bool SimplePhysicsComponent::OnSetAngularVelocity(GameMessages::GameMsg& msg) {
auto& setAngVel = static_cast<GameMessages::SetAngularVelocity&>(msg);
m_DirtyVelocity |= setAngVel.bForceFlagDirty || (m_AngularVelocity != setAngVel.angVelocity);
m_AngularVelocity = setAngVel.angVelocity;
LOG("Velocity is now %f %f %f", m_AngularVelocity.x, m_AngularVelocity.y, m_AngularVelocity.z);
Game::entityManager->SerializeEntity(m_Parent);
return true;
}
bool SimplePhysicsComponent::OnGetAngularVelocity(GameMessages::GameMsg& msg) {
auto& getAngVel = static_cast<GameMessages::GetAngularVelocity&>(msg);
getAngVel.angVelocity = m_AngularVelocity;
return true;
}

View File

@@ -61,6 +61,9 @@ public:
*/
void SetAngularVelocity(const NiPoint3& value) { m_AngularVelocity = value; m_DirtyVelocity = true; }
bool OnSetAngularVelocity(GameMessages::GameMsg& msg);
bool OnGetAngularVelocity(GameMessages::GameMsg& msg);
/**
* Returns the physics motion state
* @return the physics motion state

View File

@@ -871,5 +871,21 @@ namespace GameMessages {
bool bIgnoreChecks{ false };
};
struct GetAngularVelocity : public GameMsg {
GetAngularVelocity() : GameMsg(MessageType::Game::GET_ANGULAR_VELOCITY) {}
NiPoint3 angVelocity{};
};
struct SetAngularVelocity : public GameMsg {
SetAngularVelocity() : GameMsg(MessageType::Game::SET_ANGULAR_VELOCITY) {}
NiPoint3 angVelocity{};
bool bIgnoreDirtyFlags{};
bool bForceFlagDirty{};
};
};
#endif // GAMEMESSAGES_H

View File

@@ -9,6 +9,7 @@
#include "PropertyManagementComponent.h"
#include "PlayerManager.h"
#include "SimplePhysicsComponent.h"
#include "dMath.h"
#include "dChatFilter.h"
@@ -104,7 +105,7 @@ void Strip::HandleMsg(GameMessages::ResetModelToDefaults& msg) {
m_WaitingForAction = false;
m_PausedTime = 0.0f;
m_NextActionIndex = 0;
m_InActionMove = NiPoint3Constant::ZERO;
m_InActionTranslation = NiPoint3Constant::ZERO;
m_PreviousFramePosition = NiPoint3Constant::ZERO;
}
@@ -163,36 +164,89 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
// TODO replace with switch case and nextActionType with enum
/* BEGIN Move */
if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") {
m_IsRotating = false;
// X axis
bool isMoveLeft = nextActionType == "MoveLeft";
int negative = isMoveLeft ? -1 : 1;
// Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_X * negative)) {
m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.x = isMoveLeft ? -number : number;
m_InActionTranslation.x = isMoveLeft ? -number : number;
}
} else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") {
m_IsRotating = false;
// Y axis
bool isFlyDown = nextActionType == "FlyDown";
int negative = isFlyDown ? -1 : 1;
// Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Y * negative)) {
m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.y = isFlyDown ? -number : number;
m_InActionTranslation.y = isFlyDown ? -number : number;
}
} else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") {
m_IsRotating = false;
// Z axis
bool isMoveBackward = nextActionType == "MoveBackward";
int negative = isMoveBackward ? -1 : 1;
// Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Z * negative)) {
m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.z = isMoveBackward ? -number : number;
m_InActionTranslation.z = isMoveBackward ? -number : number;
}
}
/* END Move */
/* BEGIN Rotate */
else if (nextActionType == "Spin" || nextActionType == "SpinNegative") {
const float radians = Math::DegToRad(number);
bool isSpinNegative = nextActionType == "SpinNegative";
float negative = isSpinNegative ? -0.261799f : 0.261799f;
// Default angular velocity is 3 units per second.
if (modelComponent.TrySetAngularVelocity(NiPoint3Constant::UNIT_Y * negative)) {
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
}
} else if (nextActionType == "Tilt" || nextActionType == "TiltNegative") {
const float radians = Math::DegToRad(number);
bool isRotateLeft = nextActionType == "TiltNegative";
float negative = isRotateLeft ? -0.261799f : 0.261799f;
// Default angular velocity is 3 units per second.
if (modelComponent.TrySetAngularVelocity(NiPoint3Constant::UNIT_X * negative)) {
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);
bool isRotateDown = nextActionType == "RollNegative";
float negative = isRotateDown ? -0.261799f : 0.261799f;
// Default angular velocity is 3 units per second.
if (modelComponent.TrySetAngularVelocity(NiPoint3Constant::UNIT_Z * negative)) {
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 */
/* BEGIN Navigation */
else if (nextActionType == "SetSpeed") {
modelComponent.SetSpeed(number);
@@ -277,36 +331,37 @@ void Strip::RemoveStates(ModelComponent& modelComponent) const {
}
bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) {
if (m_IsRotating) return true;
auto& entity = *modelComponent.GetParent();
const auto& currentPos = entity.GetPosition();
const auto diff = currentPos - m_PreviousFramePosition;
const auto [moveX, moveY, moveZ] = m_InActionMove;
const auto [moveX, moveY, moveZ] = m_InActionTranslation;
m_PreviousFramePosition = currentPos;
// Only want to subtract from the move if one is being performed.
// Starts at true because we may not be doing a move at all.
// If one is being done, then one of the move_ variables will be non-zero
bool moveFinished = true;
NiPoint3 finalPositionAdjustment = NiPoint3Constant::ZERO;
if (moveX != 0.0f) {
m_InActionMove.x -= diff.x;
m_InActionTranslation.x -= diff.x;
// If the sign bit is different between the two numbers, then we have finished our move.
moveFinished = std::signbit(m_InActionMove.x) != std::signbit(moveX);
finalPositionAdjustment.x = m_InActionMove.x;
moveFinished = std::signbit(m_InActionTranslation.x) != std::signbit(moveX);
finalPositionAdjustment.x = m_InActionTranslation.x;
} else if (moveY != 0.0f) {
m_InActionMove.y -= diff.y;
m_InActionTranslation.y -= diff.y;
// If the sign bit is different between the two numbers, then we have finished our move.
moveFinished = std::signbit(m_InActionMove.y) != std::signbit(moveY);
finalPositionAdjustment.y = m_InActionMove.y;
moveFinished = std::signbit(m_InActionTranslation.y) != std::signbit(moveY);
finalPositionAdjustment.y = m_InActionTranslation.y;
} else if (moveZ != 0.0f) {
m_InActionMove.z -= diff.z;
m_InActionTranslation.z -= diff.z;
// If the sign bit is different between the two numbers, then we have finished our move.
moveFinished = std::signbit(m_InActionMove.z) != std::signbit(moveZ);
finalPositionAdjustment.z = m_InActionMove.z;
moveFinished = std::signbit(m_InActionTranslation.z) != std::signbit(moveZ);
finalPositionAdjustment.z = m_InActionTranslation.z;
}
// Once done, set the in action move & velocity to zero
if (moveFinished && m_InActionMove != NiPoint3Constant::ZERO) {
if (moveFinished && m_InActionTranslation != NiPoint3Constant::ZERO) {
auto entityVelocity = entity.GetVelocity();
// Zero out only the velocity that was acted on
if (moveX != 0.0f) entityVelocity.x = 0.0f;
@@ -316,19 +371,71 @@ bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) {
// Do the final adjustment so we will have moved exactly the requested units
entity.SetPosition(entity.GetPosition() + finalPositionAdjustment);
m_InActionMove = NiPoint3Constant::ZERO;
m_InActionTranslation = NiPoint3Constant::ZERO;
}
return moveFinished;
}
bool Strip::CheckRotation(float deltaTime, ModelComponent& modelComponent) {
if (!m_IsRotating) return true;
GameMessages::GetAngularVelocity getAngVel{};
getAngVel.target = modelComponent.GetParent()->GetObjectID();
getAngVel.Send();
const auto curRotation = modelComponent.GetParent()->GetRotation();
// 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;
// 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();
// Snap to exact target to avoid tiny residual error
modelComponent.GetParent()->SetRotation(m_RotationTarget);
m_InActionTranslation = NiPoint3Constant::ZERO;
m_IsRotating = false;
return true;
}
// minimal logging retained elsewhere; per-frame verbose logs removed
// Not finished yet
return false;
}
void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
// No point in running a strip with only one action.
// Strips are also designed to have 2 actions or more to run.
if (!HasMinimumActions()) return;
// Return if this strip has an active movement action
// Return if this strip has an active movement or rotation action
if (!CheckMovement(deltaTime, modelComponent)) return;
if (!CheckRotation(deltaTime, modelComponent)) return;
// Don't run this strip if we're paused.
m_PausedTime -= deltaTime;
@@ -348,7 +455,6 @@ void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
if (m_NextActionIndex == 0) {
if (nextAction.GetType() == "OnInteract") {
modelComponent.AddInteract();
} else if (nextAction.GetType() == "OnChat") {
// logic here if needed
} else if (nextAction.GetType() == "OnAttack") {

View File

@@ -33,6 +33,10 @@ public:
// Checks the movement logic for whether or not to proceed
// Returns true if the movement can continue, false if it needs to wait more.
bool CheckMovement(float deltaTime, ModelComponent& modelComponent);
// Checks the rotation logic for whether or not to proceed
// Returns true if the rotation can continue, false if it needs to wait more.
bool CheckRotation(float deltaTime, ModelComponent& modelComponent);
void Update(float deltaTime, ModelComponent& modelComponent);
void SpawnDrop(LOT dropLOT, Entity& entity);
void ProcNormalAction(float deltaTime, ModelComponent& modelComponent);
@@ -47,6 +51,9 @@ private:
// Indicates this Strip is waiting for an action to be taken upon it to progress to its actions
bool m_WaitingForAction{ false };
// True if this strip is currently rotating
bool m_IsRotating{ false };
// The amount of time this strip is paused for. Any interactions with this strip should be bounced if this is greater than 0.
// Actions that do not use time do not use this (ex. positions).
float m_PausedTime{ 0.0f };
@@ -60,14 +67,26 @@ private:
// The location of this strip on the UGBehaviorEditor UI
StripUiPosition m_Position;
// The current actions remaining distance to the target
// The current actions remaining translation to the target
// Only 1 of these vertexs' will be active at once for any given strip.
NiPoint3 m_InActionMove{};
NiPoint3 m_InActionTranslation{};
// The position of the parent model on the previous frame
NiPoint3 m_PreviousFramePosition{};
NiPoint3 m_RotationRemaining{};
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__

View File

@@ -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})

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

@@ -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)

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

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