Ensure physics are predicted correctly

Update the PhysicsEntity position so it always matched the physics position.
This commit is contained in:
David Markowitz 2023-08-10 02:47:27 -07:00
parent fba8fc9c45
commit 40c59c7f51
2 changed files with 20 additions and 15 deletions

View File

@ -72,16 +72,11 @@ ControllablePhysicsComponent::~ControllablePhysicsComponent() {
void ControllablePhysicsComponent::Update(float deltaTime) {
if (m_Velocity == NiPoint3::ZERO) return;
m_Position += m_Velocity * deltaTime;
m_DirtyPosition = true;
SetPosition(m_Position + (m_Velocity * deltaTime));
Game::entityManager->SerializeEntity(m_Parent);
}
void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
//If this is a creation, then we assume the position is dirty, even when it isn't.
//This is because new clients will still need to receive the position.
//if (bIsInitialUpdate) m_DirtyPosition = true;
if (bIsInitialUpdate) {
outBitStream->Write(m_InJetpackMode);
if (m_InJetpackMode) {
@ -107,14 +102,14 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bo
outBitStream->Write(m_GravityScale);
outBitStream->Write(m_SpeedMultiplier);
m_DirtyCheats = false;
if (!bIsInitialUpdate) m_DirtyCheats = false;
}
outBitStream->Write(m_DirtyEquippedItemInfo);
if (m_DirtyEquippedItemInfo) {
outBitStream->Write(m_PickupRadius);
outBitStream->Write(m_InJetpackMode);
m_DirtyEquippedItemInfo = false;
if (!bIsInitialUpdate) m_DirtyEquippedItemInfo = false;
}
outBitStream->Write(m_DirtyBubble);
@ -124,7 +119,7 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bo
outBitStream->Write(m_BubbleType);
outBitStream->Write(m_SpecialAnims);
}
m_DirtyBubble = false;
if (!bIsInitialUpdate) m_DirtyBubble = false;
}
outBitStream->Write(m_DirtyPosition || bIsInitialUpdate);
@ -146,6 +141,7 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bo
outBitStream->Write(m_Velocity.x);
outBitStream->Write(m_Velocity.y);
outBitStream->Write(m_Velocity.z);
m_DirtyVelocity = false;
}
outBitStream->Write(m_DirtyAngularVelocity);
@ -153,6 +149,7 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bo
outBitStream->Write(m_AngularVelocity.x);
outBitStream->Write(m_AngularVelocity.y);
outBitStream->Write(m_AngularVelocity.z);
m_DirtyAngularVelocity = false;
}
outBitStream->Write0();
@ -212,7 +209,7 @@ void ControllablePhysicsComponent::UpdateXml(tinyxml2::XMLDocument* doc) {
}
void ControllablePhysicsComponent::SetPosition(const NiPoint3& pos) {
if (m_Static) {
if (m_Static || m_Position == pos) {
return;
}
@ -225,7 +222,7 @@ void ControllablePhysicsComponent::SetPosition(const NiPoint3& pos) {
}
void ControllablePhysicsComponent::SetRotation(const NiQuaternion& rot) {
if (m_Static) {
if (m_Static || m_Rotation == rot) {
return;
}
@ -236,7 +233,7 @@ void ControllablePhysicsComponent::SetRotation(const NiQuaternion& rot) {
}
void ControllablePhysicsComponent::SetVelocity(const NiPoint3& vel) {
if (m_Static) {
if (m_Static || m_Velocity == vel) {
return;
}
@ -248,7 +245,7 @@ void ControllablePhysicsComponent::SetVelocity(const NiPoint3& vel) {
}
void ControllablePhysicsComponent::SetAngularVelocity(const NiPoint3& vel) {
if (m_Static) {
if (m_Static || m_AngularVelocity == vel) {
return;
}
@ -258,11 +255,13 @@ void ControllablePhysicsComponent::SetAngularVelocity(const NiPoint3& vel) {
}
void ControllablePhysicsComponent::SetIsOnGround(bool val) {
if (val == m_IsOnGround) return;
m_DirtyPosition = true;
m_IsOnGround = val;
}
void ControllablePhysicsComponent::SetIsOnRail(bool val) {
if (val == m_IsOnRail) return;
m_DirtyPosition = true;
m_IsOnRail = val;
}

View File

@ -80,12 +80,14 @@ void MovementAIComponent::Update(const float deltaTime) {
if (m_PullingToPoint) {
const auto source = GetCurrentWaypoint();
// Just a guess at the speed...
const auto speed = deltaTime * 2.5f;
NiPoint3 velocity = (m_PullPoint - source) * speed;
SetPosition(source + velocity);
// We are close enough to the pulled to point, stop pulling
if (Vector3::DistanceSquared(m_Parent->GetPosition(), m_PullPoint) < std::pow(2, 2)) {
m_PullingToPoint = false;
}
@ -114,7 +116,10 @@ void MovementAIComponent::Update(const float deltaTime) {
NiPoint3 velocity = NiPoint3::ZERO;
if (m_Acceleration > 0 && m_BaseSpeed > 0 && AdvanceWaypointIndex()) // Do we have another waypoint to seek?
// If we have no acceleration, then we have no max speed.
// If we have no base speed, then we cannot scale the speed by it.
// Do we have another waypoint to seek?
if (m_Acceleration > 0 && m_BaseSpeed > 0 && AdvanceWaypointIndex())
{
m_NextWaypoint = GetCurrentWaypoint();
@ -132,7 +137,7 @@ void MovementAIComponent::Update(const float deltaTime) {
m_CurrentSpeed = m_MaxSpeed;
}
const auto speed = m_CurrentSpeed * m_BaseSpeed; // scale speed based on base speed
const auto speed = m_CurrentSpeed * m_BaseSpeed; // scale speed based on base speed * current speed
const auto delta = m_NextWaypoint - source;
@ -149,6 +154,7 @@ void MovementAIComponent::Update(const float deltaTime) {
SetRotation(NiQuaternion::LookAt(source, m_NextWaypoint));
} else {
// Check if there are more waypoints in the queue, if so set our next destination to the next waypoint
// All checks for how to progress when you arrive at a waypoint will be handled in this else block.
HandleWaypointArrived();
if (!AdvancePathWaypointIndex()) {
if (m_Path) {