RTS Grid Issue

Currently I have got the grid of grid squares being generated correctly as you can see from the image below. The grid is made up of a series of planes.

The way it currently works is if I click on a grid square I will instantiate a cube and snap it to the center of the grid square. This works perfectly fine when the cube is the same size as a grid square as you can see below.

The problem occurs when trying to instantiate objects that are bigger than one grid square. For example if i instantiate a 3x3 cube at one of the edge grid squares it will overlap and part of it will be off the edge of the grid. You can see this problem below:

I’m currently stumped as to how I could solve this using the grid the way I have it set up at currently. Any ideas would be greatly appreciated.

Instead of using the centre of the grid to position the cube you need to make it more dynamic so it’ll handle any size of cube.

For example, take the top-left hand point of the selected grid point and then add half the cube’s width to the x position and half the cube’s height to the z position. That way it’ll be central if the cube is 1x1 and aligned properly even if the cube is 3x3.

So do I always take the top left and work from that or do I change which corner I’m using as a starting point depending on where the grid cell is?

For some cells on the left it is working with:

grid cell top left position x + half the cube size
grid cell top left position z - half the cube size

However for other cells it seems to either fit before the calculation is finished or it is slightly off afterwards.

I think it depends on how you want it to act, each of your cubes would have to have a width and height that’s a multiple of your grid size. If you place a cube at the bottom right of the grid you might want to line it up differently etc…

Okay so there’s no one calculation that will fix the entire grid, it’s gonna require multiple calculations after detecting exactly where the cell is?

Your behavior is too ambiguous. The first step is determining whether or not the cube can actually fit on the grid at the given cell. The second part of that is determining how it should try to fit. For example, if the cube is 2 cells wide and I click on the first column of cells should it use the “left” part of the cube or the “right” part. Having a center for cubes with an odd number of cells as their height and width is easy, it gets trickier beyond that.

You’re asking for a silver bullet solution when the better path is to break the problem down into smaller parts and solve them one at a time.