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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

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

Post 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
rlaitila
Prole
Posts: 9
Joined: Sat Jun 08, 2013 5:33 am

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

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 247 guests