Page 1 of 1

How to make object snap to grid with mouse's positions?

Posted: Tue May 19, 2015 12:49 am
by redsled
So, I want to make a level editor for my game, and future projects, however the first problem I have is making the mouse's position snap to a set grid (such as 32x32 tiles).

Thanks a bunch,
-redsled :awesome:

EDIT

Found the solution here: viewtopic.php?f=3&t=14743

Sorry :P

Re: How to make object snap to grid with mouse's positions?

Posted: Tue May 19, 2015 11:44 pm
by rlaitila
ive used the following helper function in many of my code dealings:

Code: Select all

--
-- Snaps a source x,y position to a grid based on grid x width and grid y width
--
function snapToGrid(SOURCE_X, SOURCE_Y, GRID_X, GRID_Y)
    local x = math.floor((SOURCE_X / GRID_X) +0.5) * GRID_X
    local y = math.floor((SOURCE_Y / GRID_Y) +0.5) * GRID_Y    
    return x, y
end