#Grid building system part misallignment [NOT SOLVED]

1 messages · Page 1 of 1 (latest)

ionic mica
#

CLICK THIS PLS I NEED HELP [EXPLAINED FURTHER IN POST]

ionic mica
#

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 ^^^^

acoustic igloo
#

Are you making a plug-in?

ionic mica
#

no

#

im making a building system

acoustic igloo
#

It looks like it's aligning on the center point

ionic mica
#

isnt it supposed to be aligned there though

acoustic igloo
#

No because then the part is inside of the other part

ionic mica
#

so is that the entire reason

acoustic igloo
#

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

ionic mica
#

wdym backing data structure

acoustic igloo
#

Do you precompute a grid

ionic mica
#

no

#

i dont

acoustic igloo
#

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

ionic mica
#

wouldnt precomputing a grid be more resource intensive

acoustic igloo
#

I mean how often do you expect a player to add a part per second

ionic mica
#

like 3

acoustic igloo
#

And how are you keeping track of positions on the server

#

Is this a plot system or fully free build?

ionic mica
#

plot system

acoustic igloo
#

Then I think a grid system is easier

ionic mica
#

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

acoustic igloo
#

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

ionic mica
#

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

acoustic igloo
#

Or can they only place things on other things

acoustic igloo
#

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