Voxel Physics like JJS

Im trying to make voxel physics like jjs but im struggling
i made this module but its slightly more “laggy” on big objects and just dosent work the same way because im not sure how do i make it like drop the parts instead of destroying them

Module:


local module = {}
module.VoxelSize = 2

function module:clonePart(template): BasePart
	local part = template:Clone()
	part.Parent = template.Parent

	return part
end

function module:getPartsInBounds(cframe: CFrame, size: Vector3, params: OverlapParams?): {BasePart}
	if tonumber(size)  then
		return workspace:GetPartBoundsInRadius(cframe.Position, size, params)
	else
		return workspace:GetPartBoundsInBox(cframe, size, params)
	end
end

function module:DividePart(part: BasePart, axis: Vector3)
	local a = module:clonePart(part)
	local b = module:clonePart(part)

	a.Size = part.Size * (-(axis/2)+Vector3.new(1,1,1))
	a.CFrame = part.CFrame * CFrame.new(-part.Size * (Vector3.new(1,1,1)*axis/4))	

	b.Size = part.Size * (-(axis/2)+Vector3.new(1,1,1))
	b.CFrame = part.CFrame * CFrame.new(part.Size * (Vector3.new(1,1,1)*axis/4))	
end

function module:SubdivdePart(part: BasePart): boolean
	if part.Size.X / 2 < module.VoxelSize and part.Size.Y / 2 < module.VoxelSize and part.Size.Z / 2 < module.VoxelSize then return false end

	local axis = math.max(part.Size.X, part.Size.Y, part.Size.Z)
	if axis == part.Size.X then
		module:DividePart(part, Vector3.new(1,0,0))
	elseif axis == part.Size.Y then
		module:DividePart(part, Vector3.new(0,1,0))
	else
		module:DividePart(part, Vector3.new(0,0,1))
	end

	part:Destroy()
	return true
end

function module:PartDividable(part: BasePart)
	if not part:IsA("Part") then return end
	if part.Shape ~= Enum.PartType.Block then return end

	return true
end

function module:DestroyPartsInArea(cframe: CFrame, size: Vector3, params: OverlapParams?)
	local parts
	repeat
		parts = module:getPartsInBounds(cframe, size, params)
		if #parts == 0 then break end
		for i = 1, #parts do
			if module:PartDividable(parts[i]) then
				local divided = module:SubdivdePart(parts[i])
				if not divided then parts[i]:Destroy() end
			else
				parts[i] = nil
			end
		end
	until #parts == 0
end

return module

What is JJS? Be more descriptive to ensure that you can receive the help needed.

jujistsu sheningans im pretty sure

@ziv377264
maybe these could help you out

tested the 2nd one dosent really fit my need but ill try the first

1 Like

the first one is close to what i want but its not exactly it im trying to achieve squares that are the same size and also hitbox based voxelization not voxalizing a entire part

well you have only two options,

option A

create your own from scratch but use the modules source code to help you

option B

use one of the modules just how it is or try to modify it yourself

edit : i forgot about option C

option C

give up

i made my own module the issue is its pretty much the same as the rest that you showed is there maybe a way to calc the amount of times i need to divide?

1 Like

also i found out the other module you sent can work for my use But the module itself seems to be broken i took the example that the owner made and it dosent work

nevermind i figured it out myself

how you figured it out, what was your solution?

i modified voxbreaker to fit my needs