Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 4160 times)
0 Members and 1 Guest are viewing this topic.
Offline DAT64X

Senior Newbie




Java games rock!


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

Hi,

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.

Thanks,
DAT
Offline zparticle

Senior Member




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 Knight


Medals: 12


falling into the abyss of reality


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

I would go for

1  
2  
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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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 Knight


Medals: 12


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.

e.g.

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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;
etc...


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!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (70 views)
2014-08-11 02:49:23

BurntPizza (47 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!