Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (794)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  "Snap-to-grid" functionality  (Read 10829 times)
0 Members and 1 Guest are viewing this topic.
Offline DAT64X

Senior Newbie

Java games rock!

« Posted 2003-04-14 17:39:26 »


I was wondering if anyone had any tips, advice of objects "snapping" to a grid so that they are aligned along some grid lines/etc.  For instance, if I have a square grid and am dragging around a square and want it to only position along the grid lines.

Offline zparticle

Senior Devvie

Thick As A Brick

« Reply #1 - Posted 2003-04-14 17:48:41 »

Off the top of my head: take the object current X,Y and add the difference between that and the mouse X,Y (because you may not always have the mouse pointer right on the left and top edge of the object) and if that is greater than halfway across a grid cell move the object to the next grid cell X, same for the Y.

Offline Abuse

JGO Ninja

Medals: 71

falling into the abyss of reality

« Reply #2 - Posted 2003-04-15 16:04:04 »

I would go for

alignedObjectX = (objectX%cellWidth)*cellWidth;
alignedObjectY = (objectY%cellHeight)*cellHeight;

ofcourse, you will have to store both the real (objectX/Y)and aligned(alignedObjectX/Y) coordinates of the object.

When you move the object, you modify the objectX/Y coords (and recalculate the alignedObjectX/Y).
When you render the object, you use the alignedObjectX/Y coords.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DarkwingGT

Senior Newbie

Java games rock!

« Reply #3 - Posted 2003-04-16 01:40:26 »

Wouldn't you want the object position to then become the aligned position?  I would think it would be confusing if you align something to a grid but the data is not aligned.

Dunno if that's what you want.  I had to implement this for my job, I created a form builder, but since the form was printable I had to make sure that the object was where it was on the screen as well as on the paper.
Offline Abuse

JGO Ninja

Medals: 71

falling into the abyss of reality

« Reply #4 - Posted 2003-04-16 18:11:05 »

depends on what you were aligning.

I was trying to highlight the pitfall of re-aligning the x/y position after incremental relative movements.


you have an object @ 0,0;  it is aligned on a 20x20 grid.

each time you press an arrow key, the object is moved 5 pixels.
After each movement, the object is realigned, and is moved back to 0,0.
Hence, its impossible to move it Cheesy

If you were using an absolute movement system i.e. (moveDragged/mouseMoved) then you wouldn't have to worry about such issues.

Maybe I was thinking ahead 2much Cheesy
Offline DarkwingGT

Senior Newbie

Java games rock!

« Reply #5 - Posted 2003-04-17 13:08:03 »

Ah, didn't think of that.  Although if it were me and I needed to use the keyboard for example, I'd simple capture the keypress and move the component in my grid increments rather than worrying about relative alignment and stuff.
Offline Jeff

JGO Coder

Got any cats?

« Reply #6 - Posted 2003-04-21 17:27:09 »

My turn to weigh in.  For sucha  too lid likely use Swing and
a scroll pane.  If i did this then mouse clicks would be relative to
the origin of the drawing surface ragrdless of where Im scrolled.

Id then just do this for simplicity sake:

x = (mouse.x / gridCellWidh)*gridCellWidth;
y = (mouse.y /gridCellHeight)*gridCellHeight;

if you want top snap from the middle then you just need to add half a cell width like so:

x = ((mouse.x+gridCellWidth/2)/gridCellWidth)*gridCellWidth;

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Pages: [1]
  ignore  |  Print  

hadezbladez (3162 views)
2018-11-16 13:46:03

hadezbladez (1145 views)
2018-11-16 13:41:33

hadezbladez (3131 views)
2018-11-16 13:35:35

hadezbladez (625 views)
2018-11-16 13:32:03

EgonOlsen (3913 views)
2018-06-10 19:43:48

EgonOlsen (4389 views)
2018-06-10 19:43:44

EgonOlsen (2618 views)
2018-06-10 19:43:20

DesertCoockie (3437 views)
2018-05-13 18:23:11

nelsongames (3559 views)
2018-04-24 18:15:36

nelsongames (4609 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!