I’m using PhysX to do the raycast, how do I go from PhysX back to Unreal Object/Type. ie I get back the PxraycastHit struct with has pointer to PxActorShape, but from here how do I get the object at the Unreal level ?
Use raycasting functions from UWorld class. They will return info in FHitResult struct containing all informations about the result of the raycast like hit actor.
Sample code:
FHitResult HitResult;
if(GetWorld()->LineTraceSingle(HitResult, Start, End, TraceChannel, FCollisionQueryParams(), FCollisionResponseParams()))
{
//HitResult.GetActor() returns actor that was hit
}
More in-depth example from documentation:
#define COLLISION_WEAPON ECC_GameTraceChannel1
void AMyCharacter::TraceHitForward(APlayerController* InController, FHitResult& OutTraceResult)
{
// Calculate the direction we are 'looking'
FVector CamLoc;
FRotator CamRot;
InController->GetPlayerViewPoint(CamLoc, CamRot);
const FVector TraceDirection = CamRot.Vector();
// Calculate the start location for trace
FVector StartTrace = FVector::ZeroVector;
if (InController)
{
FRotator UnusedRotation;
InController->GetPlayerViewPoint(StartTrace, UnusedRotation);
// Adjust trace so there is nothing blocking the ray between the camera and the pawn, and calculate distance from adjusted start
StartTrace = StartTrace + TraceDirection * ((GetActorLocation() - StartTrace) | TraceDirection);
}
// Distance to trace
const float WeaponRange = 4096.0f;
// Calculate endpoint of trace
const FVector EndTrace = StartTrace + TraceDirection * WeaponRange;
// Setup the trace query
static FName FireTraceIdent = FName(TEXT("WeaponTrace"));
FCollisionQueryParams TraceParams(FireTraceIdent, true, this);
TraceParams.bTraceAsyncScene = true;
// Perform the trace
GetWorld()->LineTraceSingleByChannel(OutTraceResult, StartTrace, EndTrace, COLLISION_WEAPON, TraceParams);
}
I haven’t tested this, but I would expect that using the UWorld raycast functions would be safe. The reason for this is that we have an async API available which eventually goes through the same code, and so this should already be thread safe on the UE side. I know for a fact it’s safe on the PhysX side. Can you give this a try?
In general trying to convert from physx back to UE yourself is not a great idea because we do change this from time to time.
@ - I’ll give it try but logically I don’t see how it can possibly work correctly, although I don’t know the internals of the engine. For example, say I’m going to get 3 sub steps, so in the first I do a raycast and get the results. Apply various forces ect, etc. PhysX simulates this sub time step and updates all physics, ie positions, rotations, etc, etc. But at this point I gather Unreal has not been updated from PhysX as this will only happen at the end of update/tick. Therefore in the next sub time step the PhysX raycast would be correct as the simulation has moved on but the Unreal objects have yet to be updated with new physics properties from the simulation. Does this make sense ?
Any Unreal Engine Developers confirm this ?
Update
The Engine does indeed work as I thought above, but @ yes you are correct in that the UWorld raycast works. All I have to rememeber is to use the PhysX transform and not the Unreal transform for anything in a sub time step. Now this makes sense.