Is there any way to get info from a component after a hit occurs, but before that hit affects its physics?

I need to get certain data (mainly velocity) from a physics BodyInstance when a physics impact occurs.

However, the data packaged into the FComponentHitSignature and sent along with the Component Hit event appears to reflect the object just after the hit has been resolved, rather than before.

So, for example, if I drop the component on the ground, the velocity reflects the state of the object after it “bounces.” (Velocity points up instead of down.) If two objects slam into each other, both the velocities end up pointing in the same direction if they both end up moving in the same direction after the collision.

Specifically, I’m trying to get the velocity of the object at the location of the impact (using FBodyInstance::GetUnrealWorldVelocityAtPoint) so weird shortcuts like caching the object’s velocity the previous frame and so on won’t work, since I need to know where the hit occurred to get the data I need. This stuff all seems to disappear down into PhysX where I can’t see or edit it.

Anyone have any ideas? This seems like it shouldn’t be that unusual a use case, but I haven’t been able to find anything which does this.

Thanks.

Hey -

One thing you can try would be to create a collision component that is only slightly larger than object itself. Rather than at the time of the hit event (or just after), this would allow you to get the velocity when the collision component is overlapped instead, which should be an instant before the hit actually occurs.

Cheers

Do overlap events all get resolved earlier in the frame than collisions? Or is that just relying on the overlap occurring at least one frame before the collision? (Which I can’t guarantee.)

Anyways, it took a few hours, but I figured out the math to find the linear velocity at an arbitrary point given the CoM, angular velocity, and linear velocity of a physics body instance. That solves my specific case, since now I can just cache last frame’s linear and angular velocity, and apply them to this frame’s hit location and CoM. I’ll share the code once it’s actual code and not paper notes and a blueprint test component.

The math:

//* Calculate the angular velocity of a rigid body at a given point for a provided CoM and Angular + Linear velocity */
FVector UTools::GetVelocityAtPointFromRigidBodyData(FVector LocationToCheck, FVector AngularVelocity, FVector LinearVelocity, FVector CenterOfMass)
{
	FVector DistanceFromCoM = LocationToCheck - CenterOfMass;
	float X = (FMath::DegreesToRadians(AngularVelocity.Y)*(DistanceFromCoM.Z)) - (FMath::DegreesToRadians(AngularVelocity.Z)*(DistanceFromCoM.Y));
	float Y = (FMath::DegreesToRadians(AngularVelocity.Z)*(DistanceFromCoM.X)) - (FMath::DegreesToRadians(AngularVelocity.X)*(DistanceFromCoM.Z));
	float Z = (FMath::DegreesToRadians(AngularVelocity.X)*(DistanceFromCoM.Y)) - (FMath::DegreesToRadians(AngularVelocity.Y)*(DistanceFromCoM.X));

	return FVector(X, Y, Z) + LinearVelocity;
}

There’s also the PxContactModifyCallback in PhysX you can use to find out about collisions before they are resolved. It’s not exposed to Unreal by default but the changes needed to the engine to do this are pretty small.

Even if you don’t want to modify the colllision response, this happens pre-solving, so contains the information you’re after. Might be of use at some point. NB: happens on the physics thread and is performance sensitive so be restrained with what you do in the callback; I’d suggest just caching off some values similar to your own solution above.