🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

moving sprite on a hex grid

Started by
73 comments, last by Tom Sloper 1 year, 11 months ago

btw dirt is like mountains

Advertisement

warp9 how do I incorporate different tiles into my movement code?. I am asking the same question as above.

here is my movement code

	case GLUT_KEY_UP:
		if(rotate_tank==3)
		{
		drawTank_one = false;
		move_x += 15.0f;
		}
		if (rotate_tank == 0)
		{
		drawTank = false;
		move_x -= 15.0f;
		}
		if (rotate_tank == 2)
		{
			drawTank_two = false;
			move_x += 8.0f;
			move_y += 13.5f;
		}
		if (rotate_tank == 1)
		{
			drawTank_three = false;
			move_x -= 8.0f;
			move_y += 13.5f;
		}
		if (rotate_tank == 4)
		{
			drawTank_four = false;
			move_x += 8.0f;
			move_y -= 13.5f;
		}
		if (rotate_tank == 5)
		{
			drawTank_five = false;
			move_x -= 8.0f;
			move_y -= 13.5f;
		}
		break;
	case GLUT_KEY_DOWN:
		if (rotate_tank == 3)
		{
			drawTank_one = false;
			move_x -= 15.0f;
		}
		if (rotate_tank == 0)
		{
			drawTank = false;
			move_x += 15.0f;
		}
		if (rotate_tank == 2)
		{
			drawTank_two = false;
			move_x -= 8.0f;
			move_y -= 13.5f;
		}
		if (rotate_tank == 1)
		{
			drawTank_three = false;
			move_x += 8.0f;
			move_y -= 13.5f;
		}
		if (rotate_tank == 4)
		{
			drawTank_four = false;
			move_x -= 8.0f;
			move_y += 13.5f;
		}
		if (rotate_tank == 5)
		{
			drawTank_five = false;
			move_x += 8.0f;
			move_y += 13.5f;
		}
		break;
	}

When you are drawing your grid, how do you determine which hexes will be grass, and which hexes will be water, and which hexes will be dirt?

I assume that you would store that data somewhere, so you can use it when drawing and redrawing your grid. If you have that data stored, then you need to match the hex the tank wants to move to, with the data for the appropriate hex. And based on that data, you can decide if that is a valid move, or not.

thanks for all the help warp9. however, I am uncertain of how to detect which sprites are grass and which are mountains and water, can you help me with my code?

@pbivens67 : You need to go a step deeper than asking about the sprites. You need to be asking about how you determined which sprite to use in the first place. Which means you need some landscape data to base everything on. I'm going to suggest a 2D array of data for your hex grid. Each array element represents one hex.

If you want to keep it simple, each hex can be represented by an integer value. Let us say 0 = water, 1 = grass, and 2 = dirt. So this could be a 2D array of type int.

int HexGrid[10][10]; // this is for a 10X10 Grid of Hexes

Each hex has an X loc and a Y loc. These hex-loc numbers are values on the hex grid, not the numbers you are giving to OpenGL to draw with. You can go back an look at that drawing of the grids I made, to see how these numbers are set up.

Before you start trying to move the tank around, fill this array with landscape data.

Once the array is set up. . . To find what a given hex is, get its Hex Grid Location, figure out which hex the tank is trying to move into. And use those hex x and y values as indices for the HexGrid array. . . Which would look like this :

int HexType = HexGrid[HexLocX][HexLocY]; // note: make sure these x and y indices are valid before doing this

If you've filled out your array with valid data, the value of HexType will indicate what kind of terrain it is. Then it is simply a matter of looking at this number to see if it is a good place to move. If the tank can only move on grass, and we've decided that grass = 1, then you can say :

if(HexType == 1)   // if our tarkget hex is grass, go ahead with the move
    DoMove();
else // this is not a good terrain for the tank
    DoNotMove(); 

here are my drawing hexes code, how do I convert my line loop hexes to hex grids. thanks, Warp9

void drawHex()
{
	bool drawTank = false;
	float hexagon_r = zoom;
	float hexagon_dx = hexagon_r * cos(30.0*PI / 180.0);
	float hexagon_dy = hexagon_r * sin(30.0*PI / 180.0);
	float hexagon_gx = 2.0*hexagon_dx;
	float hexagon_gy = 2.0*hexagon_dx*sin(60.0*PI / 180.0);
	float x = 1.0f, y = 1.0f, z = 0.0f;
	int ni = 22, nj = 22;
			int i, j; float x0;
			x -= float(ni - 1)*hexagon_gx*0.5; // just shift x,y to start position (i=0,j=0)
			x -= float(nj - 1)*hexagon_dx*0.5;
			y -= float(nj - 1)*hexagon_gy*0.5;
			for (x0 = x, j = 0; j < nj; j++, x0 += hexagon_dx, x = x0, y += hexagon_gy)
			{
				for (i = 0; i < ni; i++, x += hexagon_gx)
				{
					glBegin(GL_LINE_LOOP);
					glVertex3f(x - hexagon_dx, y - hexagon_dy, z);
					glVertex3f(x - hexagon_dx, y + hexagon_dy, z);
					glVertex3f(x, y + hexagon_r, z);
					glVertex3f(x + hexagon_dx, y + hexagon_dy, z);
					glVertex3f(x + hexagon_dx, y - hexagon_dy, z);
					glVertex3f(x, y - hexagon_r, z);
					glEnd();
				}
			}
}

@pbivens67 : Well, in your code you are looping through x and y, but you are also looping though i and j.

In your code, x and y are the coordinates used to draw the hex centers. Whereas the i and j variables are the actual hex numbers, So you definitely have the Hex Grid numbers right there with i and j (although I'd pick some more descriptive names for those variables myself).

Little bit more complicated than sqaure grids.

None

Warp9, can you explain further how to convert screen coordinates to hex grid coordinates.

Phil, it's not polite to call out an individual in an online forum. You should be open to advice from anyone who's willing to give it, without addressing one specific person. Warp9 is not obligated to explain further and into more detail ad infinitum. The explanations Warp9 has given you to date should be specific enough. If there's one particular instruction you don't understand, you should say specifically what you don't follow.

Previous helpers have become frustrated by trying to help you because no matter how much help you're given, it's never enough. In the past I have locked your threads to prevent that frustration from growing, but that doesn't seem to work out very well. This thread has gone on for four pages and it can't go on forever.

My recommendation is that you re-read everything, all the help you've gotten from Warp9, and see if you can't figure out how to convert screen coordinates to hex grid coordinates from what he's already told you.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement