- Home /
Boat physics
Hi.
I'm trying to make a boat physics script.. To do so, I have to find the nearest vertex on a mesh.
So how can I find the nearest vertex on a mesh?
For exampel:
Vector3 point = (1,33,7); // The Vector3 from where it should find the nearest vertex on the mesh "mesh".
MeshFilter mesh = OceanGameObject.GetComponent(); // The mesh
Vector3 nearestVertex = The nearest vertex on the mesh "mesh" from "point"
How would this be possible?
Thanks in advance,
Andreas.
Simpply just the nearest vertex on the mesh in Vector3 world space.
Answer by Benproductions1 · Apr 10, 2014 at 07:30 AM
Perform a linear search through all the vertices in your mesh, finding the vertex with the least distance to your point in local space. To transform the point from world space to local space, use Transform.InverseTransformPoint
.
To speed it up you could use some sort of tree structure instead of a linear search and find the nearest vertex that way, however that might get a little complicated ;)
Thanks for your answer. But how exactly do I perform a linear seach? I couldn't find any documentation? Or if possible, could you share some sample code? :)
Thanks in advance.
Andreas.
You couldn't do a google search on the term "linear search"? Are you serious?
Answer by thomasindustry · Apr 09, 2014 at 06:57 PM
the vertices property on a mesh will give you the local positions of all the vertices of the mesh. You can then use the Transform.TransformPoint method to convert these positions to world space for comparisons with your reference point.
Okay, so how do I actually find the nearest mesh vertex?
It would be more efficient to transform the point to local space once, rather than the other way around for every vertex.
Your answer
Follow this Question
Related Questions
how to increase thickness of points? 1 Answer
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
Vertex colours not blending across triangle face 1 Answer
Rendering Mesh Points on Mobile 0 Answers
Spherical Terraing LOD 0 Answers