I know they say to never change a running system, but:
I think you should look into this further. Hard-Coding is the easiest way, but changing anything later on will be a pain.*
How are you offsetting the map? Are you using translate?
You should look into vrdl's "HUMP" library, it offers a camera that is really useful here. If you use this, there's a function that converts mouse coordinates to world-coordinates, which is probably the problem that you're having.
Although it's really weird that hard coding it fixes it. Maybe you could post a .love showing the problem?
I've got a working example of snap-to-grid in the attached demo.
- Bezier.love
- (Fixed version) Usage: Draw a shape using CTRL+click multiple times. Then click outside the shape and drag it. Both creating of points and dragging the shape snap to grid.
- (28.13 KiB) Downloaded 228 times
The code for smapping to grid is in Scripts/shapeControl.lua, in function ShapeControl:update( mX, mY, dt ).
mX and mY are the mouse coordinates, transformed into world coordinates, after moving and scaling the camera.
Then, you can simply do:
Code: Select all
if self.snapToGrid then
mX = math.floor((mX+self.gridSize/2)/self.gridSize)*self.gridSize
mY = math.floor((mY+self.gridSize/2)/self.gridSize)*self.gridSize
end
"+self.gridSize/2" is there in ordeer to round up if mX or mY are more than half way to the next grid line.
* If you really need to hard-code it, at least do it using a while or for loop, something like:
Code: Select all
tileSize = 50
curX = 0
while curX < x do
if curX + tileSize >= x then
x = curX
break
end
curX = curX + tileSize
end
(untested)