From 42de987e2595c931520fd7bb75646d6d8366b329 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Fri, 4 Aug 2023 00:54:48 -0700 Subject: [PATCH] Keep updating --- dGame/dComponents/MovingPlatformComponent.cpp | 30 +++++++++++---- dGame/dComponents/MovingPlatformComponent.h | 1 + .../MovingPlatformComponentTests.cpp | 38 ++++++++++++++++++- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index caac434e..1ef7d935 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -35,6 +35,26 @@ PlatformSubComponent::PlatformSubComponent(MovingPlatformComponent* parentCompon m_IdleTimeElapsed = 0.0f; } +void PlatformSubComponent::Update(float deltaTime) { + if (m_State == 0) return; + if (m_State & eMovementPlatformState::Travelling) { + m_MoveTimeElapsed += deltaTime; + + // Only need to recalculate the linear velocity if the speed is changing between waypoints + // Unfortunately for the poor client, they chose to, instead of change the speed once at the start of the waypoint, + // the speed is changed over the course of the waypoint. This means we have to recalculate the linear velocity every frame. + // yay. + if (GetCurrentWaypoint().movingPlatform.speed != GetNextWaypoint().movingPlatform.speed) { + UpdateLinearVelocity(); + } + m_Position += m_LinearVelocity * deltaTime; + } +} + +void PlatformSubComponent::UpdateLinearVelocity() { + m_LinearVelocity = CalculateLinearVelocity(); +} + void PlatformSubComponent::AdvanceToNextWaypoint() { uint32_t numWaypoints = m_Path->pathWaypoints.size(); m_CurrentWaypointIndex = m_NextWaypointIndex; @@ -51,6 +71,7 @@ void PlatformSubComponent::AdvanceToNextWaypoint() { } } m_NextWaypointIndex = nextWaypointIndex; + UpdateLinearVelocity(); } void PlatformSubComponent::AdvanceToNextReverseWaypoint() { @@ -69,6 +90,7 @@ void PlatformSubComponent::AdvanceToNextReverseWaypoint() { } } m_NextWaypointIndex = nextWaypointIndex; + UpdateLinearVelocity(); } void PlatformSubComponent::SetupPath(const std::string& pathName, uint32_t startingWaypointIndex, bool startsInReverse) { @@ -157,14 +179,6 @@ void PlatformSubComponent::StopPathing() { m_AngularVelocity = NiPoint3::ZERO; } -void PlatformSubComponent::Update(float deltaTime) { - if (m_TimeBasedMovement && m_State & eMovementPlatformState::Travelling) { - m_MoveTimeElapsed += deltaTime; - } - if (m_State == 0) return; - -} - //------------- PlatformSubComponent end -------------- //------------- MoverPlatformSubComponent begin -------------- diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index 94157c33..02904925 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -62,6 +62,7 @@ public: void AdvanceToNextWaypoint(); void AdvanceToNextReverseWaypoint(); NiPoint3 CalculateLinearVelocity(); + void UpdateLinearVelocity(); protected: #ifdef _MOVING_PLATFORM_TEST diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index 8c70c0ee..00a7608c 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -30,7 +30,7 @@ protected: waypointMiddle.rotation = NiQuaternion(7, 8, 9, 10); PathWaypoint waypointEnd; - waypointEnd.position = NiPoint3(4, 5, 6); + waypointEnd.position = NiPoint3(4, 5, 7); waypointEnd.rotation = NiQuaternion(7, 8, 9, 10); path.pathWaypoints.push_back(waypointStart); @@ -375,7 +375,7 @@ TEST_F(MovingPlatformComponentTests, MovingPlatformMoverSpeedCalculationTest) { MoverPlatformSubComponent moverPlatformSubComponent(nullptr); path.pathWaypoints.at(0).position = NiPoint3(99.296440, 419.293335, 207.219498); path.pathWaypoints.at(0).movingPlatform.speed = 16.0f; - + path.pathWaypoints.at(1).position = NiPoint3(141.680099, 419.990051, 208.680450); path.pathWaypoints.at(1).movingPlatform.speed = 16.0f; path.pathBehavior = PathBehavior::Bounce; @@ -392,3 +392,37 @@ TEST_F(MovingPlatformComponentTests, MovingPlatformMoverSpeedCalculationTest) { ASSERT_FLOAT_EQ(r.z, 0.5511137843132); moverPlatformSubComponent.AdvanceToNextWaypoint(); } + +TEST_F(MovingPlatformComponentTests, MovingPlatformNextAndCurrentWaypointAccess) { + MoverPlatformSubComponent moverPlatformSubComponent(nullptr); + moverPlatformSubComponent._SetPath(&path); + moverPlatformSubComponent.m_CurrentWaypointIndex = 0; + moverPlatformSubComponent.m_NextWaypointIndex = 1; + ASSERT_EQ(moverPlatformSubComponent.GetCurrentWaypoint().position, NiPoint3(1, 2, 3)); + ASSERT_EQ(moverPlatformSubComponent.GetNextWaypoint().position, NiPoint3(4, 5, 6)); + moverPlatformSubComponent.AdvanceToNextWaypoint(); + ASSERT_EQ(moverPlatformSubComponent.GetCurrentWaypoint().position, NiPoint3(4, 5, 6)); + ASSERT_EQ(moverPlatformSubComponent.GetNextWaypoint().position, NiPoint3(4, 5, 7)); +} + +TEST_F(MovingPlatformComponentTests, MovingPlatformRunTest) { + MoverPlatformSubComponent moverPlatformSubComponent(nullptr); + moverPlatformSubComponent._SetPath(&path); + path.pathWaypoints.at(0).position = NiPoint3(99.296440f, 419.293335f, 207.219498f); + path.pathWaypoints.at(0).movingPlatform.speed = 16.0f; + path.pathWaypoints.at(1).position = NiPoint3(141.680099f, 419.990051f, 208.680450f); + path.pathWaypoints.at(1).movingPlatform.speed = 16.0f; + moverPlatformSubComponent.m_State = 0; + moverPlatformSubComponent.m_TimeBasedMovement = false; + moverPlatformSubComponent.m_State = eMovementPlatformState::Travelling; + moverPlatformSubComponent.m_CurrentWaypointIndex = 0; + moverPlatformSubComponent.m_NextWaypointIndex = 1; + moverPlatformSubComponent.m_Speed = 16.0f; + moverPlatformSubComponent.m_Position = path.pathWaypoints.at(0).position; + moverPlatformSubComponent.Update(2.65f); + + // just check that its close enough + EXPECT_LT(141.680099f - moverPlatformSubComponent.m_Position.x, 0.1f); + EXPECT_LT(419.990051f - moverPlatformSubComponent.m_Position.y, 0.1f); + EXPECT_LT(208.680450f - moverPlatformSubComponent.m_Position.z, 0.1f); +}