diff --git a/dGame/dComponents/ModelComponent.cpp b/dGame/dComponents/ModelComponent.cpp index f3b289988..842690f08 100644 --- a/dGame/dComponents/ModelComponent.cpp +++ b/dGame/dComponents/ModelComponent.cpp @@ -46,6 +46,9 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::ResetModelToDefaults& if (reset.bResetPos) m_Parent->SetPosition(m_OriginalPosition); if (reset.bResetRot) m_Parent->SetRotation(m_OriginalRotation); m_Parent->SetVelocity(NiPoint3Constant::ZERO); + m_MoveDirection = NiPoint3Constant::ZERO; + m_WasMoving = false; + SyncLinearVelocity(); ResetRotationState(m_Parent->GetRotation()); m_Speed = 3.0f; @@ -81,6 +84,9 @@ void ModelComponent::Update(float deltaTime) { behavior.Update(deltaTime, *this); } + // Done after all behaviors so the heading reflects any rotation applied this frame + SyncLinearVelocity(); + if (!m_RestartAtEndOfFrame) return; GameMessages::ResetModelToDefaults reset{}; @@ -289,32 +295,33 @@ void ModelComponent::RemoveUnSmash() { m_NumActiveUnSmash--; } -bool ModelComponent::TrySetVelocity(const NiPoint3& velocity) const { - auto currentVelocity = m_Parent->GetVelocity(); +bool ModelComponent::TryStartMove(const int axis, const float direction) { + if (axis < 0 || axis > 2 || direction == 0.0f) return false; + if (m_MoveDirection[axis] != 0.0f) return false; - // If we're currently moving on an axis, prevent the move so only 1 behavior can have control over an axis - if (velocity != NiPoint3Constant::ZERO) { - const auto [x, y, z] = velocity * m_Speed; - if (x != 0.0f) { - if (currentVelocity.x != 0.0f) return false; - currentVelocity.x = x; - } else if (y != 0.0f) { - if (currentVelocity.y != 0.0f) return false; - currentVelocity.y = y; - } else if (z != 0.0f) { - if (currentVelocity.z != 0.0f) return false; - currentVelocity.z = z; - } - } else { - currentVelocity = velocity; - } - - m_Parent->SetVelocity(currentVelocity); + m_MoveDirection[axis] = direction > 0.0f ? 1.0f : -1.0f; return true; } -void ModelComponent::SetVelocity(const NiPoint3& velocity) const { - m_Parent->SetVelocity(velocity); +void ModelComponent::StopMove(const int axis) { + if (axis < 0 || axis > 2) return; + m_MoveDirection[axis] = 0.0f; +} + +void ModelComponent::SyncLinearVelocity() { + const auto& rotation = m_Parent->GetRotation(); + m_MoveBasis = { QuatUtils::Right(rotation), QuatUtils::Up(rotation), QuatUtils::Forward(rotation) }; + + NiPoint3 velocity = NiPoint3Constant::ZERO; + for (int axis = 0; axis < 3; axis++) velocity += m_MoveBasis[axis] * (m_MoveDirection[axis] * m_Speed); + + // Leave velocity alone unless a move owns it, e.g. pets are driven elsewhere + const bool isMoving = velocity != NiPoint3Constant::ZERO; + if (!isMoving && !m_WasMoving) return; + m_WasMoving = isMoving; + + // Setting velocity always marks it dirty for serialization + if (velocity != m_Parent->GetVelocity()) m_Parent->SetVelocity(velocity); } bool ModelComponent::TryStartRotation(const int axis, const float direction) { diff --git a/dGame/dComponents/ModelComponent.h b/dGame/dComponents/ModelComponent.h index 10a75930d..c5dd987e1 100644 --- a/dGame/dComponents/ModelComponent.h +++ b/dGame/dComponents/ModelComponent.h @@ -139,13 +139,15 @@ public: void Resume(); - // Attempts to set the velocity of an axis for movement. - // If the axis currently has a velocity of zero, returns true. - // If the axis is currently controlled by a behavior, returns false. - bool TrySetVelocity(const NiPoint3& velocity) const; + // Attempts to claim a local axis (0 = right, 1 = up, 2 = forward) for movement in direction (+1 or -1). + // Returns false if the axis is already controlled by a behavior. + bool TryStartMove(const int axis, const float direction); - // Force sets the velocity to a value. - void SetVelocity(const NiPoint3& velocity) const; + // Releases the local axis so another behavior can move along it. + void StopMove(const int axis); + + // World space direction of the local axis used for the most recently applied velocity. + const NiPoint3& GetMoveAxis(const int axis) const { return m_MoveBasis[axis]; } // Attempts to claim a world axis (0 = x, 1 = y, 2 = z) for rotation in direction (+1 or -1). // Returns false if the axis is already controlled by a behavior. @@ -186,6 +188,9 @@ private: // Sends the client-side angular velocity for the currently active rotation axes. void SyncAngularVelocity() const; + // Recomputes the linear velocity from the active move axes and the current rotation. + void SyncLinearVelocity(); + // Clears all rotation state and makes rotation relative to newBase. void ResetRotationState(const NiQuaternion& newBase); @@ -248,4 +253,13 @@ private: // Per axis -1, 0 or 1. Non-zero means a behavior currently owns rotation on that axis. NiPoint3 m_RotationDirection{}; + + // Per local axis (right, up, forward) -1, 0 or 1. Non-zero means a behavior currently owns movement on that axis. + NiPoint3 m_MoveDirection{}; + + // Right, up and forward in world space as of the last velocity update + std::array m_MoveBasis{ NiPoint3Constant::UNIT_X, NiPoint3Constant::UNIT_Y, NiPoint3Constant::UNIT_Z }; + + // Whether the last velocity update came from an active move + bool m_WasMoving{ false }; }; diff --git a/dGame/dPropertyBehaviors/Strip.cpp b/dGame/dPropertyBehaviors/Strip.cpp index a85ffd989..1c7549195 100644 --- a/dGame/dPropertyBehaviors/Strip.cpp +++ b/dGame/dPropertyBehaviors/Strip.cpp @@ -174,30 +174,23 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent, Up // TODO replace with switch case and nextActionType with enum /* BEGIN Move */ if (nextActionType == "MoveRight" || nextActionType == "MoveLeft") { - // 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)) { + // Local right axis + const bool isMoveLeft = nextActionType == "MoveLeft"; + if (modelComponent.TryStartMove(0, isMoveLeft ? -1.0f : 1.0f)) { m_PreviousFramePosition = entity.GetPosition(); m_InActionTranslation.x = isMoveLeft ? -number : number; } } else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") { - // 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)) { + // Local up axis + const bool isFlyDown = nextActionType == "FlyDown"; + if (modelComponent.TryStartMove(1, isFlyDown ? -1.0f : 1.0f)) { m_PreviousFramePosition = entity.GetPosition(); m_InActionTranslation.y = isFlyDown ? -number : number; } - } else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") { - // 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)) { + // Local forward axis + const bool isMoveBackward = nextActionType == "MoveBackward"; + if (modelComponent.TryStartMove(2, isMoveBackward ? -1.0f : 1.0f)) { m_PreviousFramePosition = entity.GetPosition(); m_InActionTranslation.z = isMoveBackward ? -number : number; } @@ -332,45 +325,26 @@ bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) { auto& entity = *modelComponent.GetParent(); const auto& currentPos = entity.GetPosition(); const auto diff = currentPos - m_PreviousFramePosition; - 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_InActionTranslation.x -= diff.x; - // If the sign bit is different between the two numbers, then we have finished our move. - moveFinished = std::signbit(m_InActionTranslation.x) != std::signbit(moveX); - finalPositionAdjustment.x = m_InActionTranslation.x; - } else if (moveY != 0.0f) { - 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_InActionTranslation.y) != std::signbit(moveY); - finalPositionAdjustment.y = m_InActionTranslation.y; - } else if (moveZ != 0.0f) { - 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_InActionTranslation.z) != std::signbit(moveZ); - finalPositionAdjustment.z = m_InActionTranslation.z; - } - // Once done, set the in action move & velocity to 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; - else if (moveY != 0.0f) entityVelocity.y = 0.0f; - else if (moveZ != 0.0f) entityVelocity.z = 0.0f; - modelComponent.SetVelocity(entityVelocity); + for (int axis = 0; axis < 3; axis++) { + const float target = m_InActionTranslation[axis]; + if (target == 0.0f) continue; + + // The local axes are orthonormal so this isolates our axis from any other active moves + const auto& axisVector = modelComponent.GetMoveAxis(axis); + m_InActionTranslation[axis] -= diff.DotProduct(axisVector); + + // If the sign bit is different between the two numbers, then we have finished our move. + if (std::signbit(m_InActionTranslation[axis]) == std::signbit(target)) return false; // Do the final adjustment so we will have moved exactly the requested units - entity.SetPosition(entity.GetPosition() + finalPositionAdjustment); + entity.SetPosition(entity.GetPosition() + axisVector * m_InActionTranslation[axis]); + modelComponent.StopMove(axis); m_InActionTranslation = NiPoint3Constant::ZERO; } - return moveFinished; + return true; } bool Strip::CheckRotation(float deltaTime, ModelComponent& modelComponent) { diff --git a/dGame/dPropertyBehaviors/Strip.h b/dGame/dPropertyBehaviors/Strip.h index 142dfeb1d..2904a3529 100644 --- a/dGame/dPropertyBehaviors/Strip.h +++ b/dGame/dPropertyBehaviors/Strip.h @@ -65,7 +65,7 @@ private: // The location of this strip on the UGBehaviorEditor UI StripUiPosition m_Position; - // The current actions remaining translation to the target + // The current actions remaining translation to the target along the model's local right (x), up (y) and forward (z) axes. // Only 1 of these vertexs' will be active at once for any given strip. NiPoint3 m_InActionTranslation{};