# compute point coord in a mesh

This topic is 4907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does someone know a way to compute if the coordinates of an arbitrary point (not a vertex)is in a mesh?

##### Share on other sites
Well two methods pop into my head. The best one is to cast a line through that point. I don't say ray, because the starting point is irrelevant. For all practical purposes, you should be able to cast a line with any function, as long as that point lies on the line. I would recommend using an axis alligned line though, so that the equations can be simplified. Next, determine all intersection points on that line with the mesh, keeping track of whether the line is entering or exiting the mesh at each point. It won't intersect directly with vertices in most cases probably, so you will have to interpolate between vertices (and hopefully you aren't using higher order meshes). The last part of the algorithm involves determining where the point lies. It should be between an entry point and an exit point. If you want to simplify a lot of the math, I'm sure you could just cast a ray using the ray picking example in the SDK, just making sure that the ray goes through that point.

The other method is slow and I don't really see any benefits other than perhaps a bit easier to compute. You would just go through all faces in the mesh and determine if they all enclose the point. i.e. if the mesh data contains face normals, you can determine which direction the face is "facing" and if the point lies behind that face in the x,y,z values, it is at the least behind that one face. Then you could check all faces similarly. If all pass, it's within the mesh. If one fails, it is not. Think of a face as a plane. There are D3DX functions that will determine if a point lies in front of, within, or behind a plane.

Drawbacks:
The first method will have issues when the line lies parallel and on top of the mesh (i.e. the mesh wall runs through the line long ways). There would obviously be numerous intersection points...

The second method may be very slow for large meshes.

Sorry if I've left anything out or said something wrong, just kind of threw this together to get the thought process going for you.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015742
×