03-16-2002, 12:31 PM
I am trying to create a map made up of many hexagons (or even
just squares) and allow the user to click and drag a sprite/icon to any point on the map. The sprite/icon would then "snap to grid"
on the hexagon map. Im thinking kinda like the old stratagy war games like "Panzer Blitz" or "Axis and Allies" and such.
Any ideas on how to do this?
Or maybe an example game source code that would be similar to this?
03-16-2002, 12:39 PM
You use trigonometry to find the distance between the point where the icon is 'dropped' and the centre point of each hexagon. The closest hexagon gets the icon.
Linear distance between two X,Y coordinates:
'Getting a distance, thanks to Lucky's VB for this :-)
Private Function GetDist(intX1 As Single, intY1 As Single, intX2 As Single, intY2 As Single) As Single
'Return the distance between the two points (I love you, Mr. Pythagoras)
GetDist = Sqr((intX1 - intX2) ^ 2 + (intY1 - intY2) ^ 2)
03-17-2002, 09:22 PM
You don't need to use the sqr() function there. You are not interested in the actual distance, just which is smallest. If you sqr() a big number and a small number, the small number will still be smaller.
Just a thought, as the sqr() function can be a bit mucky, particularly if you are storing the result as an integer, because that forces a data type change as well.
03-18-2002, 07:51 AM
If you are using squares you could probably do something like this in the DragDrop procedure:
Source.Move X / WidthofSquares * X, Y / HeightOfSquares * Y
'Width and HeightofSquares is the dimensions of the square
this would make the object line up with the squares and hopefully "snap to the grid".:)
03-20-2002, 03:46 PM
Here is a quick example of dragging and dropping tiles onto a hexagonal grid.
I didn't bother using SQR since a hexgrid is pretty symetrical so only some simple rounding is required....
03-21-2002, 06:55 PM
Thanks for the input, everyone.
But I'm having this problem.....
I have made several small picture boxes inside of a large picture box The small boxes can be moved around the hex map using the DragDrop property of the large picture box. When I make a picture box in the form outside of the larger box (not contained in the large picture box ) I have trouble DragDrop the new picture control into the larger picture box. It always seems to reposition the new control in the form instead.
Is there a way to create a case or if statement that can test if the new control originated from outside the large picture box?
03-21-2002, 11:13 PM
I found the Container statement to be usefull here
Private Sub picMap_DragDrop(Source As........etc)
If Source.Container = frmFrame Then
Set Source.Container = picMap
Source.Top = 100
Source.Left = 100
Source.BackColor = &H8080&
This works really slick.