The next game I’ll be working on in a few months will feature a way to “cut” a mesh in two. I spent a few days figuring out how to actually do it. A post over at the Oren Game Engine site really helped out. The process is pretty much this:
- Find the mesh you want to cut
- Split the mesh using a plane into a negative and positive mesh
- For me, positive is the side that is in the direction of the normal
- Negative is the opposite side
- Close the hole in the negative and positive sides
- Map the UV’s the new parts
- Create the physics objects
It’s that simple… well, almost.
As a proof of concept, I made a quick JavaScript app that uses the canvas object to draw lines and triangles. It demonstrates step 1 and 2, and some of 3. The app really helped me understand how the process works, especially which triangles go on the positive and negative sides. Though it’s in 2D, it’s all based on vector math, so when converting to 3D it should just work. Here are my results so far:
The top image (the green triangles) is the original mesh. The blue line is the plane that I am cutting the mesh with. The black line is the normal of the plane. The numbers are the vertex order.
The bottom image is the original mesh cut in two; positive and negative. The positive mesh is colored magenta and the negative is yellow. Some things to note are that when you cut a triangle in two, you actually get a triangle and a quad as a result. Triangulating the quad properly is key. Also, full triangles that don’t intersect at all need to be added to the appropriate side.
So far I’m quite pleased with the results. Let’s hope 3D goes just a smoothly.