I figured out a way to do it. Make four points to represent each corner of the flattened model, and use a algorithm to make sure that each point is contained in within the hexagon.
Algorithm for checking if point is in hexagon:
1 | function module.IsInsideHexagon(x 0 , y 0 , d, x, y) |
2 | local dx = math.abs(y - x 0 )/d; |
3 | local dy = math.abs(x - y 0 )/d; |
4 | local a = 0.25 * math.sqrt( 3.0 ); |
5 | return (dy < = a) and (a*dx + 0.25 *dy < = 0.5 *a); |
Which is pretty much this, except that I converted it into Lua, and replaced line 5 with
return (dx <= a) and (a*dy + 0.25*dx <= 0.5*a);
because it was rotated the wrong way.
My function for checking if the model is in the bounds of the hexagon:
01 | function IsModelPositionValid(model, hexagon) |
02 | local modelPosition = model:GetPrimaryPartCFrame().Position; |
03 | local modelPosition 2 D = Vector 2. new(modelPosition.x, modelPosition.z); |
05 | local modelSize = model:GetExtentsSize(); |
06 | local modelSize 2 D = Vector 2. new(modelSize.x, modelSize.z); |
08 | local bottomLeftCorner 2 D = modelPosition 2 D - modelSize 2 D/ 2 ; |
09 | local bottomRightCorner 2 D = bottomLeftCorner 2 D + Vector 2. new(modelSize 2 D.x, 0 ); |
10 | local topLeftCorner 2 D = bottomLeftCorner 2 D + Vector 2. new( 0 , modelSize 2 D.y); |
11 | local topRightCorner 2 D = bottomLeftCorner 2 D + modelSize 2 D; |
13 | local isInsideHexagon = functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT * 1 , bottomLeftCorner 2 D.x, bottomLeftCorner 2 D.y); |
14 | isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, bottomRightCorner 2 D.x, bottomRightCorner 2 D.y); |
15 | isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, topLeftCorner 2 D.x, topLeftCorner 2 D.y); |
16 | isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, topRightCorner 2 D.x, topRightCorner 2 D.y); |
18 | return isInsideHexagon; |