diff --git a/dCommon/NiQuaternion.cpp b/dCommon/NiQuaternion.cpp index b12d3991..1677e180 100644 --- a/dCommon/NiQuaternion.cpp +++ b/dCommon/NiQuaternion.cpp @@ -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) diff --git a/dCommon/NiQuaternion.h b/dCommon/NiQuaternion.h index 482b86fa..f4b380c3 100644 --- a/dCommon/NiQuaternion.h +++ b/dCommon/NiQuaternion.h @@ -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 diff --git a/dCommon/dEnums/dCommonVars.h b/dCommon/dEnums/dCommonVars.h index 75dcc73d..ad127876 100644 --- a/dCommon/dEnums/dCommonVars.h +++ b/dCommon/dEnums/dCommonVars.h @@ -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 ============== diff --git a/dCommon/dMath.h b/dCommon/dMath.h new file mode 100644 index 00000000..f6231335 --- /dev/null +++ b/dCommon/dMath.h @@ -0,0 +1,23 @@ +// Darkflame Universe +// Copyright 2025 + +#ifndef DMATH_H +#define DMATH_H + +#include + +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 diff --git a/dGame/dComponents/ModelComponent.cpp b/dGame/dComponents/ModelComponent.cpp index 6066fa4b..baf180ce 100644 --- a/dGame/dComponents/ModelComponent.cpp +++ b/dGame/dComponents/ModelComponent.cpp @@ -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); } diff --git a/dGame/dComponents/ModelComponent.h b/dGame/dComponents/ModelComponent.h index e5fe83a0..8f36fa91 100644 --- a/dGame/dComponents/ModelComponent.h +++ b/dGame/dComponents/ModelComponent.h @@ -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. diff --git a/dGame/dComponents/SimplePhysicsComponent.cpp b/dGame/dComponents/SimplePhysicsComponent.cpp index 3651c10f..c42d5966 100644 --- a/dGame/dComponents/SimplePhysicsComponent.cpp +++ b/dGame/dComponents/SimplePhysicsComponent.cpp @@ -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(this, &SimplePhysicsComponent::OnGetObjectReportInfo); + RegisterMsg(this, &SimplePhysicsComponent::OnGetAngularVelocity); + RegisterMsg(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("Climbable Type") = StringifiedEnum::ToString(m_ClimbableType).data(); return true; } + +bool SimplePhysicsComponent::OnSetAngularVelocity(GameMessages::GameMsg& msg) { + auto& setAngVel = static_cast(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(msg); + getAngVel.angVelocity = m_AngularVelocity; + return true; +} diff --git a/dGame/dComponents/SimplePhysicsComponent.h b/dGame/dComponents/SimplePhysicsComponent.h index f8db9197..17b3f74e 100644 --- a/dGame/dComponents/SimplePhysicsComponent.h +++ b/dGame/dComponents/SimplePhysicsComponent.h @@ -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 diff --git a/dGame/dGameMessages/GameMessages.h b/dGame/dGameMessages/GameMessages.h index cf6e7adf..a64f2a18 100644 --- a/dGame/dGameMessages/GameMessages.h +++ b/dGame/dGameMessages/GameMessages.h @@ -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 diff --git a/dGame/dPropertyBehaviors/Strip.cpp b/dGame/dPropertyBehaviors/Strip.cpp index a923691e..788e3285 100644 --- a/dGame/dPropertyBehaviors/Strip.cpp +++ b/dGame/dPropertyBehaviors/Strip.cpp @@ -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; } @@ -159,40 +160,84 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) { auto valueStr = nextAction.GetValueParameterString(); auto numberAsInt = static_cast(number); auto nextActionType = GetNextAction().GetType(); + LOG("~number: %f, nextActionType: %s", static_cast(number), nextActionType.data()); // 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(); + // 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(); + } + } 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(); + } + } + /* END Rotate */ + /* BEGIN Navigation */ else if (nextActionType == "SetSpeed") { modelComponent.SetSpeed(number); @@ -277,36 +322,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 +362,82 @@ 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(); + 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); + 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; + + 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); + + m_InActionTranslation = NiPoint3Constant::ZERO; + m_IsRotating = false; + } + + LOG("angVel: x=%f, y=%f, z=%f", m_InActionTranslation.x, m_InActionTranslation.y, m_InActionTranslation.z); + return rotateFinished; +} + 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 +457,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") { diff --git a/dGame/dPropertyBehaviors/Strip.h b/dGame/dPropertyBehaviors/Strip.h index 1a61afd5..3fc8676d 100644 --- a/dGame/dPropertyBehaviors/Strip.h +++ b/dGame/dPropertyBehaviors/Strip.h @@ -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,13 +67,17 @@ 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{}; + NiPoint3 m_SavedVelocity{}; };