Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make sure that a model is contained in one and only one hexagon?

Asked by 5 years ago
Edited 5 years ago

So I'm making RTS game which has a map made of hexagons, and any buildings that the player may place can only be on one single hexagon, meaning they can't extend out into other hexagons.

https://imgur.com/a/BAwPmER

Any help is much appreciated

EDIT: Here's a crudely-drawn image I made to better represent the problem. https://imgur.com/a/0lewfPl

2 answers

Log in to vote
0
Answered by 5 years ago
Edited by User#24403 5 years ago

You can try checking if Mouse.Target is an allowed hexagon he can place in before placing the building/unit.

Mouse.Target is going to be a part (nil if pointing to the sky or just not a part in general), in your case, probably the Hexagon the player is pointing to, you could have Bool values inside of each hexagon stating if the user is allowed to place a unit/building there, or something along those lines.

0
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 davidgingerich 603 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

Answer this question