Realistic glass help

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()

1 Like

take a look at this bad baby i just whipped up in Paint:
square_to_triangles

As you can see, you can make the square out of two wedge parts with the same size and position (which would also be the original squares size and position), just rotated 180 degrees from one another.

Since you already have the code to divide into squares, it is as simple as making them wedge parts instead, cloning them, and rotating them to complete the square.

You might also want to rotate the original part 90 degrees on a random axis to introduce some variety in the shatter pattern

This makes me want to implement a glass shatter mechanic using quad-tree subdivisioning. With a system like that, you could have immensely detailed shatters which are also performant. Thank you random developer for reminding me that these structures exist

1 Like