Finding midpoints of edges on a hexagonal mesh.

Please see image below to see the positions I’m trying to locate.
And now… suffer with my English attempt to describe code idea. And then critique, knock yourself out, bash my stupidity. There’s more where it came from.

Locate the southern most vertices     (shud b 8)
Find most west of the 8    (shud b 2)
SouthWestPositiveZ = highest on Z of 2
SouthWestMinusZ = the other vert

Locate the two most westerly vertices // shud only b 2
WestPositiveZ = highest on Z
WestMinusZ = the other vert
Store the other as WestZ-

SWmidpointPositiveZ.x = (WestPositiveZ.x + SouthWestPositiveZ.x) / 2
SWmidpointMinusZ.x = (WestPostiveZ.x + SouthWestMinusZ.x) / 2
Go to pub.

To some questions before I attempt to fuck my brain by figuring out how to write this in UnityScript or C#

Am I right about this method?

Is there a better way to do this if I have a couple hundred of these hexagons but only need any one of these midpoints at a time for player input?

http://i55.tinypic.com/2cie7x5.jpg![](http://i55.tinypic.com/2cie7x5.jpg)

What data are you starting with? (e.g. What is the pivot point, is the mesh procedurally generated, are they always aligned the same way with the world, do you know any world position vertex positions?)

It might be easiest to start with one point, and rotate it by a multiple of 60 degrees, if the mesh’s pivot point is in the center.

pivot point is in the center.

Mesh is a model from 3ds. (ie the pivot point is in the center as max defined it)

Meshes are not always aligned in the same way relative to the world, and subject to change by physics forces, so world position not exactly known.

Is the hex radius (center/pivot to where edges meet) known? You have a 60 degree angle at each internal angle if I remember my hex construction class properly, so finding the mid should just be a simple cosine.

@Fiddle

Hex radius varies. However there’s no “squishing” of the hexes, so yes, the 30/60 angle thing holds for all vert to edge relationships.

@Fiddle sorry, about the time they started on about cos and sin in maths I was focusing most of my attention on learning to communicate with the fair gender. Much to their horror, I’m sure.

So I have a vague idea of how cos and sin work. But it’s VERY fucking vague. I pinched code to put things in a circle recently. And couldn’t honestly tell you a thing about what it does or how it does it.

So let me postulate a little.

you’re saying…?

Find any vert
Find the center
30 +/- degrees from that relationship, project out from center
Until hitting the "edge"...

Where I’m complete screwed there is in trying to figure out where that “edge” would be in terms of its x.y depth from the center. And I guess that’s where cos/sin comes in somehow. but have no clue.

The other problem, as I see it. The center of the mesh is not at the same Z as the edge… but I can see how to get around that, just move the center up to the same before figuring shit out. Maybe…

With the pivot point at the center, you can start with two edge midpoints, easily. (We’re working in local space, at first.) mesh.bounds.extents.y will be the Y value for each, and zero will be the X value for each. Z will be mesh.bounds.extents.z on one side, and -mesh.bounds.extents.z on the other.

Then, you can rotate those points. Quaternion.AngleAxis should do nicely – you construct it using 360 divided by the number of sides you have (60 degrees, here), and (0,0,1) – the Z axis. Then, you multiply the two edge midpoint positions by the result. (Multiplying a quaternion into a Vector3 is how you rotate a point.)

Finally, if you want the positions in world space, use transform.TransformPoint.

Clear as mud?

excactly like mud. Deep, thick gooey mud.

I’ve nearly no idea what that means. Any of it.

The axis flying thru the center of the ring is Z. sorry, should have stuck a gizmo in there for clarification.

hang on… I think I get what you’re saying…

Find the midpoint of the Northern or Southern Edge FIRST? Right?

Then rotate that 60 degrees around the center…

And that should be the center of the next edge… which ever way you go.

Is that right?

Edited to use that.

Precisely right.

but still completely fucking confused by this:

" and then you multiply the two edge midpoint positions by the result."

why are you multiplying anything, surely after the rotation, we’re in that position? And what result are you talking about?

after your edit I have a better idea why the multiplication… well, not really, I’d assume that rotating something would give the position of the object after rotation, but obviously that’s not true, more work must be done… which is the multiplication? right?

How does anyone know this stuff?

Sorry, that part could be clearer. Multiplying the two initial midpoints by that quaternion will only yield two new points. To get the remaining points, you can either keep rotating the new results by the original quaternion, or you can create five quaternions, and multiply the original two midpoints by each of those.

When you rotate a point, you take some amount of each axis of its distance from the origin. (That’s why rotating Vector3.zero is useless, because it has no amount of any distance along any axis to work with.) Multiplication is how we express that “taking a portion of”, mathematically. There generally isn’t a need to actually understand the math behind what’s going on, but it’s helpful to at least understand that concept.

Curiosity is the spark. Then, thought, practice, experimentation, and recording come into play, for communication and learning. I admit that we have a long way to go before this stuff is easy to learn, for everyone. The best place I’ve found for it, so far, was a classroom with a teacher, but I don’t have money to consistently be taking that approach.

do a physical method…

Make an empty GameObject(GO) in the center of your mesh. lets parent it to it for the sake.

var centerObject=GameObject();
centerObject.transform.position=transform.position;
centerObject.transform.parent=transform;

Now find the extents of the mesh… (extents are half the size…)

var extents=gameObject.GetComponent(MeshFilter).mesh.bounds.extents;

Since it is half the size, we want to find the shortest of the X an Z values. We need Vector3’s in the end, so lets create them here. This will also hold direction

var distance=Vector3(extents.x,0,0);
if(extents.z < extents.x) distance=Vector3(0,0,extents.z);

// nifty javascript function for this:
var distance = extents.x > extents.z ? Vector3(0,0,extents.z) : Vector3(extents.x,0,0);

now create another game object parent that to the original and offset it

var myObject=GameObject();
myObject.transform.parent=centerObject.transform;
myObject.transform.localPosition=distance;

The trick is to get the points of the side… so we rotate the center and collect the information.

for(var i=0; i<6; i++){
	centerObject.transform.localEulerAngles.y=i * 60;
	print("Center of side is at " + myObject.transform.position);
}

OH, but that isn’t enough… lets use some trickery…

Create 6 Game Objects labeled 1 to 6 that can act as normals for each side.

for(var i=0; i<6; i++){
	var myObject=GameObject();
	myObject.name="side-" + (i+1);
	myObject.transform.parent=centerObject.transform;
	myObject.transform.localPosition=distance;
	
	// this forces the side to be a normal
	myObject.transform.LookAt(centerObject.transform.position);
	myObject.transform.Rotate(Vector3(0,180,0));

	centerObject.transform.localEulerAngles.y=i * 60; 
	
	myObject.transform.parent=null; // detach it
	centerObject.transform.localEulerAngles.y=0; // turn the center  back to zero
	myObject.transform.parent=centerObject.transform; // reattach it
}

I know… everyone is probably looking at this like… WTF, you can do this mathematically… You can… but you can also do it the cheap way… lol

stay with me on this…

I might have figured this out: I’m going to need learn and experiment a little. alright, a lot.

But one last question… in your worldly knowledge, do you think this is the best way of going about this?

If these are Regular hexagons (ie: all 6 line segments are the same length) all you need is its Bounding Box (mesh-aligned) to find the points. the rest is just averaging the points.

You only need to find 1 point and the rest can assume to be mirrored ( *-1) about either the X or Y axis if the center of the hexagon is at the origin.

540301--19055--$hexagon_math.jpg

I think I’ve just fallen in love with Hexagons. I only chose the shaped because she looked good. But apparently she’s more than edge deep, perfectly formed, in an intellectual sense.

Theres a Hot for Words
http://www.youtube.com/user/hotforwords

but no hot for math :frowning: not yet anyway. But math and geometry are easy to fall in love with :slight_smile:

Ha-lo! my leetle unitoyz!

?