PDA

Click to See Complete Forum and Search --> : Hexes in games?


HalC
Feb 19th, 2001, 10:20 PM
Hello Folks,
My first time here and nervous as anything...

What I am trying to do is create a program that will display a map with a hex grid overlay. How do I go about finding an algorithm that will permit me to do this? Also, I'd love to be able to learn about what Active X is and the like. I'd love to be able to convert some of my wargames over to Visual Basic...

For what it is worth, I am something of a newbie to the art and practice of Visual Basic.

Hal

plenderj
Feb 20th, 2001, 02:27 AM
Well.
What you could do is, at design time, make a hexagon with a couple of lines on a form. Use a control array.

Then at runtime, you hide the starting hexagon, and then create more from it.
You would just creates lines at runtime, and then copy the co-ordinates from the original hexagon.
Then you would position them accordingly.

Ya follow ?

- jamie

HalC
Feb 20th, 2001, 10:44 AM
Hello Jamie,
Thank you for your fast response <grin>. While you have indicated that I should create a single hex using lines and the like, I'm afraid I'm going to have to be walked through step by step. Either that, or I am going to have to be pointed in the right direction on how to achieve what sounds like, from your description, a relatively easy task. What I'm wondering about is how to create uniform sized hexes that are perfect in shape - ie all the sides are the same length, and that each angle formed at the vortices are precisely 60 degrees and such...

Hal

plenderj
Feb 20th, 2001, 10:48 AM
Emmm ... I'll do it for you.
Hang on, I'll post some code in a few minutes :)

- jamie

plenderj
Feb 20th, 2001, 10:56 AM
Here ya go.
If you're new to VB, I would not suggest trying to make more of the hexes at runtime.
Just copy and paste the one Ive made and position them accordingly.

You can select the entire hex by drag selecting, and then copy and paste where you want.

- jamie

Sastraxi
Feb 20th, 2001, 04:01 PM
1. Take a hex that will interlock perfectly...
2. Use BitBlt API (if you have programming knowledge previously, you should probably know this one)
3. Bitblt the hex on transparently to picturebox or form and then find some algorithm for finding where they go.... I'll research a bit on this one.

Sastraxi
Feb 20th, 2001, 07:48 PM
HalC, could you say <grin> for us one more time? :)

Hope it works out!

plenderj
Feb 21st, 2001, 02:02 AM
HalC,

I'm not a newbie, but even I got cold feet at the thought of designing an algorithm to create new hexes at runtime.

I would suggest that you just do a lot of copy and pasting, and fill the entire map area with hexes, and then do something like this :


Private Sub Command1_Click()
Dim VarBool As Boolean
Select Case Command1.Caption
Case "Show Grid Overlay":
VarBool = True
Command1.Caption = "Hide Grid Overlay"
Case "Hide Grid Overlay":
VarBool = False
Command1.Caption = "Show Grid Overlay"
End Select
Dim i As Long
For i = 0 To HexLine.UBound
HexLine(i).Visible = VarBool
Next i
End Sub


- jamie

kedaman
Feb 21st, 2001, 03:57 AM
Don't forget line controls will drain up a lot of resources. In case you want variable size hexes, first initialize an array with offset coordinates from center of the hex, which is good for your cpu if you need several of those.
Initialation is done by for corner=0 to 5, h(corner).x=cos(corner*pi1by3)*radius, h(corner).y=sin(corner*pi1by3)*radiuswhere pi1by3=1.047197551

then drawing the hex, with for instance line mehod you would do:

currentx=center.X+h(0).x
currenty=center.Y+h(0).y
for corner=1 to 5
line -(center.X+h(corner).x,center.X+h(corner).y)
next corner

whereas center is where you want to have the hex. A map where the hexes are connected to each other could be achieved by having nested loops incremeanting centerx and centery, and having each second carriage return to add 2'th corner x offset to centerx. Also, you only need to loop trough the 3 first gridlines.

Jotaf98
Feb 21st, 2001, 04:15 AM
Well...the algorythm could be like this: (this will just draw an image in a picturebox, so you should already have the hex image ready instead of lines)



Dim X as Long, Y as Long, Offset as Long

For X = 0 to 4 'Width: 5 hexagons
For Y = 0 to 9 'Height: 10 hexagons
If Offset = HexWidth/2 Then
Offset = 0
ElseIf Offset = 0 Then
OffSet = HexWidth/2
End If

PaintPicture HexImage.Picture, Offset+(X*HexWidth), Y*(HexHeight/2)
Next Y
Next X



I haven't tested it, but it should work. Also the variables' names are very self-explanatory, so I'm not gonna explain each of them ;)

plenderj
Feb 21st, 2001, 04:18 AM
'cept creating them at design time is a lot quicker than designing an algorithm to make them.

ah who cares anyway.

- jamie

HalC
Feb 21st, 2001, 02:18 PM
Hi Folks,
Again, I am very thankful with the responses I've been getting on this. As I mentioned, I will be exploring all avenues presented thus far so as to gain a working knowledge on this topic. I've been trying to cut and paste with the hex that Jamie kindly sent me, and I found it was more difficult than I had expected it would be. When I click on all the lines and get 6 white squares showing at the vortices - I hit the control C command. When I tried repositioning it via the control V command, it didn't work exactly as I'd expected. To quote Thomas Edison - learning what doesn't work is just as valuable as learning what does work.

Thanks Guys,
Hal

plenderj
Feb 22nd, 2001, 03:44 AM
Well take a look at the attachment above, that has a matrix of hexes done ...

- jamie

Jotaf98
Feb 23rd, 2001, 11:27 AM
Hum... I tried my algorythm and it doesn't work. :(

I'm making a new one right now. I'm gonna finish it when I finally get home :)

BTW, what do you want the hexes for? ;)

Feb 23rd, 2001, 01:33 PM
Another way to do it is to create a Hex Class, then use kedamans drawing algorithm. Each object would have a center point, and would draw a hex with a set distance to each vertex from the center. Have a Canvas Property, so you know where you are drawing it to, and thats all. It would look something like this:

Set MyHex.Canvas = form1
MyHex.Dist = 300 'Twips
MyHex.CenterX = 500
MyHex.CenterY = 500
MyHex.Draw


If anyone wants me to write this, just post =)

HalC
Feb 23rd, 2001, 03:06 PM
Hi,
I've been trying each method to see what it takes to create hexes. Tonight, I will do the math to see what is involved in creating a hex <grin>. As for why I wanted the hexes? What I am working on is a Visual basic version of TRAVELLER, a role playing game. Hey Zaei? could you post the code you alluded to? I'm seeing something you wrote that isn't familiar to me. Jamie? I finally learned how to move an entire entity - so thanks for the hexes you posted. It was extremely helpful.
I think ultimately, what I will do is create a row of hexes (larger than Jamies, but in line with what he did) that measure 10 hexes wide by 12 hexes deep. Each hex will have a circle in the center that can be made visible or not. Each hex will contain a hexid box at the top of the hex I can change as needed, and a "star name" at the bottom of the hex as needed. This way, I can change what goes in what hex when I need it.
Again, without you guys helping, I'd have never known what to look for when it came to learning about graphics...

HalC

Jotaf98
Feb 23rd, 2001, 05:27 PM
Here's a little function to draw hexagons, along with a small demonstration. It has lots of comments but most of it was trial-and-error, so they're not much help :)

DarkJedi9
Feb 23rd, 2001, 07:54 PM
Zaei, I'm afraid Hal won't be too familiar with classes in VB, if he is a newbie.

Hal, to better understand what Zaei is alluding to, check out some of the COM tutorials by Karl Moore. Just search the articles at the VB-World home page. While not strictly a class tutorial, they do explain a little about classes.

Feb 26th, 2001, 12:58 PM
I know he's a newbie, but this is good time to learn, no?

Here is the class. To warn everyone, i didnt have enough time to actually get it to work. As it stands, it ill only darw a lopsided septagon. There is a long comment in the "Draw" method for instructions on replacing my algorithm. Anyway, some instructions for Hal:
to use this class, do this, in a module, or in the Declarations secton of a form, after you add the file to your project:

Dim Hex as New clsHex


Then, while you are programming, just use "Hex" just as you would any textbox, or label.

Before using the "Draw" method, you MUST do these things:

Set Hex.Canvas = <form, or picturebox>
Hex.PointY = <number>
Hex.PointX = <number>
Hex.dist = <number>


Thats it, here you go!

DarkJedi9
Feb 26th, 2001, 02:41 PM
After the Dim statement, does he also have to do
Set Hex = New clsHex?

Jotaf98
Feb 26th, 2001, 04:02 PM
Hey, Zaei, can you modify my algorythm so it draws hexagons with yours (because currently it uses an image)? ;)

HalC
Feb 26th, 2001, 06:48 PM
Hello Folks,
I've downloaded the code that Zaei posted, and found that in the sink or swim method of learning, I've got lead feet!
I've looked at the class module that was posted, and I haven't a clue as to what any of that stuff means. Does anyone have any recommendations as to a good VB6 book that describes all this stuff - perhaps I can attack the game or go down in flames...

Hal

plenderj
Feb 27th, 2001, 02:54 AM
HalC,

A while ago, I decided I was going to learn how to program. The year previous, my parents had bought me the Student's Visual Studio cd pack.
It (essentially) comes with C++, J++ and VB.

So I rooted out the CDs.
I decided that I would learn C++.
So I put the CD into the CD-ROM drive, but the CD was bad. So I decided I was going to learn Java. I put that CD into the drive, and it was bad too.
So I was left with VB, and it worked, and here I am now :)

I didn't know any programming when I installed VB.
To learn VB, I tried to write a game.
http://members.fortunecity.com/plenderj/

If you look through the versions, you'll see that the code gets more and more complex. Thats what I would suggest you do. Just make a very simple game, and then build upon that.

You can rip any of my code if you'd like.

- jamie

kedaman
Feb 27th, 2001, 06:19 AM
I suggest you forget about object oriented programming. VB access to com objects is extreemly slow and should not be used frequently, especially not in a rendering process for a game. VB's not the right language for games either, so if you know C++ better, stick to it, other than that, i'm not saying it's impossible, games only have a few really time critical parts, that should be subsituted by C++ or assembler code for reasonable performance.

About this hex grid, skip the line controls and hex rendering classes, and make a simple rendering procedure with nested loops.

Feb 27th, 2001, 12:00 PM
Jedi: No, you dont have to do that Set, because i already defined the variable as New. It saves you a statement.

Jotaf: Sure, I can do that a bit later, it'll be a few hours, Ive got some stuff to get finished first.

Hal: You dont even need to take a look at the code. Just add it, and copy the code i posted to initialize it. As you learn more about all of this, you can get into the "Why?" of it all, and learn to make your own classes. Just keep imagining the textbox as you program with it =P

kedaman: It may be slow, but once you get the hang of it, it makes things a ton easier =)

kedaman
Feb 27th, 2001, 01:18 PM
Zaei: It doesn't make things a ton easier, it keeps your code organized, in C++ that is an advantage but in vb you loose performance. New statement should never be used in declarations, each access to that object requires extra performance. So Set object=new class is the way to go.

Feb 27th, 2001, 03:53 PM
I know kedaman, everytime you do a Dim X as New Y, it has to check to see if X is already valid. As for the class thing, you are right, as to the performance hit.

ElevenToes
Mar 2nd, 2001, 01:52 PM
HalC

I am also new to VB and have been following this
discussion because I am trying to do the same
thing but for a different game.

I found this tutorial on another site.
It was very helpful to me and I thought it might be to
you also.

Look at the tiling games tutorial
http://www.vbexplorer.com/graphicstutorials.asp