Grid Snapping Problem

Hey guys, so I have a weird problem with my grid-snapping system. It’s working just fine for 15x15 structures but any time I try to use 20x20 structures (or really any multiple of 10) the grid is off-line.

Here’s the snippet of my code that controls the placement/snapping

--"plate" is the part that the structures are being placed upon
--"rotation" is a multiple of 90
--"item" is the structure being placed
local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction*1000)
local hit,rPos,normal=workspace:FindPartOnRayWithWhitelist(ray,{plate});
local x,z=math.floor(rPos.X/5+0.5)*5,math.floor(rPos.Z/5+0.5)*5;
pos=Vector2.new(x,z)
item:SetPrimaryPartCFrame(CFrame.Angles(0,rotation,0)+Vector3.new(pos.X,plate.Position.Y+(plate.Size.Y/2)+(item.PrimaryPart.Size.Y/2),pos.Y));

This is for a 5x5 grid, however 10x10, 20x20, etc. structures are being offset by 2.5 studs (being placed in the center of the grid cells) while it’s working perfectly fine for structures that are 5x5, 15x15, etc.

Does anyone happen to know what’s going on here? Any help is greatly appreciated :smile:

1 Like

for the model try making the primary part an invisible part that encompasses the entire model/

The part spans the entirety of the model (as you can see from the outline below the model when placing it), it’s just offset from the grid which leads me to believe it’s a problem in my math somewhere.

1 Like

Can i ask why are you using raycasting? when you dont need to I have a solution you can use without raycasting

1 Like

Raycasting is the easiest way to fetch the mouse’s hit position with the baseplate without anything else interfering. If I were to use mouse.Hit or something like that, I would have to use mouse.TargetFilter that only supports one model at a time.

For me, this works fine for any model and grid size as long as you follow the parameters (though these could be used regardless of the method used).

  • Your grid size is a multiple of your plot (cannot be a decimal) - Needed
  • Primary part size is a multiple of grid size - Recommended
  • Grid size is an even number - Recommended

I also recommend that when building the primary part and the model, use the grid that you are using as a guide.

Anyway, here is the method:

local function Calc(pos, grid, primary)
     return math.floor((pos.AXIS / grid) + 0.5) * grid + (primary.Size.AXIS / 2)
end

You would have to make it work for your placement system but it’s the basic equation.

Hope this help!

try doing this:

--calculate your grid snapped position like this instead
local placingAreaPart = --you need to place the part that you are placing items on here
local model =--put the model you are placing here
local rotation = --insert the rotation here of the model
local modelSize = CFrame.fromEulerAnglesYXZ(0, rotation, 0)*model.PrimaryPart.Size
local size,size2
modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
size = Vector2.new((placingAreaPart.Size * Vector3.new(-1,0,0))).magnitude, (placingAreaPart.Size  * Vector3.new(0,0,-1)).magnitude)
size2 = (size - Vector2.new(modelSize.x, modelSize.z))/2 
x = math.sign(rPos.x)*((math.abs(rPos.x) - math.abs(rPos.x) % 5) + ( Size2.x% 5))
z = math.sign(rPos.z)*((math.abs(rPos.z) - math.abs(rPos.z) % 5) + ( Size2.z% 5))

warning there may be some syntax errors

1 Like

Hey, thanks for the reply!

If you look above I am using that method currently, however the problem seems to stem from my grid being an odd number (5). I already follow all the grid guidelines in my primary parts & structures.

I’m going to try to add an offset for any multiples of 10 and see if maybe that’ll solve my problem. I’ll update this reply w/ my results.

1 Like

Ok great! If you are not taking rotation into account then you might wan’t to take a look at the answer to the post I posted. My placement post (not required).

One other thing though, I would recommend you to not use ray casting for the use of ignoring other objects on the plot.

I instead just get the tycoon objects folder/model and set that as the mouse’s target filter.

mouse.TargetFilter = tycoonObjs

Good luck!

Hey, that worked! Thanks so much!! :smile:

1 Like

There is a way around this. I know I said this in a reply, but I will explain how I do this more in depth since this is simpler. I usually have my plot organized like this:

help

If you set the mouse’s target filter to the PlacedObjects folder, then all the objects will be ignored!

mouse.TargetFilter = placedObjects

Your description suggests your calculations work fine and placement.

It seems you are using the center of a grid to line up with the center of an object though.

What you are wanting to do is line up corners so the edge is on the lines right?

The reason it works with 5/15/25 sizes is that they use up an odd number of your grids.

1 Like

But I also want to be able to filter out your character & other players’ characters and anything else that might be in the way. Basically it’s easier for me to set a whitelist than it is to set a blacklist.

also i would recommend you use this tutorial its where i learned about placement systems for a game im working on its very good: Creating A Furniture Placement System

2 Likes

This is probably terrible (I don’t recommend doing this. I don’t know if this would work), but you could just say this:

mouse.TargetFilter = game.Workspace

This is also bad for the things you do wan’t to interact with. After placement, you would have to set target filter to nil.

This isn’t too hard if you keep all your objects on square bases, just use the size/2 offset and put it on a corner.

something like this:
gridCorner = gridCenter + (-2.5,0,-2.5)
SetCFrame to: gridCorner + (size/2, 0, size/2)

3 Likes