Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Author
Message
Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 16th Jun 2011 10:14
I didn't know which forum section to post this in and so i just decided this because i have dbp. Anyways, I am having trouble making a simple grid. A grid in which if you put your mouse in just one pixel of one of the boxes, it snaps to it. Ive looked through the forums on 2D grids and nothing really met what I was having a problem with.

onhcet d(-_-)b techno
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 16th Jun 2011 11:14 Edited at: 16th Jun 2011 11:15
Hi,

If you're looking to make a normal rectangular grid, then you can use the following expressions:

If each grid cell has a size of (cWidth, cHeight) and the grids origin is located at coordinates (GridX, GridY), then:

Finding the grid coordinates from 2D coordinates:
GridPosX# = (PosX# - GridX) / cWidth
GridPosY# = (PosY# - GridY) / cHeight

Finding 2D coordinates from grid coordinates:
PosX# = GridPosX# * cWidth + GridX
PosY# = GridPosY# * cHeight + GridY

'Snapping' grid coordinates:
(You basically have to round them up/down, for example)
GridPosX = int(GridPosX# + 0.5)
GridPosY = int(GridPosY# + 0.5)
-> Gives more or less unwanted results when GridPosX/Y# < 0.0 though, you might want to write your own rounding function.

'Snapping' 2D coordinates:
GridPosX# = (PosX# - GridX) / cWidth
GridPosY# = (PosY# - GridY) / cHeight
GridPosX = int(GridPosX# + 0.5)
GridPosY = int(GridPosY# + 0.5)
PosX = GridPosX * cWidth + GridX
PosY = GridPosY * cHeight + GridY

I think this sums it all up. Good luck with your project ^^!

Sven B

Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 16th Jun 2011 11:47
thank you for the replay, ill try this.

onhcet d(-_-)b techno
DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 21st Jun 2011 01:35
Everything Sven says looks correct, although he explains in a typical coders logic. By that I mean stating code snippets without explanation. When snapping an object to a grid you just have to think logically.

If your screen estate is 100x100 pixels and you have 10x10 grid within that, all you need do is divide your pixel x and y position by 10 ( 100/10=10), to get a snap influence.
Lets say you click pixel 11x11. Dividing that by ten would equal 1.1 x 1.1, rounding that down you get 1x1, which would bet correct and snap you to the grid If you click pixel 9x9 you would get .9x.9, which would be rounded down to 0 x 0.
I'm pretty sure DB will automatically do this if you set your variables as an integer, rather than a float. If you want to control it use the int() command, which will automatically round your numbers down. You can also round up if you use the ceil command, rather than int. But it is not needed for this, just giving you a little info

http://s6.bitefight.org/c.php?uid=103081
Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 21st Jun 2011 03:56
thanks, thats more than i knew before

onhcet d(-_-)b techno

Login to post a reply

Server time is: 2026-07-10 21:18:39
Your offset time is: 2026-07-10 21:18:39