Jump to content
  • Advertisement
Sign in to follow this  

Caculating triangular grid position

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am developing a puzzle game and I have run into a bit of a problem. In order to do the various shapes (hexagon,large triangle, small triangle, trapazoid, ect) I needed a triangular grid. After searching around on the internet for some time, I found little to no information on how to caculate the positions needed for a triangular grid. I however managed to adapt the formula used for hexagon positioning (from one of the articles on this site) My adapted formulas are: PlotX=a*(width*0.85)+(b&1)*(width*0.85); PlotY=b*((height*2)*0.75)+(a&1)*(height/2); where a and b are the Map positions. Then in order to "draw" the grid I use this: FlipAng=180*(a&1); For(s=0;s<3;s++) { x=(PlotX+(width/2))+Cos(GetRadian(s*Angle+Rot+FlipAng))*width; y=(PlotY+(height/2))+Sin(GetRadian(s*Angle+Rot+FlipAng))*height; x2=(PlotX+(width/2))+Cos(GetRadian((s+1)*Angle+Rot+FlipAng))*width; y2=(PlotY+(height/2))+Sin(GetRadian((s+1)*Angle+Rot+FlipAng))*height; myWin.Line(x,y,x2,y2); } Basically every other position I flip the triangle so that I get something that looks like this: Now my problem is that I need to be able to convert x,y coordinates (like mouse coordinates) into the MapX and MapY coordinates (a,b) in order to use an array. Can someone point me in the right direction to go about this? I can't really use a mask like in the isometric tutorial becuase the triangles flip up and down.... One way I could go about it is to loop through the entire number of tiles, recaculate all the points that make up a triangle and then test to see if the mouse x/y is inside the polygon using the sum=2pi method , however this seems very slow (becuase of the trig) and I also figure that there must be some kind of purely mathmatical way to turn the x/y mouse coordinates into map positions. Thanks for the help!

Share this post


Link to post
Share on other sites
Advertisement
For this problem I would use barycentric coordinates, or the parametric representation V0+uE0+vE1. If you need help with the math, I'd be glad to post details tomorrow. The only thing I'm not quite clear on is what 'a' and 'b' are, and how they map to the triangle grid. (It's probably obvious from the equations you posted, but I couldn't pick it up at a glance...)

Share this post


Link to post
Share on other sites
Yes, help with the math would be great. A and B are the cell coordinates. So the very first cell would be A=0 and B=0 and then the cell to the right of that would be A=1 B=0 , ect.

Share this post


Link to post
Share on other sites
At first I rejected the mouse map idea due to the triangles flipping up and down... However now I believe that I can infact make the mouse map work..

Basically I need to make a grid so that every other tile has a box (so one box will intersect three tiles, 1 right side up and 2 down but only half way)

Then I make a mouse map and test to see if the mouse pointer is over the blue area (then we stay at the xindex*2,yindex) otherwise if it is red then we do xindex*2-1 (red fills the left side) ... if it is yellow then we do xindex*2+1 (yellow fills the right side)

This SHOULD work, I am 90% sure I can code the formulas and such to make this method work. IF it does work, I will repost here so that other people can learn how to do it as well :).

This way is not purely mathmatical but it also doesn't involve huge amounts of looping or trig heh.

Thanks for the help so far :).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!