Snap to grid help with placement system

TLDR: How can i trick my game into assuming a different origin with a cframe?

I’m almost there!!

I am working on a placement system, and the snap to grid is the last thing to do. Right now, my placement system has no snap to grid yet, but I have the steps to get there.

I already have a snapping function,

local function round(n: number): number
	return math.floor(n / 3 + .5) * 3
end

but how can I make that work with this custom set grid-origin which is rotated differently and at a different position other than 0,0,0?

image
Key Red part: ray hit + ray normal

By that, here is a grid that is generated based off of the face the mouse is on. Its position does not change no matter where my mouse is on a specific face.

How can I get snap-to-grid to lock on this specific custom set origin I made? I tried using :ToObjectSpace() with the cframe of my grid before applying the snap function to the position, but it didn’t work. I also tried dividing the cframe of the block by the grid cframe before snapping (and then re-multiplying afterwards). Can anyone help me out?

Also when I say divide cframe, I mean cframe1 * cframe2:Inverse()

A quick search I found a snap system, after watching his video I conclude he might be using attachments for his snap system. Perhaps you can use something like that.

2 Likes

Thank you very much for your reply! I’m trying to see how I can divide a cframes, apply the snap, and multiply them back. That’s what I’m doing, and I’m pretty sure it should work, but I can’t. Do you know how I can make use use of the cframe class to get there?