Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Hexagonal grid with vertexarray  (Read 5367 times)

0 Members and 1 Guest are viewing this topic.

Clyx

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Hexagonal grid with vertexarray
« 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11037
    • View Profile
    • development blog
    • Email
AW: Hexagonal grid with vertexarray
« Reply #1 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Clyx

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Hexagonal grid with vertexarray
« Reply #2 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

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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11037
    • View Profile
    • development blog
    • Email
AW: Hexagonal grid with vertexarray
« Reply #3 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Clyx

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Hexagonal grid with vertexarray
« Reply #4 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 :



Any idea?

Thanks you

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Hexagonal grid with vertexarray
« Reply #5 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?
Laurent Gomila - SFML developer

Clyx

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Hexagonal grid with vertexarray
« Reply #6 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..


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Hexagonal grid with vertexarray
« Reply #7 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.
Laurent Gomila - SFML developer

 

anything