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

@@ -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;
}
@@ -159,40 +160,84 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
auto valueStr = nextAction.GetValueParameterString();
auto numberAsInt = static_cast<int32_t>(number);
auto nextActionType = GetNextAction().GetType();
LOG("~number: %f, nextActionType: %s", static_cast<float>(number), nextActionType.data());
// TODO replace with switch case and nextActionType with enum
/* BEGIN Move */
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") {

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,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{};
};