Voxel greedy mesh system doesn't work with rotation

I’ve been working on a voxel destruction system for my fps game for a while now and I recently realized once I implemented welding that getting the position for parts after greedy meshing does not account for the original rotation of the target part. My code works by splitting up a target part into 1x1x1 voxels and then checking for collisions on each one. Once I have the voxels that haven’t collided with anything I greedy mesh them and use the voxel size and the new size for the greedy meshed part to get a position. I’m not great with CFrame math so I’m trying to get any help I can get.

Here are some images of what it looks like(the red parts are what it should line up with):

With no rotation:
Screen Shot 2023-11-21 at 12.12.15 AM

With rotation:

A snippet of my module that calculates the final position of a voxel:

for x, row in pairs(regionPos) do
		for y, column in pairs(row) do
			for z, size in pairs(column) do
				local position = size.Position
 				local part = Instance.new("Part")
				part.Anchored = true
				part.Size = Vector3.new(size.X, size.Y, size.Z)
				part.CFrame = CFrame.new(position.X - size.X / 2 + blockSize.X/2, position.Y - size.Y / 2 + blockSize.Y/2, position.Z - size.Z / 2 + blockSize.Z/2)
				part.Color = targetPart.Color
				part.Material = targetPart.Material
				part.TopSurface = targetPart.TopSurface
				part.BottomSurface = targetPart.BottomSurface
				part:SetAttribute("Destructible", true)
				part.Parent = workspace
1 Like

you’re defining the new part’s cframe in position only. You need to get the rotation of the original part and apply it to the new part

3 Likes

I’ve already tried this but since the rotation is applied to the middle of the part it offsets it even more and I couldn’t figure out how to correct it, I know you could pivot it but I don’t know how I would be able to figure out where to pivot it from.

2 Likes

In the total algorithm for deciding position you may have to consider the local vector space of the original part when figuring out the position of the new part. Your current positioning is world axis aligned, so if the original part is rotated, the world axis will no longer be aligned with the axis of the part. The CFrame of the original part should give some insight into this. You may be able to translate using the CFrame of the original part, as multiplying a CFrame by a Vector3 will translate that Vector3 in object space but give you the world coordinates as a result

2 Likes

The thing that makes this tricky is since the original part’s position I’m using to calculate the new position has a different size, that’s why I have to do all the math with the part’s size to get a new position, thats why you can’t translate to object space otherwise it will break things.

1 Like

Without seeing how regionPos coordinates are derived from the original part, which would require object space translation, I can’t help you any further

2 Likes

Thanks for this reply, I ended up doing more research and while I was looking on the forum I came across this post: Non-voxel greedy mesher for runtime parts merging/map optimization. The code helped me understand I just needed to multiply the original position by a new CFrame using the new greedy meshed size and now it works perfectly for rotated objects and I have realtime destruction physics.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.