Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 2812 times)
0 Members and 1 Guest are viewing this topic.
Offline DAT64X

Senior Newbie




Java games rock!


« Posted 2003-04-14 19: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 19: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 Coder


Medals: 2


falling into the abyss of reality


« Reply #2 - Posted 2003-04-15 18: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!
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 03: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 Coder


Medals: 2


falling into the abyss of reality


« Reply #4 - Posted 2003-04-16 20: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!
Offline DarkwingGT

Senior Newbie




Java games rock!


« Reply #5 - Posted 2003-04-17 15: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 19: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (77 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.158 seconds with 20 queries.