Closest collision point

I am trying to find the nearest collision point on any mesh to a given point in space. I.e. say I have a sphere located at (x1,y1,z1) with radius 0, and I would expand its radius until the first collision occurs.

I need to find the closest collision point on a mesh, such that I can snap to it.

How would I go about it? I could expand a sphere until it hits something, but that is highly expensive if objects are located far away and potentially very inaccurate if many objects are locating very close to each other.

Another approach could be to iterate through all meshes, that are eligible, and find the closest collision point on any triangle and pick the closest one, as described in this post nearest point on mesh? - Unity Answers. Although that just does sound immensely expensive if I have an great number of models with a huge number of vertices, although it would prove perfect accuracy.

Any tips and help is highly appreciated!

Depends on the number of game objects you want to check for, if a not that many you could cycle through a list of each object and check the distance.

See Vector3.Distance

This is similar to expanding the collider but if you had 100 objects and the closest was 200 units away then it’d be faster checking which one’s closest than expanding the collider till it hits something.

You can use it to find the closest object and act as if triggered.

EDIT:

Sorry for the misunderstanding and for going out immediately after answering! That link appears to be either wrong or down.

EDIT 2:

OK got it. looks like a trailing full stop nearest-point-on-mesh

Hmmm looks complex might have to think about this.

EDIT 3:

OK how’s this, increase your collider, let’s say 1 unit (1 meter) per frame (store in float IncUnit) and check for collisions. That increase can be anything you like as long as none of your GameObects can slip in between the size you’re increasing.

When the collider is triggered set a bool to say HaveInitialCollison = true;

In Update if that bool is true call a function to do the following.

Store these values:

bool BackUp = true //if still colliding

int AmountToDivideBy 2; // Double this with each step

float AmountToMove = IncUnit / AmountToDivideBy; // to check how far to resize the collider.

If BackUp = True then reduce your collider by half it’s size and check if a collison is still happening.

If no longer colliding set BackUp to false but increase the collider by AmountToMove instead of reducing. Each time do this:

AmountToDivideBy = AmountToDivideBy * 2;

Keep doing this until these are met:

A: there is a collision still happening

B: are happy that the increments are so small you must be at the closest point.

C: there’s only I GameObject being collided with. You’ll need some logic to handle this past your maximum AmountToDivideBy.

At this point do whatever check you want from that other page.

It’s not ideal but It’ll vastly reduce the number of operations being performed on just increasing the collider a fraction from 0 to the first collision.

Basically just jumps forward rapidly then, when collision happens, halves the distance until you are happy that it’s close enough and the only collision happening.