- Home /
How to find the center of quad polygons in a mesh
I'm trying to create some sort of grid space for different kinds of ground meshes (just like in the picture). I won't need this grid for movement, it's just for instantiating objects before the game starts. These objects (representing the grid cells) will have their own properties (colliders, info, etc.), and will mostly be used for other objects placement on the grid (by the user).
I need to find a way to get the center of any quad formed by two half-square triangles, and pass that to the instantiate method for the position.
The ground meshes will always be modelled in respect of the unit size (eg., the quad); I just need a way to make the instantiating phase an automatic procedure (mabe done via Editor script), to prepare the game levels.
I've tried different approaches; for example, I came up with (working) code that given a triangle in a mesh, it finds its opposite (meaning, the one that creates a quad with it). The problem is that this code is based on RaycastHit.hitTriangleIndex; now I need a more generic procedure to process the ground mesh and find ALL complementary triangles which create a quad, in an automatic way, withouth raycasting input.
Answer by Erik · Nov 13, 2010 at 03:47 PM
Ok, sorted out. Well, at least for my purposes.
I just cycled through the triangles array, and found the hypotenuse's vertices; then I moved on to the next triangle, to check if it had those vertices in common with the first triangle (meaning, it's a quad). Then I just found the quad center (the hypothenuse's middle point) and stored it into a list for later use.
Now the problem is that this list is not ordered in any way, so I can't do basic checks (for example, finding the adjacent tiles)...