How to know when a world position is inside an Actor?

I am trying to check whether a particular world position is inside an Actor having any random static mesh or not. I have no idea how to approach this problem. I searched around but couldn’t find much except GetActorBounds() but that gives a box. I want to know if it is inside the mesh or not.

Thanks in advance!

Finding if it is in a complex mesh (like a character for example) is not something easily done. Most of the time, collision geometry is a convex hull of the visual representation of the object, to facilitate the collision detection.

The best way for you to detect that you need would be to add the most “complex” collision geometry out of the actor you want to detect if you are in or not and register a function to the OnComponentBeginOverlap delegate of that component.

Hope this sets you in the correct direction!

So if I understand you right, I should register OnComponentBeginOverlap delegate on the most complex geometry and then detect when it overlaps? My question is a bit different. What if I don’t have anything to collide with? I just want to know if an actor contains that world point or not.

I was surprised myself by the fact that there appears to be no simple point inside/outside geometry test in UE4, it seems like such basic functionality to me. So far I’ve just used dodgy workarounds (like messing around with ray casts).

By chance, yesterday I noticed the method AVolume::EncompassesPoint, which makes use of UPrimitiveComponent::GetDistanceToCollision to seemingly do what you are after. It looks to me like you can call the latter method on your collision component, and if the result is 0 that means the point is inside.

Note that I haven’t had time to test this yet, I’m just making assumptions based on a look at the code. It’s possible it may only be implemented for basic collision shapes. If it does work, please report back.

You could use OverlapComponent with a very small sphere:

 *  Test the collision of the supplied Sphere at the supplied location, and determine if it overlaps this component

virtual bool OverlapComponent(const FVector& Pos, const FQuat& Rot, const FCollisionShape& CollisionShape);

Thanks! I’ll try it out and see if it works and update here.

UPDATE: Works exactly as you said it! Thank you!

Just to aware anyone using this method: GetDistanceToCollision doesn’t return correct point for welded geometry (tested on 4.22). Point and distance will be calculated only for parent body.

1 Like

Hi Anyone happens to know if there is a way to achieve this using blueprints?
Thanks!

There’s a node called get closest point on collision. You need convex geometry though.
link text