GAZ082
Level 0
|
|
« 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
|
|
« Reply #1 on: December 13, 2009, 12:15:04 PM » |
|
i believe Battle for Wesnoth rotated it 90 degrees in translation. ie: would translate to 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
|
|
« 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
|
|
« 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
|
|
« 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
|
|
« Reply #5 on: December 13, 2009, 01:56:23 PM » |
|
i believe Battle for Wesnoth rotated it 90 degrees in translation. ie: would translate to 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.htmlWhich i dont get
|
|
|
Logged
|
|
|
|
kiwi
|
|
« Reply #6 on: December 13, 2009, 02:40:52 PM » |
|
No feedback on my reply ?
|
|
|
Logged
|
|
|
|
LemonScented
|
|
« Reply #7 on: December 13, 2009, 03:22:01 PM » |
|
No feedback on my reply ? I'll give you feedback. You're entirely correct
|
|
|
Logged
|
|
|
|
GAZ082
Level 0
|
|
« 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
|
|
|
|
|
GAZ082
Level 0
|
|
« Reply #10 on: December 13, 2009, 05:18:25 PM » |
|
Looks cool. Could you write the .fla code in a post? Don't have flash to open it. Thanks!
|
|
|
Logged
|
|
|
|
droqen
|
|
« 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
|
|
|
|
|
raigan
|
|
« 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
|
|
|
Logged
|
|
|
|
GAZ082
Level 0
|
|
« 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
|
|
|
|
|