Glen Morse
Glen Morse

Reputation: 2593

Drawing a hexagon

Using Firemonkey XE2 , I was able to use the TCube component to create a cubed map, But I am now wanting a hexagon map. There was no option for a 3d hexagon shape i could find. I figured you could create a THex similar to the TCube but I have not been able to do this or even get close. Can anyone supply a sample of doing this?

The shape I'm looking for is a hexagon prism.

enter image description here

Upvotes: 3

Views: 1826

Answers (3)

Loghman
Loghman

Reputation: 1499

I am doing the same thing that you are doing and developing a strategic game with a hexagon map in Delphi.

You have the object that you need in Delphi itself and that is TCylinder. You should set SubdivisionAxes from 12 to 6 (12 is default for this object) and that is all you need to have the object that you want.

for implementing your map I suggest you to check this link as well. http://www.redblobgames.com/grids/hexagons/

good luck.

Upvotes: 4

James
James

Reputation: 9985

You'll need to derive from TCustomMesh and override Render to pass in you calculated vertices.

Start with a center and a radius and the points are as follows. Assuming the shape is constructed parallel to a plane and subsequently transformed. The following creates a vertical Hexagonal Prism (I have no IDE atm and no way of testing this!).

ClearPoints();
prismEnd := -1;
while prismEnd < 2 do
begin
    Z := Center.Z + (prismEnd * length)
    angle = 0;
    AddPoint(0, 0, Z);
    while angle < 360 do
    begin
        X := Center.X + (radius * Cos(DegToRad(angle)));
        Y := Center.Y + (radius * Sin(DegToRad(angle)));
        AddPoint(X, Y, Z);
        Inc(angle, 60);
    end;
    Inc(prismEnd, 2);
end;

For the 6 values this creates the TexCoords should be

Tex X   Tex Y 
1       0.5
0.75    1
0.25    1
0       0.5
0.25    0
0.75    0

You'll need 24 Triangles to render this, which depending on your draw method could require up to 72 indices.

but that will depend on how you map your textures.

I found this link which has examples of inheriting and using TCustomMesh

This should in theory provide a shape such as

enter image description here

Upvotes: 3

VitaliyG
VitaliyG

Reputation: 1857

You can use TMesh to create whatever shape you need.

Use the Data property to specify the points, normals and textures for each point, and the order in which the resulting triangles are drawn.

All you need is precalculated points and normals for desired hexagon shape (I guess this can be found with google, or created in specialized shape editor)

Upvotes: 1

Related Questions