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:
function module.IsInsideHexagon(x0, y0, d, x, y)
local dx = math.abs(y - x0)/d;
local dy = math.abs(x - y0)/d;
local a = 0.25 * math.sqrt(3.0);
return (dy <= a) and (a*dx + 0.25*dy <= 0.5*a);
end
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:
function IsModelPositionValid(model, hexagon)
local modelPosition = model:GetPrimaryPartCFrame().Position;
local modelPosition2D = Vector2.new(modelPosition.x, modelPosition.z);
local modelSize = model:GetExtentsSize();
local modelSize2D = Vector2.new(modelSize.x, modelSize.z);
local bottomLeftCorner2D = modelPosition2D - modelSize2D/2;
local bottomRightCorner2D = bottomLeftCorner2D + Vector2.new(modelSize2D.x, 0);
local topLeftCorner2D = bottomLeftCorner2D + Vector2.new(0, modelSize2D.y);
local topRightCorner2D = bottomLeftCorner2D + modelSize2D;
local isInsideHexagon = functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT * 1, bottomLeftCorner2D.x, bottomLeftCorner2D.y);
isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, bottomRightCorner2D.x, bottomRightCorner2D.y);
isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, topLeftCorner2D.x, topLeftCorner2D.y);
isInsideHexagon = isInsideHexagon and functions.IsInsideHexagon(hexagon.Position.x, hexagon.Position.z, HEXAGON_HEIGHT, topRightCorner2D.x, topRightCorner2D.y);
return isInsideHexagon;
end
1 comment
Well the problem with that, is that even though it will rest mostly on the designated hexagon, it can still extend into other hexagons if it is on the outskirts. Check the image that I posted to see what I mean