Add support for editing WheeledVehicle wheels config at runtime without full physics state rebuild

#1324
This commit is contained in:
Wojtek Figat
2023-09-01 12:40:51 +02:00
parent a8cc4d7fcb
commit b0ec8525aa
4 changed files with 77 additions and 0 deletions

View File

@@ -40,6 +40,31 @@ const Array<WheeledVehicle::Wheel>& WheeledVehicle::GetWheels() const
void WheeledVehicle::SetWheels(const Array<Wheel>& value)
{
#if WITH_VEHICLE
// Don't recreate whole vehicle when some wheel properties are only changed (eg. suspension)
if (_actor && _vehicle && _wheels.Count() == value.Count() && _wheelsData.Count() == value.Count())
{
bool softUpdate = true;
for (int32 wheelIndex = 0; wheelIndex < value.Count(); wheelIndex++)
{
auto& oldWheel = _wheels.Get()[wheelIndex];
auto& newWheel = value.Get()[wheelIndex];
if (oldWheel.Type != newWheel.Type ||
Math::NotNearEqual(oldWheel.SuspensionForceOffset, newWheel.SuspensionForceOffset) ||
oldWheel.Collider != newWheel.Collider)
{
softUpdate = false;
break;
}
}
if (softUpdate)
{
_wheels = value;
PhysicsBackend::UpdateVehicleWheels(this);
return;
}
}
#endif
_wheels = value;
Setup();
}