I am writing a placement module where you can edit certain settings such as grid size, interpolation and others to make a custom placement system similar to another one I saw. It is supposed to be very simple to use, but there is one problem. The models sometimes don’t snap to a grid. The models don’t snap when:
The model is a certain size (example 1x1x1, 2x2x1)
The model is rotated while placing by using the rotate system.
The grid size is set to anything but 2 studs.
The plot is not a multiple of the grid size
Other times, the model snaps fine. Such as when all these things are in play at one time:
The model is un-rotated
The model’s primary part is the “correct” size (such as 4x4x4)
The grid size is set to 2
The plot is a multiple of the grid size
I am using the simple formula for snapping, but I find it does not work with most situations.
local function CalcGrid1(pos, grid)
return math.floor((pos / grid) + 0.5) * grid
end
I tried using the models size to fix this, but it does not work when the model is rotated or grid size is changed.
local function CalcGrid2(x, grid, primary)
posX = math.floor((x / grid) + 0.5) * grid + (primary.Size.X / grid)
-- same for z
end
Here are some photos of the correct and incorrect snapping (using the second method).
My only guess would be that you don’t take the model rotation into account, or maybe because you apply the rotation wrongly (same for size), but I might me wrong.
I suggest you to look at the source of this module and check how tunicus did it.
Maybe there you can find a way on how to calculate the proper position with rotation.
Are these grids only on the XZ plane or are you looking at supporting other planes as well? You have quite a large desired feature set but if done properly each feature can be developed independently.
A 1x2 blocks center point is .5,1 so that is point of rotation.
I would recommend tracking a corner and using it to match the grid points.
So you have an item.
Find upper left corner offset (top of view being -z dir)
xAxisSize/2 , 0, zAxisSize/2
Move to a corner
setCFrame to grid point + offset
Rotating 90degrees makes previous X your Z and Z, X
zAxisSize/2, 0, xAxisSize/2
If you want this and want to allow freedom of placement you will need to add the ability to change which corner is being used to snap.
So that you choose which corner of an object to connect to which corner of the grid. regardless of rotation.
Thanks! I have been able to fix the problem with “incorrect” model sizes using your reply. Now I won’t get people saying “Why is the module not working?” (I have a module that handles placement using the incorrect grid system).