So I saw someone do this on Reddit once. It was this mechanic where when a mesh was hit by a force, it “died”. The way it died was to “split” into multiple smaller cubes that all put together made up the same size as the original cube.
As an example, if you had a cube which was 1x1x1 then hitting the cube would spawn 4 0.25x0.25x0.25 cubes in its place, with rigidbodies attached so the cube can “fall apart”. Sort of like primitive procedural destructible objects or environments.
I think I know how it’s supposed to happen, but I have an issue with execution:
Make a raycast to find all the boxes I can hit.
For every box, figure out their measurements
For every box, replace the box with the right amount of smaller boxes
Apply the force to all smaller boxes.
First problem is figuring out the actual volume of the box so that I can fill in the void it leaves behind when I replace it. I assume that I can go by transform.localScale as it returns a Vector3. But the next issue is then to calculate how many of the smaller boxes I would need to replace it. I guess I could just take every axis (X,Y,Z) and then divide their value by 4 to get 4 smaller boxes for that axis…but then I have the issue of boxes overlapping each other. How could I do that otherwise?
The other issue I can see with this, is figuring out where in 3D space to place those new smaller boxes that replaces the big one. I don’t actually have a clue on how I would do this.
Maybe you could use the mesh vertices since they already contain local position information. Get each corner vertex, interpolate between them to make new points along each edge. Then interpolate between those vertices to get points across each face of the cube. Then you can create new meshes using subsections of all the vertices. I think that should work, in theory. Don’t ask me how to program it though haha. I imagine a 3D array of vector3’s and some clever looping would do it.
And I guess if I go by vertices I could probably skip step 2 all together and do vertices instead…hmmm…wonder how I could do that though since the box probably only have 1 face per side instead of 4 smaller faces per side or more.
You would be creating the new faces by making new cubes. The original cube would be destroyed, and in its place would be the smaller cubes created using the same vertices, plus the new interpolated vertices.