tag:blogger.com,1999:blog-5052387.post3668616327412097478..comments2020-11-26T09:50:15.215-08:00Comments on Blobs in Games: Hex grids: code generatorAmithttp://www.blogger.com/profile/12159325271882018300noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-5052387.post-58839636095102185542015-04-11T08:51:14.146-07:002015-04-11T08:51:14.146-07:00Thanks Chris! The algorithms (including pixel to h...Thanks Chris! The algorithms (including pixel to hex) should work with any language but I'm also working on some code samples in various languages, including C++.<br /><br />Amithttps://www.blogger.com/profile/12159325271882018300noreply@blogger.comtag:blogger.com,1999:blog-5052387.post-75624776627958896322015-04-11T08:28:35.476-07:002015-04-11T08:28:35.476-07:00Hi,
I have recently started developing a game usi...Hi,<br /><br />I have recently started developing a game using a hexgrid and the problem of converting mouse coordinates to a grid location has hassled me for 2 days now.<br /><br />I tried working with code examples from your website unfortunately to no success (I am working with c++ and none of the information I needed was in c++).<br /><br />I finally worked it out today and I thought I would share my code with you in hope that you may be able to help provide other people with good solutions.<br /><br />I am aware the following code is quiet sloppy but all my testing has shown it works.<br /><br />My grid layout is <i>“odd-q” vertical layout</i> and the following code only works for that grid type.<br /><br />Note: sf::Vector2i is from the SFML library and refers to an class that holds an x and y int.<br /><br /><i><b><br />sf::Vector2i HexagonGrid::PixelToHexgrid(double x, double y) {<br /><br /> double w = (hex_radius*2)*0.75; //<br /> double h = (hex_radius*2)*0.86; //So currently I have worked out height is 86% of the radius, dodgy? I don't know..<br /><br /> int xgrid = floor(x/w); //What column is it roughly in?<br /> int splitx = xgrid*w; //Bring this back to a pixel coordinate, but it's now rounded to the column<br /><br /> if ((int)floor(x/w) & 1) //Is it an odd column?<br /> {<br /> y-=h/2; //Shift the y value upward to compensate for the odd column<br /> }<br /> <br /> double yp = ((int)y % (int)h)/h; //Percentage of way down y row 0-1 (0% to 100%)<br /> double zigzag = abs((yp*2)-1); //Convert yp into a range of 0-2 then -1 to 1 and finally abs it so we get a wrapped result of 0 - 0.5 - 1 (same as 0)<br /><br /> splitx += zigzag*(w/3); //Multiply the zigzag by 1/4 of the pentagon, or with the current w, 1/3 is 1/4<br /><br /> if ((((int)x % (int)w)/w)<0.33) //Is the point on the join between cells<br /> {<br /> if (splitx > x) //Is the current point in the zigzag (join) larger than the pixel x?<br /> {<br /> if (xgrid & 1) //Was the old result a odd column?<br /> y+=h/2; //Remove the shift from earlier<br /> else<br /> y-=h/2; //Add a shift because it is now odd<br /> xgrid-=1; //Push left one because this isn't really in the next column<br /> }<br /> }<br /><br /> int ygrid = floor(y/h);<br /><br /> return sf::Vector2i(xgrid, ygrid);<br />}<br /></b></i><br /><br />Keep up the good work :) and thanks for the ideas on how to tackle this problem<br /><br />Becareful not to let pretty diagrams draw away from the information, I found a few times your interactive diagram was absolutely amazing but the information with it was mostly just confusing and involved jumping to other parts of the page to find out what a function wasAnonymoushttps://www.blogger.com/profile/04871348267106868723noreply@blogger.comtag:blogger.com,1999:blog-5052387.post-15323668031432079652015-03-30T07:25:40.489-07:002015-03-30T07:25:40.489-07:00I am enjoying your posts. Good luck!I am enjoying your posts. Good luck!Craighttps://www.blogger.com/profile/09499915714489062423noreply@blogger.com