I am attempting to create a grid based game. so far i have a hexagonal tile based grid, with a coordinate scheme as shown below:
col 0
| col 1
| | col 2
| | |
__ | __ __ __ __
/00\__/02\__/04\__/06\__/08\__
\__/01\__/03\__/05\__/07\__/09\--- row 0
/10\__/12\__/14\__/16\__/18\__/
\__/11\__/13\__/15\__/17\__/19\--- row 1
/20\__/22\__/24\__/26\__/28\__/
\__/21\__/23\__/25\__/27\__/29\--- row 2
/30\__/32\__/34\__/36\__/38\__/
\__/ \__/ \__/ \__/ \__/ --- row 3
And looks like this in real life just with random colours for each hexagon:
What i am struggling to figure out is, when the user clicks on a hexagon how do i determine what hexagon they have clicked on?
the code i have tried so far is as follows:
private: System::Void MyForm_MouseDown(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
int CloseI=0,CloseJ=0;
CloseJ = FindNearesetX(e->X);
CloseI = FindNearesetY(e->Y);
//Grid[down(y)][along(x)]
P1.X = Grid[CloseI][CloseJ].GetX();
P1.Y = Grid[CloseI][CloseJ].GetY();
} // END MOUSE DOWN EVENT
int FindNearesetX(int ActualX){
int ClosestJPos;
ClosestJPos = ((ActualX-Grid[0][0].GetX())/(1.5*HexSideLength));
return ClosestJPos;
}//END FIND NEAREST X
int FindNearesetY(int ActualY){
int ClosestIPos;
ClosestIPos = ((ActualY-Grid[0][0].getY())/(HexHeight));
return ClosestIPos;
}//END FIND NEAREST Y
private: System::Void MyForm_MouseMove(System::Object^ sender,
System::Windows::Forms::MouseEventArgs^ e) {
this->Invalidate();
P2.X = e->X;
P2.Y = e->Y;
} // END MOUSE MOVE EVENT
This however did not work how i wanted, this is because when the user clicks to the left of the centre point of a hexagon it snaps to the hexagon to the left of the one they clicked, and also if they click above the centre point on all odd columns it snaps to the hexagon above the one they clicked on.
I have been stuck on this one for 2 days now and really want to get it figured out. Thanks