Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWM-17410 Optimize movement component replicated data #61

Merged
merged 5 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
AWM-17410 Optimize movement component replicated data
  • Loading branch information
IlinAleksey committed Aug 22, 2018
commit 193fb6a9c49f8ce266b830d45906c0ec36b0cc74
34 changes: 28 additions & 6 deletions Source/PsRealVehiclePlugin/Classes/PrvVehicleMovementComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,6 @@ class PSREALVEHICLEPLUGIN_API UPrvVehicleMovementComponent : public UPawnMovemen
float MaxEngineRPM;

/** Engine RPM */
UPROPERTY(Replicated)
float EngineRPM;

float EngineTorque;
Expand All @@ -850,7 +849,6 @@ class PSREALVEHICLEPLUGIN_API UPrvVehicleMovementComponent : public UPawnMovemen
float TargetSteeringAngularSpeed;

/** Used for wheels animation */
UPROPERTY(Replicated)
float EffectiveSteeringAngularSpeed;

/** Vector computed from EffectiveSteeringAngularSpeed */
Expand Down Expand Up @@ -891,12 +889,10 @@ class PSREALVEHICLEPLUGIN_API UPrvVehicleMovementComponent : public UPawnMovemen
bool bAutoBrakeSteering;

public:
/** Replicated velocity for tracks animation [left] */
UPROPERTY(Replicated)
/** Velocity for tracks animation [left] */
float LeftTrackEffectiveAngularSpeed;

/** Replicated velocity for tracks animation [right] */
UPROPERTY(Replicated)
/** Velocity for tracks animation [right] */
float RightTrackEffectiveAngularSpeed;


Expand Down Expand Up @@ -1205,6 +1201,32 @@ class PSREALVEHICLEPLUGIN_API UPrvVehicleMovementComponent : public UPawnMovemen
/** Keep real value of throttle while steering stabilizer is active */
UPROPERTY(Transient)
float RawThrottleInputKeep;

/** Optimized replicated data */
protected:

void PrepareOptimizedRepData();

/** Engine RPM */
UPROPERTY(Transient, ReplicatedUsing = OnRep_RepEngineRPM)
uint8 RepEngineRPM;

/** Replicated velocity for tracks animation [left] */
UPROPERTY(Transient, ReplicatedUsing = OnRep_RepLeftTrackEffectiveAngularSpeed)
int8 RepLeftTrackEffectiveAngularSpeed;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще, я бы запихнул это в структурку все три данных, и в одном онрепе это обрабатывал. Нет смысла делать три онрепа, которые по сути конвертят значения, которые постоянно меняются. Мол, FCosmeticRepData , что-то такое


/** Replicated velocity for tracks animation [right] */
UPROPERTY(Transient, ReplicatedUsing = OnRep_RepRightTrackEffectiveAngularSpeed)
int8 RepRightTrackEffectiveAngularSpeed;

UFUNCTION()
void OnRep_RepEngineRPM();

UFUNCTION()
void OnRep_RepLeftTrackEffectiveAngularSpeed();

UFUNCTION()
void OnRep_RepRightTrackEffectiveAngularSpeed();
};

//////////////////////////////////////////////////////////////////////////
Expand Down
43 changes: 35 additions & 8 deletions Source/PsRealVehiclePlugin/Private/PrvVehicleMovementComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ UPrvVehicleMovementComponent::UPrvVehicleMovementComponent(const FObjectInitiali

LastAntiRolloverValue = 0.f;
bUseMeshRotationForEffect = true;

RepEngineRPM = 0;
RepLeftTrackEffectiveAngularSpeed = 0;
RepRightTrackEffectiveAngularSpeed = 0;
}


Expand Down Expand Up @@ -321,6 +325,8 @@ void UPrvVehicleMovementComponent::TickComponent(float DeltaTime, enum ELevelTic
{
UpdateAntiRollover(DeltaTime);
}

PrepareOptimizedRepData();
}
else
{
Expand Down Expand Up @@ -2610,6 +2616,29 @@ bool UPrvVehicleMovementComponent::GetCameraVector(FVector& RelativeCameraVector
//////////////////////////////////////////////////////////////////////////
// Replication

void UPrvVehicleMovementComponent::PrepareOptimizedRepData()
{
RepEngineRPM = static_cast<uint8>((FMath::Min(EngineRPM, MaxEngineRPM) / MaxEngineRPM) * 255.f);
RepLeftTrackEffectiveAngularSpeed = static_cast<int8>(FMath::Clamp(FMath::RoundHalfFromZero(LeftTrackEffectiveAngularSpeed), -127.f, 127.f));
Copy link
Contributor

@Antonrr Antonrr Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Точно ли TrackEffectiveAngularSpeed влезет в int8?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пока гонял не видел чтобы превышало 50 и -20

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

50 — это уже половина до допустимого предела. Думаю, здесь лучше тоже передавать с уменьшенной точностью значение, чтобы защититься от overflow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IlinAleksey , проконсультируйся с Игорем, как он скажет. Вообще по ощущениям uint8 хватит, просто клэпмить.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Antonrr так ведь идёт клэмп [-127, 127]. overflow не будет, можем потерять значения больше 127 и меньше -127
@ufna спросил, на практике не замечал что доходило до 100. Если и будет доходить и переходить, то не страшно что мы обрежем до 127.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IlinAleksey , тогда гуд

RepRightTrackEffectiveAngularSpeed = static_cast<int8>(FMath::Clamp(FMath::RoundHalfFromZero(RightTrackEffectiveAngularSpeed), -127.f, 127.f));
}


void UPrvVehicleMovementComponent::OnRep_RepEngineRPM()
{
EngineRPM = static_cast<float>(RepEngineRPM) / 255.f * MaxEngineRPM;
}

void UPrvVehicleMovementComponent::OnRep_RepLeftTrackEffectiveAngularSpeed()
{
LeftTrackEffectiveAngularSpeed = static_cast<float>(RepLeftTrackEffectiveAngularSpeed);
}

void UPrvVehicleMovementComponent::OnRep_RepRightTrackEffectiveAngularSpeed()
{
RightTrackEffectiveAngularSpeed = static_cast<float>(RepRightTrackEffectiveAngularSpeed);
}

void UPrvVehicleMovementComponent::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
Expand All @@ -2619,18 +2648,16 @@ void UPrvVehicleMovementComponent::GetLifetimeReplicatedProps(TArray< FLifetimeP

if (bFakeAutonomousProxy)
{
DOREPLIFETIME(UPrvVehicleMovementComponent, EngineRPM);
DOREPLIFETIME(UPrvVehicleMovementComponent, EffectiveSteeringAngularSpeed);
DOREPLIFETIME(UPrvVehicleMovementComponent, RepEngineRPM);

DOREPLIFETIME(UPrvVehicleMovementComponent, LeftTrackEffectiveAngularSpeed);
DOREPLIFETIME(UPrvVehicleMovementComponent, RightTrackEffectiveAngularSpeed);
DOREPLIFETIME(UPrvVehicleMovementComponent, RepLeftTrackEffectiveAngularSpeed);
DOREPLIFETIME(UPrvVehicleMovementComponent, RepRightTrackEffectiveAngularSpeed);
}
else
{
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, EngineRPM, COND_SimulatedOnly);
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, EffectiveSteeringAngularSpeed, COND_SimulatedOnly);
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, RepEngineRPM, COND_SimulatedOnly);

DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, LeftTrackEffectiveAngularSpeed, COND_SimulatedOnly);
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, RightTrackEffectiveAngularSpeed, COND_SimulatedOnly);
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, RepLeftTrackEffectiveAngularSpeed, COND_SimulatedOnly);
DOREPLIFETIME_CONDITION(UPrvVehicleMovementComponent, RepRightTrackEffectiveAngularSpeed, COND_SimulatedOnly);
}
}