Need help offsetting a vector in a grid

So i have a grid that can be centered or not. as shown below

This grid is procedural so its all math based and I use the actor location - the hit location then divide that by the grid length in each direction multiplied by the number of rows and it will spit out a a floored grid coord.

this works perfectly as shown below

but when i center the grid so the actors location is zeroed in the center of the grids my function to get the cell location is obviously offfset as show below

so I need to set up a branch that checks if grid is centered when it determines cell location.
The only issue i’m having is how do I set up the math to do the offset? below is the math for getting the cell location when not centered

Somewhere in this math i need to offset the cell location by half the cell size in X and Y but I’m just drawing a blank on where. any help is appreciated.

After some trial and error i have discovered the current code works perfectly fine when centered also unless the number of rows or columns is an odd number. if it is an even number it will work perfectly but the issue is when it is not centered the odd numbers work perfectly. so i need to branch off when centered an offset for the odd number of rows or columns i guess? maybe idk

Hi, I suggest you keep your grid origin (Actor Location) always in the lower left corner of a grid cell (since that is what your logic in your blueprint image assumes).

As a “hotfix” for odd number, subtract half cell size (in x and y) from your actor location and then use that.

What you’re doing in your logic is taking the distance from the input location to your grid origin in numbers of cells. So if you’re 0 to 1 cell sizes from origin, you’re in the 0. cell. From 1 to 2 cell sizes your in the 1. cell, and so on. Now imagine your origin is inside the middle of a cell. Now that above no longer applies. Now as long as you’re -1/2 to 1/2 cell sizes from origin you’re in the 0. cell. From 1/2 to 3/2 you’re in the 1. cell, and so on. Now if your origin is in the middle of a cell and you subtract 1/2 grid size from it, then the result is again in the lower left corner of a grid cell and your logic will again work. That means you can either subtract half cell size from your origin if your origin is in the middle of a cell (you have an odd number) or you directly set your actor location to always be in the lower left corner of a grid cell (then you need no branch there to check between even and odd numbers).

Alright so after hours of working on this issue I finally found a solution that works through trial and error!

So as I stated in a comment the equation worked perfectly when the grid root actor was not centered in the middle of rows and columns regardless if rows or columns were even or odd numbers.

When I would center the grid root actor to the middle of the grid I would run into issues when either the row or column was an odd number or if the rows or columns were not the same number. I was stumped because I couldn’t seem to see the answer that was right in front of me the whole time!

So what I did was at the start of the function I added a branch to check if grid is centered or not. if it wasn’t I ran original script. if it was centered I then did a check to see if the X value was an even or odd number by taking the in X value and inputting it into a modulo against the number 2 then checking if the remaining value was greater than 0 if so i would then check the Y and if not i would check the Y and through whatever scenario it came up with i would then add half the tile size from whatever value was odd then run the original scrip with those values and it works perfectly

This is a very niche problem I ran into but in the unfortunate event someone reads this and is looking for a similar issue when using a math based grid on the location of a mouse hit location this is how I got it to work like a charm. Cheers!

this is sort of what I did. I ended up adding a print string to my mouse hit location and breaking down the math in real time and came up with a solution to tell if the rows or columns were even and if so was able to off set the corresponding X or Y with half the cell size and it works perfectly. I do appreciate you taking the time to give me your input. and I thought about keeping the root actor in the bottom left but it defeats he purpose of future design I plan to use with this grid. I posted my solution below if you were curious on how I solved this.