add rotation behavior logic (not working correctly for multiple axes)

This commit is contained in:
David Markowitz
2025-08-31 00:13:56 -07:00
parent 3890c0a86c
commit 4d043398ab
11 changed files with 344 additions and 35 deletions

View File

@@ -14,13 +14,14 @@ Vector3 NiQuaternion::GetEulerAngles() const {
angles.x = std::atan2(sinr_cosp, cosr_cosp); angles.x = std::atan2(sinr_cosp, cosr_cosp);
// pitch (y-axis rotation) // 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) { // if (std::abs(p) >= 1) {
angles.y = std::copysign(3.14 / 2, sinp); // use 90 degrees if out of range // angles.y = std::copysign(3.14 / 2, p); // use 90 degrees if out of range
} else { // } else {
angles.y = std::asin(sinp); // angles.y = std::asin(p);
} // }
// yaw (z-axis rotation) // yaw (z-axis rotation)
const float siny_cosp = 2 * (w * z + x * y); const float siny_cosp = 2 * (w * z + x * y);
@@ -30,6 +31,65 @@ Vector3 NiQuaternion::GetEulerAngles() const {
return angles; 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 // MARK: Helper Functions
//! Look from a specific point in space to another point in space (Y-locked) //! 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; [[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 // MARK: Operators
//! Operator to check for equality //! 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 LWOMAPID LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID
constexpr uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID constexpr uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID
constexpr float PI = 3.14159f; constexpr float PI = 3.14159265358979323846264338327950288f;
//============ STRUCTS ============== //============ 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

@@ -18,6 +18,7 @@ ModelComponent::ModelComponent(Entity* parent) : Component(parent) {
using namespace GameMessages; using namespace GameMessages;
m_OriginalPosition = m_Parent->GetDefaultPosition(); m_OriginalPosition = m_Parent->GetDefaultPosition();
m_OriginalRotation = m_Parent->GetDefaultRotation(); 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_IsPaused = false;
m_NumListeningInteract = 0; m_NumListeningInteract = 0;
@@ -37,6 +38,10 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::GameMsg& msg) {
m_Parent->SetPosition(m_OriginalPosition); m_Parent->SetPosition(m_OriginalPosition);
m_Parent->SetRotation(m_OriginalRotation); m_Parent->SetRotation(m_OriginalRotation);
m_Parent->SetVelocity(NiPoint3Constant::ZERO); m_Parent->SetVelocity(NiPoint3Constant::ZERO);
GameMessages::SetAngularVelocity setAngVel;
setAngVel.target = m_Parent->GetObjectID();
setAngVel.angVelocity = NiPoint3Constant::ZERO;
setAngVel.Send();
m_Speed = 3.0f; m_Speed = 3.0f;
m_NumListeningInteract = 0; m_NumListeningInteract = 0;
@@ -303,6 +308,38 @@ void ModelComponent::SetVelocity(const NiPoint3& velocity) const {
m_Parent->SetVelocity(velocity); 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) { void ModelComponent::OnChatMessageReceived(const std::string& sMessage) {
for (auto& behavior : m_Behaviors) behavior.OnChatMessageReceived(sMessage); for (auto& behavior : m_Behaviors) behavior.OnChatMessageReceived(sMessage);
} }

View File

@@ -144,6 +144,11 @@ public:
// Force sets the velocity to a value. // Force sets the velocity to a value.
void SetVelocity(const NiPoint3& velocity) const; 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 OnChatMessageReceived(const std::string& sMessage);
void OnHit(); void OnHit();
@@ -161,6 +166,8 @@ public:
// Decrements the number of strips listening for an attack. // 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. // If this is the last strip removing an attack, it will reset the factions to the default of -1.
void RemoveAttack(); void RemoveAttack();
float GetSpeed() const noexcept { return m_Speed; }
private: private:
// Loads a behavior from the database. // Loads a behavior from the database.

View File

@@ -16,7 +16,10 @@
#include "Amf3.h" #include "Amf3.h"
SimplePhysicsComponent::SimplePhysicsComponent(Entity* parent, int32_t componentID) : PhysicsComponent(parent, componentID) { 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_Position = m_Parent->GetDefaultPosition();
m_Rotation = m_Parent->GetDefaultRotation(); m_Rotation = m_Parent->GetDefaultRotation();
@@ -38,12 +41,22 @@ SimplePhysicsComponent::~SimplePhysicsComponent() {
} }
void SimplePhysicsComponent::Update(const float deltaTime) { void SimplePhysicsComponent::Update(const float deltaTime) {
if (m_Velocity == NiPoint3Constant::ZERO) return; if (m_Velocity != NiPoint3Constant::ZERO) {
m_Position += m_Velocity * deltaTime; m_Position += m_Velocity * deltaTime;
m_DirtyPosition = true; m_DirtyPosition = true;
Game::entityManager->SerializeEntity(m_Parent); 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) { void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
if (bIsInitialUpdate) { if (bIsInitialUpdate) {
outBitStream.Write(m_ClimbableType != eClimbableType::CLIMBABLE_TYPE_NOT); outBitStream.Write(m_ClimbableType != eClimbableType::CLIMBABLE_TYPE_NOT);
@@ -52,8 +65,12 @@ void SimplePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIs
outBitStream.Write(m_DirtyVelocity || bIsInitialUpdate); outBitStream.Write(m_DirtyVelocity || bIsInitialUpdate);
if (m_DirtyVelocity || bIsInitialUpdate) { if (m_DirtyVelocity || bIsInitialUpdate) {
outBitStream.Write(m_Velocity); outBitStream.Write(m_Velocity.x);
outBitStream.Write(m_AngularVelocity); 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; m_DirtyVelocity = false;
} }
@@ -92,3 +109,18 @@ bool SimplePhysicsComponent::OnGetObjectReportInfo(GameMessages::GameMsg& msg) {
info.PushDebug<AMFStringValue>("Climbable Type") = StringifiedEnum::ToString(m_ClimbableType).data(); info.PushDebug<AMFStringValue>("Climbable Type") = StringifiedEnum::ToString(m_ClimbableType).data();
return true; 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; } 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 * Returns the physics motion state
* @return the physics motion state * @return the physics motion state

View File

@@ -871,5 +871,21 @@ namespace GameMessages {
bool bIgnoreChecks{ false }; 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 #endif // GAMEMESSAGES_H

View File

@@ -9,6 +9,7 @@
#include "PropertyManagementComponent.h" #include "PropertyManagementComponent.h"
#include "PlayerManager.h" #include "PlayerManager.h"
#include "SimplePhysicsComponent.h" #include "SimplePhysicsComponent.h"
#include "dMath.h"
#include "dChatFilter.h" #include "dChatFilter.h"
@@ -104,7 +105,7 @@ void Strip::HandleMsg(GameMessages::ResetModelToDefaults& msg) {
m_WaitingForAction = false; m_WaitingForAction = false;
m_PausedTime = 0.0f; m_PausedTime = 0.0f;
m_NextActionIndex = 0; m_NextActionIndex = 0;
m_InActionMove = NiPoint3Constant::ZERO; m_InActionTranslation = NiPoint3Constant::ZERO;
m_PreviousFramePosition = NiPoint3Constant::ZERO; m_PreviousFramePosition = NiPoint3Constant::ZERO;
} }
@@ -159,40 +160,84 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
auto valueStr = nextAction.GetValueParameterString(); auto valueStr = nextAction.GetValueParameterString();
auto numberAsInt = static_cast<int32_t>(number); auto numberAsInt = static_cast<int32_t>(number);
auto nextActionType = GetNextAction().GetType(); auto nextActionType = GetNextAction().GetType();
LOG("~number: %f, nextActionType: %s", static_cast<float>(number), nextActionType.data());
// TODO replace with switch case and nextActionType with enum // TODO replace with switch case and nextActionType with enum
/* BEGIN Move */ /* BEGIN Move */
if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") { if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") {
m_IsRotating = false;
// X axis // X axis
bool isMoveLeft = nextActionType == "MoveLeft"; bool isMoveLeft = nextActionType == "MoveLeft";
int negative = isMoveLeft ? -1 : 1; int negative = isMoveLeft ? -1 : 1;
// Default velocity is 3 units per second. // Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_X * negative)) { if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_X * negative)) {
m_PreviousFramePosition = entity.GetPosition(); m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.x = isMoveLeft ? -number : number; m_InActionTranslation.x = isMoveLeft ? -number : number;
} }
} else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") { } else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") {
m_IsRotating = false;
// Y axis // Y axis
bool isFlyDown = nextActionType == "FlyDown"; bool isFlyDown = nextActionType == "FlyDown";
int negative = isFlyDown ? -1 : 1; int negative = isFlyDown ? -1 : 1;
// Default velocity is 3 units per second. // Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Y * negative)) { if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Y * negative)) {
m_PreviousFramePosition = entity.GetPosition(); m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.y = isFlyDown ? -number : number; m_InActionTranslation.y = isFlyDown ? -number : number;
} }
} else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") { } else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") {
m_IsRotating = false;
// Z axis // Z axis
bool isMoveBackward = nextActionType == "MoveBackward"; bool isMoveBackward = nextActionType == "MoveBackward";
int negative = isMoveBackward ? -1 : 1; int negative = isMoveBackward ? -1 : 1;
// Default velocity is 3 units per second. // Default velocity is 3 units per second.
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Z * negative)) { if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Z * negative)) {
m_PreviousFramePosition = entity.GetPosition(); m_PreviousFramePosition = entity.GetPosition();
m_InActionMove.z = isMoveBackward ? -number : number; m_InActionTranslation.z = isMoveBackward ? -number : number;
} }
} }
/* END Move */ /* 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 */ /* BEGIN Navigation */
else if (nextActionType == "SetSpeed") { else if (nextActionType == "SetSpeed") {
modelComponent.SetSpeed(number); modelComponent.SetSpeed(number);
@@ -277,36 +322,37 @@ void Strip::RemoveStates(ModelComponent& modelComponent) const {
} }
bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) { bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) {
if (m_IsRotating) return true;
auto& entity = *modelComponent.GetParent(); auto& entity = *modelComponent.GetParent();
const auto& currentPos = entity.GetPosition(); const auto& currentPos = entity.GetPosition();
const auto diff = currentPos - m_PreviousFramePosition; const auto diff = currentPos - m_PreviousFramePosition;
const auto [moveX, moveY, moveZ] = m_InActionMove; const auto [moveX, moveY, moveZ] = m_InActionTranslation;
m_PreviousFramePosition = currentPos; m_PreviousFramePosition = currentPos;
// Only want to subtract from the move if one is being performed. // 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. // 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 // If one is being done, then one of the move_ variables will be non-zero
bool moveFinished = true; bool moveFinished = true;
NiPoint3 finalPositionAdjustment = NiPoint3Constant::ZERO; NiPoint3 finalPositionAdjustment = NiPoint3Constant::ZERO;
if (moveX != 0.0f) { 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. // 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); moveFinished = std::signbit(m_InActionTranslation.x) != std::signbit(moveX);
finalPositionAdjustment.x = m_InActionMove.x; finalPositionAdjustment.x = m_InActionTranslation.x;
} else if (moveY != 0.0f) { } 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. // 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); moveFinished = std::signbit(m_InActionTranslation.y) != std::signbit(moveY);
finalPositionAdjustment.y = m_InActionMove.y; finalPositionAdjustment.y = m_InActionTranslation.y;
} else if (moveZ != 0.0f) { } 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. // 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); moveFinished = std::signbit(m_InActionTranslation.z) != std::signbit(moveZ);
finalPositionAdjustment.z = m_InActionMove.z; finalPositionAdjustment.z = m_InActionTranslation.z;
} }
// Once done, set the in action move & velocity to zero // 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(); auto entityVelocity = entity.GetVelocity();
// Zero out only the velocity that was acted on // Zero out only the velocity that was acted on
if (moveX != 0.0f) entityVelocity.x = 0.0f; 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 // Do the final adjustment so we will have moved exactly the requested units
entity.SetPosition(entity.GetPosition() + finalPositionAdjustment); entity.SetPosition(entity.GetPosition() + finalPositionAdjustment);
m_InActionMove = NiPoint3Constant::ZERO; m_InActionTranslation = NiPoint3Constant::ZERO;
} }
return moveFinished; 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) { void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
// No point in running a strip with only one action. // No point in running a strip with only one action.
// Strips are also designed to have 2 actions or more to run. // Strips are also designed to have 2 actions or more to run.
if (!HasMinimumActions()) return; 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 (!CheckMovement(deltaTime, modelComponent)) return;
if (!CheckRotation(deltaTime, modelComponent)) return;
// Don't run this strip if we're paused. // Don't run this strip if we're paused.
m_PausedTime -= deltaTime; m_PausedTime -= deltaTime;
@@ -348,7 +457,6 @@ void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
if (m_NextActionIndex == 0) { if (m_NextActionIndex == 0) {
if (nextAction.GetType() == "OnInteract") { if (nextAction.GetType() == "OnInteract") {
modelComponent.AddInteract(); modelComponent.AddInteract();
} else if (nextAction.GetType() == "OnChat") { } else if (nextAction.GetType() == "OnChat") {
// logic here if needed // logic here if needed
} else if (nextAction.GetType() == "OnAttack") { } else if (nextAction.GetType() == "OnAttack") {

View File

@@ -33,6 +33,10 @@ public:
// Checks the movement logic for whether or not to proceed // Checks the movement logic for whether or not to proceed
// Returns true if the movement can continue, false if it needs to wait more. // Returns true if the movement can continue, false if it needs to wait more.
bool CheckMovement(float deltaTime, ModelComponent& modelComponent); 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 Update(float deltaTime, ModelComponent& modelComponent);
void SpawnDrop(LOT dropLOT, Entity& entity); void SpawnDrop(LOT dropLOT, Entity& entity);
void ProcNormalAction(float deltaTime, ModelComponent& modelComponent); 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 // Indicates this Strip is waiting for an action to be taken upon it to progress to its actions
bool m_WaitingForAction{ false }; 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. // 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). // Actions that do not use time do not use this (ex. positions).
float m_PausedTime{ 0.0f }; float m_PausedTime{ 0.0f };
@@ -60,13 +67,17 @@ private:
// The location of this strip on the UGBehaviorEditor UI // The location of this strip on the UGBehaviorEditor UI
StripUiPosition m_Position; 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. // 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 // The position of the parent model on the previous frame
NiPoint3 m_PreviousFramePosition{}; NiPoint3 m_PreviousFramePosition{};
NiPoint3 m_RotationRemaining{};
NiQuaternion m_PreviousFrameRotation{};
NiPoint3 m_SavedVelocity{}; NiPoint3 m_SavedVelocity{};
}; };