-
What do you want to achieve?
I want to rotate the pattern on the grid (the field) without having holes in it. -
What is the issue?
The issue is that sometimes holes are appearing in the pattern. This is especially visible at/or around 45°, 135, 225° and 315°. Please watch the video.
local patternData = {
{0, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{1, 1, 1, 2, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 0}
}
local SEGMENT_SIZE = 1
local SEGMENT_TRANSPARENCY = 0.6
local function applyPattern(Object:Model, PatternContainer:Model, distance)
local PatternRootPart:Part
if Object:FindFirstChild("PatternRootPart") then
PatternRootPart = Object.PatternRootPart
elseif Object:FindFirstChild("HumanoidRootPart") then
PatternRootPart = Object.HumanoidRootPart
else
warn("No PatternRootPart found!")
return
end
if distance then
PatternContainer.PrimaryPart:PivotTo(PatternRootPart.CFrame * (CFrame.new(-Vector3.new(0, 0, distance * 2))))
else
PatternContainer.PrimaryPart:PivotTo(PatternRootPart.CFrame)
end
local newWeld = Instance.new("WeldConstraint")
newWeld.Part0 = PatternContainer.Origin
newWeld.Part1 = PatternRootPart
newWeld.Parent = PatternContainer.Origin
PatternContainer.Origin.Anchored = false
end
local function translateToParts(patternData)
local PatternContainer = Instance.new("Model")
PatternContainer.Name = "PatternContainer"
PatternContainer.Parent = workspace.GlobalPatternContainer
local origin = Vector3.new(0, 0, 0)
for Y = 1, #patternData, 1 do
for X = 1, #patternData[Y], 1 do
if patternData[Y][X] == 1 then
local newPart = Instance.new("Part")
newPart.Name = X.."-"..Y
newPart.Size = Vector3.new(SEGMENT_SIZE, 2, SEGMENT_SIZE)
newPart.Position = Vector3.new(2 * X, 0, 2 * Y)
newPart.Anchored = true
newPart.CanCollide = false
newPart.Transparency = SEGMENT_TRANSPARENCY
newPart.Massless = true
newPart.CastShadow = false
newPart.Parent = PatternContainer
elseif patternData[Y][X] == 2 then
local newPart = Instance.new("Part")
newPart.Name = "Origin"
newPart.Size = Vector3.new(SEGMENT_SIZE, 2, SEGMENT_SIZE)
newPart.Position = Vector3.new(2 * X, 0, 2 * Y)
newPart.Anchored = true
newPart.CanCollide = false
newPart.Transparency = SEGMENT_TRANSPARENCY
newPart.Massless = true
newPart.CastShadow = false
newPart.Parent = PatternContainer
PatternContainer.PrimaryPart = newPart
end
end
end
for _, segment in pairs(PatternContainer:GetChildren()) do
if not (segment.Name == "Origin") then
local newWeld = Instance.new("WeldConstraint")
newWeld.Part0 = segment
newWeld.Part1 = PatternContainer.Origin
newWeld.Parent = segment
segment.Anchored = false
end
end
return PatternContainer
end
local PatternContainer:Model = translateToParts(patternData)
applyPattern(workspace:WaitForChild("ByGermanKnight"), PatternContainer, 5)
task.wait(1)
local Field = game.Workspace.Fields.Testfield
local function findPatternFieldCoordinates(PatternContainer, Field)
local Border:Part = Field.Border
local PatternCoordinatesInField = {}
for _, segment in pairs(PatternContainer:GetChildren()) do
local objectSpaceCFrame = CFrame.new(segment.Position):PointToObjectSpace(Vector3.new(
Border.Position.X + (Border.Size.X / 2),
Border.Position.Y + (Border.Size.Y / 2),
Border.Position.Z + (Border.Size.Z / 2)))
local X = math.floor(objectSpaceCFrame.X / 2) + 1
local Z = math.floor(objectSpaceCFrame.Z / 2) + 1
if X < 0 then
X = -X
end
if Z < 0 then
Z = -Z
end
table.insert(PatternCoordinatesInField, {X = X, Z = Z})
end
return PatternCoordinatesInField
end
while true do
local PatternCoordinatesInField = findPatternFieldCoordinates(PatternContainer, Field)
for _, Coordinates in pairs(PatternCoordinatesInField) do
local X = Coordinates.X
local Z = Coordinates.Z
if Field.Flowers:FindFirstChild("FP-"..X.."-"..Z) then
Field.Flowers:FindFirstChild("FP-"..X.."-"..Z).TopTexture.Color3 = Color3.fromRGB(170, 0, 255)
else
print("Flower not found")
end
end
task.wait()
for _, Coordinates in pairs(PatternCoordinatesInField) do
local X = Coordinates.X
local Z = Coordinates.Z
if Field.Flowers:FindFirstChild("FP-"..X.."-"..Z) then
Field.Flowers:FindFirstChild("FP-"..X.."-"..Z).TopTexture.Color3 = Color3.fromRGB(255, 255, 255)
end
end
end