Hello, I’ve been making voxel physics for my game. Everything seems well and fine, except for that it lags very hard when trying to voxelize larger walls, as many small parts are being made in order to fill the wall.
As I was looking into ways to try to fix this, I came across Quadtree partitioning. It is exactly what I need in order for this. The game Voxel Destruction Physics uses this to help with optimization, as it is very powerful.
Basically, the whole wall will voxelize, with parts closer to the impact point being more packed together.
This is the code I have for creating the voxels.
local function Divide(part : BasePart, targetBlockSize, HitPosition : Vector3)
if not part:IsA("BasePart") then return nil, nil end
if part:IsA("TrussPart") then return nil, nil end
local NewParts = {}
local Blocks = {}
local function Elw(vec, op)
return Vector3.new(op(vec.X), op(vec.Y), op(vec.Z))
end
local blockCount = Elw(part.Size, function(l) return math.floor(l / targetBlockSize) end)
local blockSize = part.Size / blockCount
local Offset = (part.Size + blockSize) / 2.0;
local Health = nil
pcall(function()
Health = (blockSize.Magnitude / part:GetAttribute("Health")) / 0.01
end); --smaller part has less health, bigger has more health
local NewCS = ChunkSystem.new(3, blockSize.X)
for x = 1, blockCount.X do
Blocks[x] = {}
for y = 1, blockCount.Y do
Blocks[x][y] = {}
for z = 1, blockCount.Z do
--local p = Instance.new("Part")
--p.Anchored = true
--p.Size = blockSize
--p.CFrame = part.CFrame:ToWorldSpace(CFrame.new(Vector3.new(x,y,z) * blockSize - Offset));
--p.Parent = workspace
Blocks[x][y][z] = {
Offset = Offset;
CF = part.CFrame:ToWorldSpace(CFrame.new(Vector3.new(x,y,z) * blockSize - Offset));
Position = Vector3.new(x, y, z);
OriginalSize = part.Size;
BlockSize = blockSize;
Health = Health;
PropertyInfo = {
Color = part.Color;
Material = part.Material;
Transparency = part.Transparency;
Reflectance = part.Reflectance;
CastShadow = part.CastShadow;
Name = part.Name..":"..x..":"..y..":"..z;
Parent = part.Parent;
Descendants = part:GetDescendants()
}
}
end
end
end
return Blocks, blockCount
end
If someone can figure out a way to just make it more optimized using Quadtrees, I’d be very very thankful.