I have a game with high velocities and physics based movement, vehicles travelling 300-800 m/s. When these vehicles collide with the terrain, I have a custom event set up to trigger. I take the Hit.Location passed into this event and then do a sphere trace with radius of at least 50 unreal units. However, sometimes this sphere trace returns nothing.
How can this be? If the game is telling me something impacted at Hit.Location, then I check that location, how can it not be there? Is the event being triggered after the physics engine moves the pawn, therefore sending a HitResult with false location because the pawn has now moved elsewhere? Does anyone know when during the physics cycle the event is actually triggered?
Appreciate any help or ideas. Here is the relavent code:
void ADrifter::OnRaceHit(UPrimitiveComponent* Comp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
float VelocityInHitDirection = FMath::Abs(FVector::DotProduct(Velocity, Hit.Normal));
// DamageMinVelocity = 50.f, ignore low velocity collisions
if (VelocityInHitDirection < DamageMinVelocity)
return;
TArray<FHitResult> HitResultsSphereTrace;
FCollisionQueryParams CollisionQueryParams;
CollisionQueryParams.AddIgnoredComponent(Comp);
CollisionQueryParams.bTraceComplex = true;
CollisionQueryParams.bDebugQuery = true;
FCollisionResponseParams CollisionResponseParams;
FVector Start = Hit.Location;
TArray<TEnumAsByte<EObjectTypeQuery>> ObjectTypes;
TArray<AActor*> IgnoredActors;
TArray<UPrimitiveComponent*> Overlaps;
ObjectTypes.Add(UEngineTypes::ConvertToObjectType(DrifterRoot->GetCollisionObjectType()));
UKismetSystemLibrary::SphereOverlapComponents(GetWorld(), Start, VelocityInHitDirection, ObjectTypes, UPart::StaticClass(), IgnoredActors, Overlaps);
// Returning empty sometimes
if (Overlaps.IsEmpty())
{
DrawDebugSphere(GetWorld(), Start, VelocityInHitDirection, 5, FColor::Red, false, 10.f);
DrawDebugPoint(GetWorld(), Start, 10.f, FColor::Blue, false, 10.f);
Freeze();
}
else
{
DrawDebugSphere(GetWorld(), Start, VelocityInHitDirection, 5, FColor::Green, false, 10.f);
DrawDebugPoint(GetWorld(), Start, 10.f, FColor::Purple, false, 10.f);
}
}