Keep updating

This commit is contained in:
David Markowitz 2023-08-04 00:54:48 -07:00
parent e6fe2edfae
commit 42de987e25
3 changed files with 59 additions and 10 deletions

View File

@ -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 --------------

View File

@ -62,6 +62,7 @@ public:
void AdvanceToNextWaypoint();
void AdvanceToNextReverseWaypoint();
NiPoint3 CalculateLinearVelocity();
void UpdateLinearVelocity();
protected:
#ifdef _MOVING_PLATFORM_TEST

View File

@ -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);
}