Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411613 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 10, 2024, 02:28:25 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Pixel to hexagonal coordinates.
Pages: [1]
Print
Author Topic: Pixel to hexagonal coordinates.  (Read 2354 times)
GAZ082
Level 0
*



View Profile
« on: December 13, 2009, 12:01:07 PM »

Hi guys. I'm trying to dig into canvas and HTML5 making a simple board game. I'm trying to get an algorithm that allows me to convert mouse position on screen to hex grid values.

I've read several tutorials, Amit's one among them, but those are like 10 years old. I'm wondering if you guys know an elegant system to make it work, if something new has came up in all these years.

Thanks!
Logged
JMickle
Level 10
*****



View Profile
« Reply #1 on: December 13, 2009, 12:15:04 PM »

i believe Battle for Wesnoth rotated it 90 degrees in translation. ie:
Code:
1236
2349
3496
would translate to
Code:
 3 2 1
4 3 2
 9 4 3
6 9 6[code]on a hexagonal map.

Dunno if this helps or not but it might be something to work by.
[/code]
Logged

kiwi
Level 0
***


View Profile WWW
« Reply #2 on: December 13, 2009, 12:48:29 PM »

You could try this
1) See if distance from mouse to center of the hexagon is greater than half the size of the hexagon's diagonal (basically a bounding circle)
2) If no do the same for the rest of the hexagons, if yes then do a convex wrap - for each of the hexagon's points compute the angle made with the mouse cursor and then add them together. If that sum is 2*PI then the mouse is inside the hexagon
Logged

raigan
Level 5
*****


View Profile
« Reply #3 on: December 13, 2009, 01:41:06 PM »

There's a really great overview of this problem in Game Programming Gems #7, Article 1.5, "For Bees and Gamers: How to Handle Hexagonal Tiles". I had read all the online resources about dealing with hex grids and nothing clicked until I read the GPG article.
Logged
GAZ082
Level 0
*



View Profile
« Reply #4 on: December 13, 2009, 01:55:16 PM »

There's a really great overview of this problem in Game Programming Gems #7, Article 1.5, "For Bees and Gamers: How to Handle Hexagonal Tiles". I had read all the online resources about dealing with hex grids and nothing clicked until I read the GPG article.

Yeah but, humm, for 60 bucks i'd rather buy a good programming book I'll try to stick to free and freely available solutions! Thanks tho.
Logged
GAZ082
Level 0
*



View Profile
« Reply #5 on: December 13, 2009, 01:56:23 PM »

i believe Battle for Wesnoth rotated it 90 degrees in translation. ie:
Code:
1236
2349
3496
would translate to
Code:
 3 2 1
4 3 2
 9 4 3
6 9 6[code]on a hexagonal map.

Dunno if this helps or not but it might be something to work by.
[/code]

I believe your solution is kinda this: http://playtechs.blogspot.com/2007/04/hex-grids.html

Which i dont get  Facepalm
Logged
kiwi
Level 0
***


View Profile WWW
« Reply #6 on: December 13, 2009, 02:40:52 PM »

No feedback on my reply ? Sad
Logged

LemonScented
Level 7
**



View Profile
« Reply #7 on: December 13, 2009, 03:22:01 PM »

No feedback on my reply ? Sad

I'll give you feedback. You're entirely correct  Grin
Logged

GAZ082
Level 0
*



View Profile
« Reply #8 on: December 13, 2009, 04:15:57 PM »

You could try this
1) See if distance from mouse to center of the hexagon is greater than half the size of the hexagon's diagonal (basically a bounding circle)
2) If no do the same for the rest of the hexagons, if yes then do a convex wrap - for each of the hexagon's points compute the angle made with the mouse cursor and then add them together. If that sum is 2*PI then the mouse is inside the hexagon

hah, sorry for not replying. Well, i though that, to calculate the distance from the screen point to the hex. I read in gamedev that's inefficient, but i'll give it a try (i'm coding it right now).
Logged
Zhal
Level 0
***


View Profile
« Reply #9 on: December 13, 2009, 04:35:33 PM »

http://www.tonypa.pri.ee/tbw/tut25.html

There's one, though I have yet to try it, which I should do like... soon-ish. Tired
Logged
GAZ082
Level 0
*



View Profile
« Reply #10 on: December 13, 2009, 05:18:25 PM »

http://www.tonypa.pri.ee/tbw/tut25.html

There's one, though I have yet to try it, which I should do like... soon-ish. Tired

Looks cool. Could you write the .fla code in a post? Don't have flash to open it.

Thanks!
Logged
droqen
Level 10
*****


View Profile WWW
« Reply #11 on: December 13, 2009, 10:48:19 PM »

I read in gamedev that's inefficient, but i'll give it a try (i'm coding it right now).

Should it matter that much? You're making a simple board game, which one would presume is turn-based and not really all that system-intensive in the first place. I am probably a horrible person for suggesting that you ignore inefficiency, but it's a single call you're making -- you don't even have to do it multiple times per frame, and there's very little else you're going to be handling.
Logged

Christian Knudsen
Level 10
*****



View Profile WWW
« Reply #12 on: December 14, 2009, 02:26:17 AM »

Maybe use a mouse map like suggested at the end of this article?:

http://www.gamedev.net/reference/articles/article747.asp
Logged

Laserbrain Studios
Currently working on Hidden Asset (TIGSource DevLog)
raigan
Level 5
*****


View Profile
« Reply #13 on: December 14, 2009, 07:33:15 AM »

Yeah but, humm, for 60 bucks i'd rather buy a good programming book I'll try to stick to free and freely available solutions! Thanks tho.

Leaving aside the issue of whether or not the GPG books are "good programming books", you could just go to the bookstore and read that one chapter.

Alternately you could just use a square grid.. it's good enough for Chess and Advance Wars Smiley

Logged
GAZ082
Level 0
*



View Profile
« Reply #14 on: December 14, 2009, 09:26:34 AM »

Thanks guys, i'll try Zhal's link. Already contacted the author and he sent me the flash code in a text file. Looks like simple to do.

I'll post my results!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic