Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (755) Games in Android Showcase (229) games submitted by our members Games in WIP (842) 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
 How to store vertices positions of a hexgrid in a 2D Array?  (Read 7695 times) 0 Members and 1 Guest are viewing this topic.
kafka82

Junior Newbie

 « Posted 2011-07-11 15:48:36 »

Im facing this issue. I want to create an hexgrid and be able to create in this fashion:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `//grid extentsint numCols,numRows;for (int i=0; i

All methods I found to make hexgrid, first create an hex object and then replicate it over a grid thus creating duplicate verteces position ad joining edges. I want to avoid duplicating verteces position. How can I declare statements to make such a grid?

Thanks
philfrei
 « Reply #1 - Posted 2011-07-11 17:04:29 »

May I ask why not have the duplicate vertices? I don't think the draws involved consume very much time.

You could write your own, and have if-statements to inspect whether you are drawing the first row or column, and only draw all six sides in that case. Then for the rest, draw only the remaining sides.

I'm using the following to calculate the locations of the vertices, given a top corner (outside the hex). By creating a Polygon, I'm able to use its "contains" function to tell if the mouse is within the bounds. When you draw, you can pick the sides you want to draw. The orientation here is pointy side up. Shouldn't be too hard to change to make it flat-side up.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `// some constants    public static final int SIDELENGTH = 32;  // length of a side in pixels    public static final int COSSIDE = (int)(SIDELENGTH * Math.cos((30 * Math.PI)/180));// for top corner (xloc, yloc)    int[] xpts = new int[6];    int[] ypts = new int[6];    xpts[0] =  xLoc + COSSIDE;    xpts[1] = xpts[0]+ COSSIDE;    xpts[2] = xpts[1];    xpts[3] = xpts[0];    xpts[4] = xLoc;    xpts[5] = xpts[4];       ypts[0] = yLoc;    ypts[1] = yLoc + (SIDELENGTH/2);    ypts[2] = ypts[1] + SIDELENGTH;    ypts[3] = ypts[0] + SIDELENGTH*2;    ypts[4] = ypts[2];    ypts[5] = ypts[1];    this.polygon = new Polygon(xpts, ypts, 6);`

music and music apps: http://adonax.com
philfrei
 « Reply #2 - Posted 2011-07-11 17:29:58 »

I'm sorry, I didn't read your question closely enough.

Maybe just sort the positions by their starting x and y values? For example, set it up so that each row in the array has the same y starting value?

Of course it all depends upon what you want to do with the data once you have it. Will the orientation be fixed or can the hexes rotate? Will you be using the data for bounds checking ever?

To find the vertices of a given hex x,y, you can write a formula to get the correct indices of their locations in the array.

music and music apps: http://adonax.com
 Games published by our own members! Check 'em out!
kafka82

Junior Newbie

 « Reply #3 - Posted 2011-07-11 17:52:03 »

no worries. what I would like to do is this
-have different basic grid layouts (hex, quad, tria)
-able to extract from the created grid every single basic  shape (so its vertices) and store it in a list

I will have so different classes each one for each basic shape. For example a triangle class to be instatiated needs 3 points. Im doing this because the starting grid will be deformed according to some rules but topology will remain unchanged and so made up od "deformed" shape. Still later I want to retrieve all "pieces" of the deformed grid. Hope this is clear.

Now, for quads, is straightforward, I manaed to do it for triangles...but with hexagons Im really struggling...I read many articles but still stuck. How would you sort the points?
kafka82

Junior Newbie

 « Reply #4 - Posted 2011-07-11 18:40:39 »

Ok Ill show you with one pic the problem I have so maybe you can give more inputs
Now I have a shifted grid which I used for triangles, the code looks like
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `float side = 10;for (int i=0; i

Ok I can have unsed points although I was trying to avoid it but it seems I cannot skip with hexagons. So the situation is like in the picture below. Now once I created the grid how do I sort to create the hex tiling as in the picture? I need the indeces of the six verteces to sort all point in my grid...of course at the edge of the grid there will be some unsed (unsorted) points - those within green line in my pic.

Thanks for any help!
philfrei
 « Reply #5 - Posted 2011-07-11 22:49:23 »

I can't see your picture, unfortunately. Have you posted it on a working URL?

If all you want are the points, I would give your for loops one pass per y-value, rather than one pass per hex. And store them sorted first by y then by x. Given that, it should be possible to create a formula to find the right indexes into the array for a particular hex.

It seems like you already have a handle on the trig involved to make successive horizontal passes.

I confess, when I made Hexara (www.hexara.com still a work in progress), which uses a hex grid, I wasn't trying to search for shapes on the fly. Instead, I created a Hexagon object, with a polygon stored for its "contains" method, and gave each Hexagon a list of it's neighbors, in a HashMap for when I'm doing tree searches of possible paths through the grid. I only have 20-something hexagons, sounds like you have a lot more than that.

music and music apps: http://adonax.com
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #6 - Posted 2011-07-11 23:09:40 »

You can easily make all the vertices for a hex grid just by looping through and offsetting every other row by 0.25 * hex width, or 0.5 * the length of a side (so 1 in your case). Like this.

 1  2  3  4  5  6  7  8  9 `for (int i = 0; i < rowCount; i++){    for (int j = 0; j < colCount; j++)    {        float x = j * gridSize;        float y = (i % 2 == 0) ? (i+0.5f) * gridSize : i * gridSize;        vertexList.add(new Vertex(x,y));     }}`

See my work:
OTC Software
kafka82

Junior Newbie

 « Reply #7 - Posted 2011-07-12 00:07:52 »

Im not sure about what this lines does

 1 `  float y = (i % 2 == 0) ? (i+offsetFraction) * gridSize : i * gridSize;`

what are ? and : for? sorry Im new to programming.
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #8 - Posted 2011-07-12 00:25:15 »

Im not sure about what this lines does

 1 `  float y = (i % 2 == 0) ? (i+0.5f) * gridSize : i * gridSize;`

what are ? and : for? sorry Im new to programming.
Sure, that's called a ternary operator which is basically shorthand for an if/else statement, and it's a little faster to execute.

 1 `String resultString = amount > 50 ? "You won!" : "You lost!";`

is the same as
 1  2  3  4  5 `String resultString = "You lost!";if (amount > 50){    resultString = "You won!";}`

Or also:
 1  2  3  4  5  6  7  8  9 `String resultString = null;if (amount > 50){    resultString = "You won!";}else{    resultString = "You lost!";}`

So basically I was just checking to see if we were at an even row, and if we were I offset by 0.5.

See my work:
OTC Software
kafka82

Junior Newbie

 « Reply #9 - Posted 2011-07-12 12:55:59 »

mhh here's the solution I found - I wonder if I could cleanup the code a bit and make just 1 loop instead of 2 without the j%2 statement...
The two blocks of code for j%2 change only for Vector c and f respectively the other stayed unchanged - so it it possible to improve it?
xpos = side; and ypos=0.866*side; in this manner I force the grid to be of regular hexagons.

I execute outside the for loops a purgeLines() function to delete lines that share equal start and end points coordinates - so get rid of duplicates.

As Philfrei mentioned the other approach would be to the create a hexagon object itself and then replicate over a grid, and then purge duplicates points at joining hexagons and not at the edge of grid. Never used hashmaps but is something I would really like to harness when Ill get sometime.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61 `float side = w / (numCols);         particles = new Particle[numCols][numRows][2];         for (int i=0; i

Pages: [1]
 ignore  |  Print

 DesertCoockie (52 views) 2018-05-13 18:23:11 nelsongames (82 views) 2018-04-24 18:15:36 nelsongames (74 views) 2018-04-24 18:14:32 ivj94 (757 views) 2018-03-24 14:47:39 ivj94 (87 views) 2018-03-24 14:46:31 ivj94 (643 views) 2018-03-24 14:43:53 Solater (102 views) 2018-03-17 05:04:08 nelsongames (184 views) 2018-03-05 17:56:34 Gornova (426 views) 2018-03-02 22:15:33 buddyBro (1086 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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