#Grid building system part misallignment [NOT SOLVED]
1 messages · Page 1 of 1 (latest)
here's the bug
so aligning on the ground against a baseplate works perfectly, but when it comes to aligning parts to other parts you'd think it aligns a part against the other, but no. from 2 directions, it aligns the object behind the object that your aligning the preview with and from the other 2, 0.5 studs inside it.
local function sign(x)
if (x == 0) then return 0; end
if (x > 0) then return 1; end
return - 1;
end
-- get the axis-aligned unit vector that vec is closest to
function SnapToAxis(vec)
local lx = math.abs(vec.X)
local ly = math.abs(vec.Y)
local lz = math.abs(vec.Z)
if (lx > ly and lx > lz) then
return Vector3.new(sign(vec.X), 0, 0);
elseif (ly > lx and ly > lz) then
return Vector3.new(0, sign(vec.Y), 0);
else
return Vector3.new(0, 0, sign(vec.Z));
end
end
local function GetPlacementPosition(GridSize, ObjectSize, Rotation, Position, Normal)
local GridCorner = Vector3.new(
math.floor(Position.X / GridSize + 0.5) * GridSize,
math.floor(Position.Y / GridSize + 0.5) * GridSize,
math.floor(Position.Z / GridSize + 0.5) * GridSize
)
local SnappedNormal = SnapToAxis(Normal)
local RotationCF = CFrame.Angles(
math.rad(Rotation.X),
math.rad(Rotation.Y),
math.rad(Rotation.Z)
)
local minProjection = math.huge
local maxProjection = -math.huge
for _, Corner in ipairs(Corners) do
local RotatedCorner = RotationCF * (ObjectSize * Corner)
local Projection = RotatedCorner:Dot(SnappedNormal)
minProjection = math.min(minProjection, Projection)
maxProjection = math.max(maxProjection, Projection)
end
local OffsetAmount
if SnappedNormal:Dot(Vector3.new(0, 1, 0)) > 0 then
OffsetAmount = -minProjection
else
OffsetAmount = -maxProjection
end
local Offset = SnappedNormal * OffsetAmount
return GridCorner + Offset
end
this is my current function which returns the cframe that i align my preview to every frame ^^^^
Are you making a plug-in?
It looks like it's aligning on the center point
isnt it supposed to be aligned there though
No because then the part is inside of the other part
so is that the entire reason
That's part of it
It looks like you are just doing this with raw coordinate data
I would recommend making a backing data structure and using that instead
It will be easier to debug
wdym backing data structure
Do you precompute a grid
I would start by making a system that works w a precomputes grid module
It will compute all cells and coordinates
Then you interact w the grid instead of with coordinates
Then after the core works you can add extensibility to make it dynamic for players to have more control
wouldnt precomputing a grid be more resource intensive
I mean how often do you expect a player to add a part per second
like 3
And how are you keeping track of positions on the server
Is this a plot system or fully free build?
plot system
Then I think a grid system is easier
but at some points its likely gonna be free build
like
you can build outside the plot
and your objects get deleted after a certain amount of time
I think at minimum starting w a small precomputed grid will help you make the system actually work
Once it works, if you run into performance issues you can remove the abstraction
Or lazy load it or something
how would a precomputed grid work
i just get every cell possible for the plot in a table?
then once i move the object it moves to the closest possible cell or something
Well can people place things in mid air
Or can they only place things on other things
Yes
When you are ready to place an object you can find if it's in a certain cell and then set it to that cells pos
You can also do some fancy stuff like assigning a piece multiple cells
I'd at least do that for a prototype
Once everything is aligning properly you can replace it w whatever other implementation you want
Or expand it