x


Closest point on mesh / collider

Hi everyone!

I'm stumbling across a small problem, here's hoping someone can help. Basically what I need is the nearest point to an object.

For now, I've been trying to use Collider.ClosestPointOnBounds. It looks perfect! Right? Unfortunately, the results I'm getting from this method are axis-aligned. Whenever the object is rotated (which is just about always), the method returns an invalid point which isn't actually on the surface. So I'll have to use another approach...

Also, it doesn't matter to me whether the nearest point is on the collider or the mesh. I just need a way to know which point on the surface of an object is nearest to another point.

Any help is really appreciated!

more ▼

asked Nov 16 '09 at 02:19 PM

pgbrandao gravatar image

pgbrandao
58 1 1 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Here's a method which will give you the nearest vertex of the gameObject's mesh. I've written this code to be suitable for putting into a MonoBehaviour script, because it uses references to the gameObject's "transform" and "MeshFilter" components.

An alternative would be to make a static "helper tool" version of this function which could accept both a point and a GameObject parameter.

public Vector3 NearestVertexTo(Vector3 point)
{
    // convert point to local space
    point = transform.InverseTransformPoint(point);

    Mesh mesh = GetComponent<MeshFilter>().mesh;
    float minDistanceSqr = Mathf.Infinity;
    Vector3 nearestVertex = Vector3.zero;

    // scan all vertices to find nearest
    foreach (Vector3 vertex in mesh.vertices)
    {
        Vector3 diff = point-vertex;
        float distSqr = diff.sqrMagnitude;

        if (distSqr < minDistanceSqr)
        {
            minDistanceSqr = distSqr;
            nearestVertex = vertex;
        }
    }

    // convert nearest vertex back to world space
    return transform.TransformPoint(nearestVertex);

}

If you really need to find the nearest point within the surface of the nearest triangle of your mesh, this code could serve as a good starting point.

more ▼

answered Nov 16 '09 at 09:42 PM

duck gravatar image

duck ♦♦
41k 92 148 415

That's an interesting idea, Duck. The problem is that my meshes are rather large, thus this would be rather inefficient. I've already used a different approach, but hopefully your snippet will come in useful to someone else going through the same problem. :-)

Nov 17 '09 at 01:50 PM pgbrandao

Don't underestimate how fast unity's code executes (unless you're targeting the iPhone!). There are ways this could be optimised too. You could spread the process out over a number of frames, only processing a handful of vertices per frame. Alternatively, if the point being tested is likely to move along a path at a reasonable speed, you could make use of the fact that result each frame will most likely either be the same vertex as found last frame, or a neighbour of the vertex found last frame, by using a lookup table of each vertex's neigbours.

Nov 17 '09 at 03:54 PM duck ♦♦

I'm really curious about what your solution was. Right now, Duck's solution is my only option, and like you I have a large mesh, so the efficiency thing does concern me. NOTE: I do have one way that I have slimmed down the searching process. What I do is, based on the world coordinate of the point that I am comparing with the mesh, I can slim down the vertex search to a specific series of indices in the vertex array. That cuts down the search from an average of 20k verts to 2k.

Dec 08 '11 at 12:55 AM Kilometers
(comments are locked)
10|3000 characters needed characters left

Don't know if you're still interested in this, but I've posted some code for this on the forum:-

http://forum.unity3d.com/viewtopic.php?t=36772

It's a bit like the code Duck suggested, but it also implements the nearest point on triangle part and uses a spatial data structure to store the vertices so as to reduce the overhead of the search.

more ▼

answered Nov 18 '09 at 04:19 PM

andeeee gravatar image

andeeee ♦
1.4k 3 6 18

(comments are locked)
10|3000 characters needed characters left

As a quick workaround, you could try the following in whatever script you're calculating the point from: -Un-rotate the object with the collider so it's rotation = Quaternion.identity. Keep the previous rotation in a temp. variable. -Use the Collider.ClosestPointOnBounds method to calculate the Point -Rotate the object back to it's original rotation. -Rotate the point along the object's position with the same rotation you applied to the object.

This should, in theory, result in a point that's on the object/collider's surface even when it's rotated.

more ▼

answered Nov 16 '09 at 07:25 PM

VoxelBoy gravatar image

VoxelBoy
133 5 5 9

False. Apart from the fact, that the operations are listed in a wrong order, this would only work if the object/collider was a right cuboid (a rectangular box), because only then the bounds would align with object's/collider's faces.

Nov 17 '09 at 08:52 AM robert ♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x1880

asked: Nov 16 '09 at 02:19 PM

Seen: 6054 times

Last Updated: Dec 08 '11 at 12:55 AM