// Copyright 2016 Mookie. All Rights Reserved. #include "EBBullet.h" FVector AEBBullet::UpdateVelocity_Implementation(UWorld* World, FVector Location, FVector PreviousVelocity, float DeltaTime) const { FVector NewVelocity = PreviousVelocity; //airDensity float air; float speedOfSound; air = GetAirDensity(World, Location); speedOfSound = GetSpeedOfSound(World, Location); //gravity if (!OverrideGravity) { NewVelocity += FVector(0, 0, World->GetGravityZ())*DeltaTime; } else { NewVelocity += Gravity*DeltaTime; }; //drag FVector relVel = (NewVelocity - GetWind(World, Location)); float speed = relVel.Size(); // Safety check: Prevent division by zero and clamp mach number to reasonable range float safeMach = (speedOfSound > 0.1f) ? (speed / speedOfSound) : 0.0f; float mach = FMath::Clamp(safeMach, 0.0f, 50.0f); // Clamp to prevent extreme values float profile = FMath::Pow(Diameter / 200.0f, 2.0f)*3.141592f; float drag = GetCurveValue(MachDragCurve, mach, 0.25f)*FMath::Pow(speed / 100.0f, 2.0f)*profile*air*FormFactor*50.0f; // Safety check: Ensure WorldScale is not zero float safeWorldScale = FMath::Max(WorldScale, 0.01f); NewVelocity -= relVel.GetSafeNormal() * drag / Mass * DeltaTime / safeWorldScale; // SAFETY: Prevent infinite or NaN velocities from physics calculations if (!FMath::IsFinite(NewVelocity.X) || !FMath::IsFinite(NewVelocity.Y) || !FMath::IsFinite(NewVelocity.Z) || FMath::IsNaN(NewVelocity.X) || FMath::IsNaN(NewVelocity.Y) || FMath::IsNaN(NewVelocity.Z)) { UE_LOG(LogTemp, Error, TEXT("EBBullet: Invalid velocity detected in UpdateVelocity, using previous velocity")); return PreviousVelocity; } return NewVelocity; }