Advertisement

moving sprite on a hex grid

Started by June 14, 2022 08:51 PM
73 comments, last by Tom Sloper 2 years, 4 months ago

according to my code above how do I convert my pointy top hex to flat top hex.

@pbivens67 : Hmm, converting pointy top to flat top hex. . .

My snarky answer is to turn your screen sideways. :-D But I somehow suspect that this suggestion will not satisfy you.

The less snarky answer is that I haven't had to worry about that specifically, so I don't have actual experience with doing that. but it should be a simple matter of switching x and y in the draw code.

You could also rotate everything along the z-axis in OpenGL. . . I currently have a game where the hexes are pointy top, but things rotate on the z-axis, so it often appears on the screen as if they are flat top (it just depends on how the player is facing). Although this approach might be more trouble, especially if you want to get mouse input, and find out where you clicked on your game map. If you've rotated everything on your map, you'll have to transform the mouse data as well, to get it to match with your map. That is what I've done with my game, but it makes things more complicated.

Advertisement

Warp9 said:
I like the fact that it gets rid of all the conditional tests based on odd/even row.

Conditional tests? Why not simply paint the odd rows at a slightly different position? For size S, paint square (x, y) at (x * S + (y % 2) * S/2, y * S)

@Alberth : Yes, you are correct. Thank you for pointing that out, and helping me make my code better!

We can get rid of the conditional tests. And here is an improved version of my code for taking a North West Step. . .

// better version of the code!
glm::ivec2 Take_NorthWestStep_On_HexGrid(glm::ivec2 StartPoint)
{
glm::ivec2 Destination;

Destination.y = StartPoint.y + 1; // NW move is always +1 y

// on a NW step, we either keep x the same (on an odd row), or subtract 1 from it (on an even row)
Destination.x = StartPoint.x - (1 - (StartPoint.y % 2) );

return Destination;
}

I tried the glRotate3f function, but it does not work, is there a way to rotate the hex grid using sin and cos

Did the rotate function do something you didn't want? Or did it not do anything at all? What version of OpenGL are you using?

It's been a while since I worked with the rotate function (there are more modern ways to do things now, but I'm guessing you are probably using a more basic version of OpenGL). Still, I remember the old ways, and I believe the function used was :

glRotatef(angle, 0.0f, 0.0f, 1.0f);

The function takes 4 parameters. It allows you to specify an angle (parameter 1) and an axis of rotation (given in the next 3 parameters). In the above example, that would be the z axis (with x and y set to 0.0 and the z value set to 1.0. Which is actually what you should be looking for (since you want to rotate on the z axis).

And for your situation, you'd probably want to put in 90 for the angle of rotation (and I'm pretty sure that it actually takes degrees, as opposed to radians).

You also want to call the rotate function first, before you draw. Because you are actually setting a bunch of stuff OpenGL is going to use in order to draw. And if you've already drawn your hexes, then calling the rotate function will not do anything.

If you want to have more fun though, you could write your own rotation code, instead of relying on OpenGL's functions. . . but I'd suggest trying harder to make the OpenGL stuff work first.

Advertisement

I have tried glRotate but it does not work that's why I want to rotate using trig functions.

@pbivens67 I'm still a bit unclear what “it does not work” specifically means in your situation. . . There could be multiple reasons why it don't work.

However, if you are set on using trig functions to rotate, there is an article on the subject here.

I should add though that most rotation in OpenGL is done with matrix math. If you are going to delve deeper into OpenGL, then it is probably a good idea to learn about that stuff. I'd start off with this article here (which deals with matrix multiplication), and then you can read about matrix based rotation here.

well, warp9 I have drawn green grass background sprites on the hex grid I want the tank sprite to only move on the grass path not on the water or dirt sprites which I have also drawn on the hex grid. let me know if you need more information.

@pbivens67 : it is good to hear that your project is moving along! :-)

To answer your question : if you don't want the tank to move onto specific types of hexes, you want to incorporate a test into the move code. When the tank moves, you need to test to see if this is a legal move, before you proceed to set the tank to the new position. And if it is not a legal move, then don't move the tank.

To test for a legal move, (A) you want to look at the hex where the tank is moving, (B) find out what type of hex it is, and (C) if that hex is a grass hex then the move is OK, other wise it is not.

BTW, this has nothing to do with the actual code stuff. . . but I'm a bit surprised that your tank can't move onto dirt. What kind of tank is that?

This topic is closed to new replies.

Advertisement