mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 02:43:44 +00:00
feat: make directional movements based on model rotation and not world axes
This commit is contained in:
@@ -46,6 +46,9 @@ bool ModelComponent::OnResetModelToDefaults(GameMessages::ResetModelToDefaults&
|
|||||||
if (reset.bResetPos) m_Parent->SetPosition(m_OriginalPosition);
|
if (reset.bResetPos) m_Parent->SetPosition(m_OriginalPosition);
|
||||||
if (reset.bResetRot) m_Parent->SetRotation(m_OriginalRotation);
|
if (reset.bResetRot) m_Parent->SetRotation(m_OriginalRotation);
|
||||||
m_Parent->SetVelocity(NiPoint3Constant::ZERO);
|
m_Parent->SetVelocity(NiPoint3Constant::ZERO);
|
||||||
|
m_MoveDirection = NiPoint3Constant::ZERO;
|
||||||
|
m_WasMoving = false;
|
||||||
|
SyncLinearVelocity();
|
||||||
ResetRotationState(m_Parent->GetRotation());
|
ResetRotationState(m_Parent->GetRotation());
|
||||||
|
|
||||||
m_Speed = 3.0f;
|
m_Speed = 3.0f;
|
||||||
@@ -81,6 +84,9 @@ void ModelComponent::Update(float deltaTime) {
|
|||||||
behavior.Update(deltaTime, *this);
|
behavior.Update(deltaTime, *this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Done after all behaviors so the heading reflects any rotation applied this frame
|
||||||
|
SyncLinearVelocity();
|
||||||
|
|
||||||
if (!m_RestartAtEndOfFrame) return;
|
if (!m_RestartAtEndOfFrame) return;
|
||||||
|
|
||||||
GameMessages::ResetModelToDefaults reset{};
|
GameMessages::ResetModelToDefaults reset{};
|
||||||
@@ -289,32 +295,33 @@ void ModelComponent::RemoveUnSmash() {
|
|||||||
m_NumActiveUnSmash--;
|
m_NumActiveUnSmash--;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ModelComponent::TrySetVelocity(const NiPoint3& velocity) const {
|
bool ModelComponent::TryStartMove(const int axis, const float direction) {
|
||||||
auto currentVelocity = m_Parent->GetVelocity();
|
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
|
m_MoveDirection[axis] = direction > 0.0f ? 1.0f : -1.0f;
|
||||||
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);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ModelComponent::SetVelocity(const NiPoint3& velocity) const {
|
void ModelComponent::StopMove(const int axis) {
|
||||||
m_Parent->SetVelocity(velocity);
|
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) {
|
bool ModelComponent::TryStartRotation(const int axis, const float direction) {
|
||||||
|
|||||||
@@ -139,13 +139,15 @@ public:
|
|||||||
|
|
||||||
void Resume();
|
void Resume();
|
||||||
|
|
||||||
// Attempts to set the velocity of an axis for movement.
|
// Attempts to claim a local axis (0 = right, 1 = up, 2 = forward) for movement in direction (+1 or -1).
|
||||||
// If the axis currently has a velocity of zero, returns true.
|
// Returns false if the axis is already controlled by a behavior.
|
||||||
// If the axis is currently controlled by a behavior, returns false.
|
bool TryStartMove(const int axis, const float direction);
|
||||||
bool TrySetVelocity(const NiPoint3& velocity) const;
|
|
||||||
|
|
||||||
// Force sets the velocity to a value.
|
// Releases the local axis so another behavior can move along it.
|
||||||
void SetVelocity(const NiPoint3& velocity) const;
|
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).
|
// 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.
|
// 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.
|
// Sends the client-side angular velocity for the currently active rotation axes.
|
||||||
void SyncAngularVelocity() const;
|
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.
|
// Clears all rotation state and makes rotation relative to newBase.
|
||||||
void ResetRotationState(const NiQuaternion& 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.
|
// Per axis -1, 0 or 1. Non-zero means a behavior currently owns rotation on that axis.
|
||||||
NiPoint3 m_RotationDirection{};
|
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<NiPoint3, 3> 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 };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -174,30 +174,23 @@ void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent, Up
|
|||||||
// 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") {
|
||||||
// X axis
|
// Local right axis
|
||||||
bool isMoveLeft = nextActionType == "MoveLeft";
|
const bool isMoveLeft = nextActionType == "MoveLeft";
|
||||||
int negative = isMoveLeft ? -1 : 1;
|
if (modelComponent.TryStartMove(0, isMoveLeft ? -1.0f : 1.0f)) {
|
||||||
// Default velocity is 3 units per second.
|
|
||||||
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_X * negative)) {
|
|
||||||
m_PreviousFramePosition = entity.GetPosition();
|
m_PreviousFramePosition = entity.GetPosition();
|
||||||
m_InActionTranslation.x = isMoveLeft ? -number : number;
|
m_InActionTranslation.x = isMoveLeft ? -number : number;
|
||||||
}
|
}
|
||||||
} else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") {
|
} else if (nextActionType == "FlyUp" || nextActionType == "FlyDown") {
|
||||||
// Y axis
|
// Local up axis
|
||||||
bool isFlyDown = nextActionType == "FlyDown";
|
const bool isFlyDown = nextActionType == "FlyDown";
|
||||||
int negative = isFlyDown ? -1 : 1;
|
if (modelComponent.TryStartMove(1, isFlyDown ? -1.0f : 1.0f)) {
|
||||||
// Default velocity is 3 units per second.
|
|
||||||
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Y * negative)) {
|
|
||||||
m_PreviousFramePosition = entity.GetPosition();
|
m_PreviousFramePosition = entity.GetPosition();
|
||||||
m_InActionTranslation.y = isFlyDown ? -number : number;
|
m_InActionTranslation.y = isFlyDown ? -number : number;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") {
|
} else if (nextActionType == "MoveForward" || nextActionType == "MoveBackward") {
|
||||||
// Z axis
|
// Local forward axis
|
||||||
bool isMoveBackward = nextActionType == "MoveBackward";
|
const bool isMoveBackward = nextActionType == "MoveBackward";
|
||||||
int negative = isMoveBackward ? -1 : 1;
|
if (modelComponent.TryStartMove(2, isMoveBackward ? -1.0f : 1.0f)) {
|
||||||
// Default velocity is 3 units per second.
|
|
||||||
if (modelComponent.TrySetVelocity(NiPoint3Constant::UNIT_Z * negative)) {
|
|
||||||
m_PreviousFramePosition = entity.GetPosition();
|
m_PreviousFramePosition = entity.GetPosition();
|
||||||
m_InActionTranslation.z = isMoveBackward ? -number : number;
|
m_InActionTranslation.z = isMoveBackward ? -number : number;
|
||||||
}
|
}
|
||||||
@@ -332,45 +325,26 @@ bool Strip::CheckMovement(float deltaTime, ModelComponent& modelComponent) {
|
|||||||
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_InActionTranslation;
|
|
||||||
m_PreviousFramePosition = currentPos;
|
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
|
for (int axis = 0; axis < 3; axis++) {
|
||||||
if (moveFinished && m_InActionTranslation != NiPoint3Constant::ZERO) {
|
const float target = m_InActionTranslation[axis];
|
||||||
auto entityVelocity = entity.GetVelocity();
|
if (target == 0.0f) continue;
|
||||||
// Zero out only the velocity that was acted on
|
|
||||||
if (moveX != 0.0f) entityVelocity.x = 0.0f;
|
// The local axes are orthonormal so this isolates our axis from any other active moves
|
||||||
else if (moveY != 0.0f) entityVelocity.y = 0.0f;
|
const auto& axisVector = modelComponent.GetMoveAxis(axis);
|
||||||
else if (moveZ != 0.0f) entityVelocity.z = 0.0f;
|
m_InActionTranslation[axis] -= diff.DotProduct(axisVector);
|
||||||
modelComponent.SetVelocity(entityVelocity);
|
|
||||||
|
// 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
|
// 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;
|
m_InActionTranslation = NiPoint3Constant::ZERO;
|
||||||
}
|
}
|
||||||
|
|
||||||
return moveFinished;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Strip::CheckRotation(float deltaTime, ModelComponent& modelComponent) {
|
bool Strip::CheckRotation(float deltaTime, ModelComponent& modelComponent) {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ 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 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.
|
// Only 1 of these vertexs' will be active at once for any given strip.
|
||||||
NiPoint3 m_InActionTranslation{};
|
NiPoint3 m_InActionTranslation{};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user