SFML community forums

Help => General => Topic started by: Clyx on October 06, 2016, 09:15:34 pm

Title: Hexagonal grid with vertexarray
Post by: Clyx on October 06, 2016, 09:15:34 pm
Hi,

I'm stucked for about 1 week trying to make an algorithm which will allow me to render an hexagonal tilemap.

I would like to know how I can implement this system through SFML in an optimzed way.

For now I have a class which extends Drawable and that's all...

Here is what I've done :

(click to show/hide)

PS: I woud like to have pointy hexagon.

Any help would be appreciated.

Thanks in advance.
Title: AW: Hexagonal grid with vertexarray
Post by: eXpl0it3r on October 07, 2016, 09:51:58 am
First you need to get your math laid out. This step is unrelated to SFML and is simply about geometry. Try answering questions like: What are hexagon's position in the grid? How do I check whether a point is in a hexagon? How do I figure out what the hexagon is underneath a random.positio? etc.
There are multiple websites on the internet that are dedicated to this topic.

As for drawing a hexagon you can either use the sf::ConvexShape or a vertex array.

What else are you looking for specifically?
Title: Re: Hexagonal grid with vertexarray
Post by: Clyx on October 07, 2016, 11:41:35 am
Thanks for your answer !

What I want to do is basically setup my vertices variable which is a VertexArray and fill it with the good hexagon's corner and then simply draw it with the RenderWindow.

So I'd like to know what do I have to put for each hex hex[0] hex[1] ...
I'm using the tutorial http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php (http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php)

Also I don't know which drawing mode I have to user, do I use Triangles, TrianglesFan , TrianlesStrip or something else?

I'm sorry for my english , I do my best to be understandable.

Thanks.
Title: AW: Hexagonal grid with vertexarray
Post by: eXpl0it3r on October 07, 2016, 12:06:58 pm
This is one of the sites I had in mind: http://www.redblobgames.com/grids/hexagons/

The vertex position just represent all six corners of the hexagon. I'm sure you can figure out the values yourself...

Look up what each primitive does and then make your own decision. You don't learn much if you just follow 1:1 what people tell you without doing your own research and trying to understand how it works. ;)
Title: Re: Hexagonal grid with vertexarray
Post by: Clyx on October 10, 2016, 04:02:20 pm
Yeah.. I tried again and again.. But I'm not able to figure out :/

My code :

(click to show/hide)

I don't know why it doesn't work, it seems that the second hex starts from the origin of the previous.

This is what I get :

(http://image.prntscr.com/image/5a543c5283f44fa2b090a3093bcd1386.png)

Any idea?

Thanks you
Title: Re: Hexagonal grid with vertexarray
Post by: Laurent on October 10, 2016, 04:09:12 pm
You've got a single triangle fan, so it has a single center and all the other points are the border. How was it supposed to know when a new fan starts again?
Title: Re: Hexagonal grid with vertexarray
Post by: Clyx on October 10, 2016, 04:12:56 pm
Thanks for your answer,

but I don't understand how can I tell that a new fan starts? I'm totally confused about that..

Title: Re: Hexagonal grid with vertexarray
Post by: Laurent on October 10, 2016, 04:22:10 pm
That's the thing... you can't. A single vertex array will always be a single triangle fan. You need multiple vertex arrays if you want more than one fan. That's why Triangles primitive is often preferred: you'll need more points but you'll get much more flexibility.