Hey I’m making a realistic game that has breakable glass, but I need help calculating where to place triangles for the glass. My code below divides the glass into 4 squares around the Breaker point but I need help converting them into triangles
local main = script.Parent.Parent
local glass = script.Parent
local breaker = main:WaitForChild("Breaker")
local glassCF = glass.CFrame
local glassSize = glass.Size
local localHit = glassCF:PointToObjectSpace(breaker.Position)
-- Compute the 4 quadrant sizes and positions
local function CreateShard(pos, size)
local part = Instance.new("Part")
part.Name = "Shard: ".. tostring(pos)
part.Material = glass.Material
part.Transparency = glass.Transparency
part.Anchored = true
part.Size = size
part.CFrame = glassCF:ToWorldSpace(CFrame.new(pos))
part.TopSurface = Enum.SurfaceType.Smooth
part.LeftSurface = Enum.SurfaceType.Smooth
part.RightSurface = Enum.SurfaceType.Smooth
part.BottomSurface = Enum.SurfaceType.Smooth
part.BackSurface = Enum.SurfaceType.Smooth
part.FrontSurface = Enum.SurfaceType.Smooth
part.Parent = workspace.Ignored_Parts
end
-- Compute bounds
local x1 = -glassSize.X/2
local x2 = localHit.X
local x3 = glassSize.X/2
local y1 = -glassSize.Y/2
local y2 = localHit.Y
local y3 = glassSize.Y/2
-- Shard 1 (top-left)
CreateShard(
Vector3.new((x1 + x2)/2, (y2 + y3)/2, localHit.Z),
Vector3.new(math.abs(x2 - x1), math.abs(y3 - y2), glassSize.Z)
)
-- Shard 2 (top-right)
CreateShard(
Vector3.new((x2 + x3)/2, (y2 + y3)/2, localHit.Z),
Vector3.new(math.abs(x3 - x2), math.abs(y3 - y2), glassSize.Z)
)
-- Shard 3 (bottom-left)
CreateShard(
Vector3.new((x1 + x2)/2, (y1 + y2)/2, localHit.Z),
Vector3.new(math.abs(x2 - x1), math.abs(y2 - y1), glassSize.Z)
)
-- Shard 4 (bottom-right)
CreateShard(
Vector3.new((x2 + x3)/2, (y1 + y2)/2, localHit.Z),
Vector3.new(math.abs(x3 - x2), math.abs(y2 - y1), glassSize.Z)
)
glass:Destroy()