diff --git a/Source/Engine/Physics/PhysX/PhysicsBackendPhysX.cpp b/Source/Engine/Physics/PhysX/PhysicsBackendPhysX.cpp index e4335e318..5c5d107e8 100644 --- a/Source/Engine/Physics/PhysX/PhysicsBackendPhysX.cpp +++ b/Source/Engine/Physics/PhysX/PhysicsBackendPhysX.cpp @@ -3447,39 +3447,13 @@ PxVehicleAntiRollBarData CreatePxPxVehicleAntiRollBarData(const WheeledVehicle:: return antiRollBar; } -bool SortWheelsFrontToBack(WheeledVehicle::Wheel const& a, WheeledVehicle::Wheel const& b) +bool SortWheelsFrontToBack(WheeledVehicle::Wheel* const& a, WheeledVehicle::Wheel* const& b) { - return a.Collider && b.Collider && a.Collider->GetLocalPosition().Z > b.Collider->GetLocalPosition().Z; + return a->Collider && b->Collider && a->Collider->GetLocalPosition().Z > b->Collider->GetLocalPosition().Z; } void* PhysicsBackend::CreateVehicle(WheeledVehicle* actor) { - // Physx vehicles needs to have all wheels sorted to apply controls correctly. - // Its needs to know what wheels are on front to turn wheel to correctly side - // and needs to know wheel side to apply throttle to correctly direction for each track when using tanks. - // Anti roll bars needs to have all wheels sorted to get correctly wheel index too. - if (actor->_driveType != WheeledVehicle::DriveTypes::NoDrive) - { - Sorting::QuickSort(actor->_wheels.Get(), actor->_wheels.Count(), SortWheelsFrontToBack); - - // Sort wheels by side - if (actor->_driveType == WheeledVehicle::DriveTypes::Tank) - { - for (int32 i = 0; i < actor->_wheels.Count() - 1; i += 2) - { - auto a = actor->_wheels[i]; - auto b = actor->_wheels[i + 1]; - if (!a.Collider || !b.Collider) - continue; - if (a.Collider->GetLocalPosition().X > b.Collider->GetLocalPosition().X) - { - actor->_wheels[i] = b; - actor->_wheels[i + 1] = a; - } - } - } - } - // Get wheels Array> wheels; for (auto& wheel : actor->_wheels) @@ -3510,6 +3484,33 @@ void* PhysicsBackend::CreateVehicle(WheeledVehicle* actor) // No woman, no cry return nullptr; } + + // Physx vehicles needs to have all wheels sorted to apply controls correctly. + // Its needs to know what wheels are on front to turn wheel to correctly side + // and needs to know wheel side to apply throttle to correctly direction for each track when using tanks. + // Anti roll bars needs to have all wheels sorted to get correctly wheel index too. + if (actor->_driveType != WheeledVehicle::DriveTypes::NoDrive) + { + Sorting::QuickSort(wheels.Get(), wheels.Count(), SortWheelsFrontToBack); + + // Sort wheels by side + if (actor->_driveType == WheeledVehicle::DriveTypes::Tank) + { + for (int32 i = 0; i < wheels.Count() - 1; i += 2) + { + auto a = wheels[i]; + auto b = wheels[i + 1]; + if (!a->Collider || !b->Collider) + continue; + if (a->Collider->GetLocalPosition().X > b->Collider->GetLocalPosition().X) + { + wheels[i] = b; + wheels[i + 1] = a; + } + } + } + } + actor->_wheelsData.Resize(wheels.Count()); auto actorPhysX = (PxRigidDynamic*)actor->GetPhysicsActor();