Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 3376 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 extents
int numCols,numRows;
for (int i=0; i<numCols; ++i){
 for (int j=0; j<numRows; ++j){

 //x and y coordinates of my hexagon's vertices
float xpos,ypos;


 //2D array storing verteces of my hextopology  
vertices[i][j] = new VertexClass(xpos, ypos);

 // statements to change xpos/ypos and create hex
}
}


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
Offline 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);

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline 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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline 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 Cheesy
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<numCols; ++i){
            for(int j=0; j<numRows; ++j) {
               float xpos,ypos;
               if (j%2!=0 && i%2!=0 ){
                xpos = side*(i+0.5f);
                ypos = (0.866f*side) * j;
               
               
               } else{
                   xpos = side*(i);
                   ypos = (0.866f*side) * (j+0.5f);
               }
               
               Vector[i][j] = new Vector( xpos, ypos);
            }
         }


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!
Offline 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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline 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
Offline 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.
Offline 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
Offline 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<numCols; ++i){
            for(int j=0; j<numRows; ++j) {
               float xpos,ypos;
               if (j%2!=0  ){
                xpos = side*(i-0.5f);
                ypos = (0.866f*side) * j;
               
               
               } else{
                   xpos = side*(i);
                   ypos = (0.866f*side) *j;
               }
               
               vertices[i][j][0] = new Vector(xpos,
                     ypos, 1.0f);
            }
         }
         for  (int j =0; j<numRows-2; ++j){
            if (j%2 ==0){
               for (int i=j%2*2; i<numCols-2; i+=3){
               
                 
                   Vector a = vertices[i][j][0];              
                   Vector b = vertices[i+1][j][0];
                  Vector c = vertices[i+2][j+1][0];  
                   Vector d = vertices[i+1][j+2][0];
                   Vector e = vertices[i][j+2][0];
                   Vector f = vertices[i][j+1][0];  
                 
               draw.line(a,b);
                 draw.line(b,c);
                 draw.line(c,d);
                 draw.line(d,e);                  
                  draw.line(e,f);
                  draw.line(f,a);
                 
               
               }
            } else{
               for(int i=j%2*2; i<numCols-2; i+=3){

                   Vector a = vertices[i][j][0];              
                   Vector b = vertices[i+1][j][0];
                   Vector c = vertices[i+1][j+1][0];  
                  Vector d = vertices[i+1][j+2][0];
                   Vector e = vertices[i][j+2][0];
                   Vector f = vertices[i-1][j+1][0];  
                 
                draw.line(a,b);
                 draw.line(b,c);
                 draw.line(c,d);
                 draw.line(d,e);                  
                  draw.line(e,f);
                  draw.line(f,a);
                 
               }
            }
           
         }


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.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (32 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
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!