2021-12-05 17:54:36 +00:00
|
|
|
#include "VehiclePhysicsComponent.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
VehiclePhysicsComponent::VehiclePhysicsComponent(Entity* parent) : Component(parent) {
|
2021-12-05 17:54:36 +00:00
|
|
|
m_Position = NiPoint3::ZERO;
|
|
|
|
m_Rotation = NiQuaternion::IDENTITY;
|
|
|
|
m_Velocity = NiPoint3::ZERO;
|
|
|
|
m_AngularVelocity = NiPoint3::ZERO;
|
|
|
|
m_IsOnGround = true;
|
|
|
|
m_IsOnRail = false;
|
|
|
|
m_DirtyPosition = true;
|
|
|
|
m_DirtyVelocity = true;
|
|
|
|
m_DirtyAngularVelocity = true;
|
2023-04-18 06:40:20 +00:00
|
|
|
m_EndBehavior = GeneralUtils::GenerateRandomNumber<uint32_t>(0, 7);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
VehiclePhysicsComponent::~VehiclePhysicsComponent() {
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetPosition(const NiPoint3& pos) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (pos == m_Position) return;
|
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Position = pos;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetRotation(const NiQuaternion& rot) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (rot == m_Rotation) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Rotation = rot;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetVelocity(const NiPoint3& vel) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (vel == m_Velocity) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Velocity = vel;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetAngularVelocity(const NiPoint3& vel) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (vel == m_AngularVelocity) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_AngularVelocity = vel;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetIsOnGround(bool val) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (val == m_IsOnGround) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_IsOnGround = val;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetIsOnRail(bool val) {
|
2023-06-20 14:19:21 +00:00
|
|
|
if (val == m_IsOnRail) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
m_DirtyPosition = true;
|
2022-07-28 13:39:57 +00:00
|
|
|
m_IsOnRail = val;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 14:19:21 +00:00
|
|
|
void VehiclePhysicsComponent::SetRemoteInputInfo(const RemoteInputInfo& remoteInputInfo) {
|
|
|
|
if (m_RemoteInputInfo == remoteInputInfo) return;
|
|
|
|
this->m_RemoteInputInfo = remoteInputInfo;
|
|
|
|
m_DirtyRemoteInput = true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetDirtyPosition(bool val) {
|
|
|
|
m_DirtyPosition = val;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetDirtyVelocity(bool val) {
|
|
|
|
m_DirtyVelocity = val;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::SetDirtyAngularVelocity(bool val) {
|
|
|
|
m_DirtyAngularVelocity = val;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 21:33:15 +00:00
|
|
|
void VehiclePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) {
|
2022-07-28 13:39:57 +00:00
|
|
|
outBitStream->Write(bIsInitialUpdate || m_DirtyPosition);
|
|
|
|
|
|
|
|
if (bIsInitialUpdate || m_DirtyPosition) {
|
2023-06-20 14:19:21 +00:00
|
|
|
m_DirtyPosition = false;
|
|
|
|
outBitStream->Write(m_Position.x);
|
|
|
|
outBitStream->Write(m_Position.y);
|
|
|
|
outBitStream->Write(m_Position.z);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write(m_Rotation.x);
|
|
|
|
outBitStream->Write(m_Rotation.y);
|
|
|
|
outBitStream->Write(m_Rotation.z);
|
|
|
|
outBitStream->Write(m_Rotation.w);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
outBitStream->Write(m_IsOnGround);
|
|
|
|
outBitStream->Write(m_IsOnRail);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-08-20 06:17:12 +00:00
|
|
|
bool isVelocityZero = m_Velocity == NiPoint3::ZERO;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-08-20 06:17:12 +00:00
|
|
|
outBitStream->Write(isVelocityZero);
|
|
|
|
if (isVelocityZero) {
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write(m_Velocity.x);
|
|
|
|
outBitStream->Write(m_Velocity.y);
|
|
|
|
outBitStream->Write(m_Velocity.z);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-08-20 06:17:12 +00:00
|
|
|
bool isAngularVelocityZero = m_AngularVelocity == NiPoint3::ZERO;
|
|
|
|
outBitStream->Write(isAngularVelocityZero);
|
|
|
|
if (isAngularVelocityZero) {
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write(m_AngularVelocity.x);
|
|
|
|
outBitStream->Write(m_AngularVelocity.y);
|
|
|
|
outBitStream->Write(m_AngularVelocity.z);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write0(); // local_space_info. TODO: Implement this
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write(m_DirtyRemoteInput || bIsInitialUpdate); // remote_input_info
|
|
|
|
if (m_DirtyRemoteInput || bIsInitialUpdate) {
|
|
|
|
outBitStream->Write(m_RemoteInputInfo.m_RemoteInputX);
|
|
|
|
outBitStream->Write(m_RemoteInputInfo.m_RemoteInputY);
|
|
|
|
outBitStream->Write(m_RemoteInputInfo.m_IsPowersliding);
|
|
|
|
outBitStream->Write(m_RemoteInputInfo.m_IsModified);
|
|
|
|
m_DirtyRemoteInput = false;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write(125.0f); // remote_input_ping TODO: Figure out how this should be calculated as it seems to be constant through the whole race.
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!bIsInitialUpdate) {
|
|
|
|
outBitStream->Write0();
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (bIsInitialUpdate) {
|
2023-04-18 06:40:20 +00:00
|
|
|
outBitStream->Write<uint8_t>(m_EndBehavior);
|
2023-06-20 14:19:21 +00:00
|
|
|
outBitStream->Write1(); // is input locked?
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
outBitStream->Write0();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void VehiclePhysicsComponent::Update(float deltaTime) {
|
|
|
|
if (m_SoftUpdate > 5) {
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(m_Parent);
|
2022-07-28 13:39:57 +00:00
|
|
|
m_SoftUpdate = 0;
|
|
|
|
} else {
|
|
|
|
m_SoftUpdate += deltaTime;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|