- Home /
Converting a Plane to a grid
Hi guys,
I've got a large Plane like object which I've imported from Blender which is acting as my worlds 'floor'.
I'd like to programmatically split this into a grid - the grid doesn't have to show to the user but internally I need to know each grid square location and size.
My inital thoughts are to take the Mesh.bounds (or the Renderer.bounds) and divide the x and the z to get an even number of grid squares for the plane.
Is this the best approach?
I understand that doing it this way will most likely leave the last grid square on each row as an odd size so I would need to figure out how to fix that, but otherwise this is the only way I can think of to do this.
Any help much appreciated!
I think the answer to the question of how best to go about it would depend on what the purpose of the grid is and how it will be used.
The grid will be used to highlight squares to show each player which is a valid 'move' when it is their turn. So the whole grid doens't have to be visible always, just the valid grid points during that players turn.
Answer by skovacs1 · Oct 28, 2010 at 04:41 PM
The "best approach" is decided by considering the use case. This is a subjective definition, perhaps too much so.
There are several ways to do grids as indicated in the questions tagged grid. You might start there.
Your approach seems fine. There are really two ways to approach dividing a space into a grid:
- Divide into n spaces.
- Divide into spaces of size n.
Since it's your model and you are looking for an even number of squares, could you not simply ensure that your model evenly divides to avoid odd grid squares on the outside edges?
Creating a class for grid tiles and storing some sort of array of them would be the typical approach to the internal description you've provided. Regardless of which of the above methods you've chosen, the size of your tiles will be known in advance so you shouldn't need to store them with each tile unless you are allowing for arbitrarily sized/shaped tiles which is a different problem entirely.
To get the position of the tiles would involve somehow dividing the space that you are dividing by either the size to get the number of tiles or by the number of tiles to get the size. You could get the size of the space from the bounds or from some raycasting or some other method to your liking like snapping some child empty gameObjects to your corner vertices. The "best" is determined by your use case, but the solution I'd recommend is the one that you have described if your space is axis-aligned. Storing the position might be needless as well as you can easily figure out where the tile is by its position in the array, the stored area size and the stored tile dimensions that are the same for all tiles and if you store your current position, it shouldn't be too hard to figure it out from there. If you are highlighting your tiles with a projector or something, then you would just increment the position with the tile size * the number of tiles moved, etc.
Good point regarding designing the model that way, I will do so to ensure no odd grid squares as you suggest.
Regarding tile positions, the object.renderer.bounds is something I'm currently looking into as a way of getting the centre point of each tile in the array - something I've asked about in another question so I won't repeat that here.
$$anonymous$$any thanks
Bear in $$anonymous$$d that renderer.bounds will give an encapsulating axis-aligned bounding box (that by definition will not rotate with the object, but I believe will scale with the object's scale and rotation) so if your 'plane' is rotated, the bounds will not represent the values that you are looking for. If this is the case, one solution is to align your object, get the bounds.size and then rotate back. Another is to simply place child empty GameObjects snapped to your corners (for a rectangle, you only need 2 (at opposite corners)) and use those as your reference points to get size, position, etc.