#Snap to grid building system
1 messages · Page 1 of 1 (latest)
local function to_nearest(number: number, interval: number): number
return math.floor(number / interval) * interval
end
local function to_nearest_vertex(vector3: Vector3): Vector3
return Vector3.new(
to_nearest(vector3.X, GRID_SIZE),
to_nearest(vector3.Y, GRID_SIZE),
to_nearest(vector3.Z, GRID_SIZE)
)
end
When you calculate the user's placement vector, cast it to the nearest vertex before attempting to place/render the object
can i know why do you multiply the number by the interval and then divides it by the interval
Grids are made up of equadistant vertices. This means your placement vector cannot land anywhere but on a point 0 ± interval. If we conceptualize this in the first dimension with a grid size of 5 units, your valid points are -15, -10, -5, 0, 5, 10, 15 etc
0 / 5 is 0. 0 * 5 is still zero.
1 / 5 floored is 0. 0 * 5 is still zero.
This continues onto 5 / 5.
5 / 5 is 1. 1 * 5 is five.
6 / 5 floored is 1. 1 * 5 is still five.
This continues onto 10 / 5.
10 / 5 is 2. 2 * 5 is ten.
11 / 5 floored is 2. 2 * 5 is still ten.
The grid must work in perfect intervals of GRID_SIZE. Floor-dividing a number by this interval tells us how many perfect vertices we can travel in that distance. We use this perfect vertex count to calculate the correct position at that distance from the origin
A given distance of 11.5 with a GRID_SIZE of 5 gives us 2 perfect vertices of 5 (5 fits into 11.5 2 times). This means we can travel 2 GRID_SIZEs from the origin, giving us a position of 10
Apologies, I had accidentally written the equation backwards!
LOL i was acctually reading everything and trying to understand why you were multiplying than dividing the number floored 😭
But it was not sarcarsm even if u had written it right i would still not know how to make a grid system
That was a genuine question
Did my explanation help?
I have a system on my GitHub that demonstrates the core logic behind a placement system:
This system is fully client-sided. It expects the client to click an eligible assembly to test placing it
You will have to modify this system for actual use in a project