How to Properly Rotate a Mesh for Splitting a Mesh in Two?

Hi everyone,

I’m currently working on a Babylon.js project where I need to split a mesh into two parts. To determine the split position, I’m using a line-mesh. I want to be able to rotate this line using the ‘e’ and ‘q’ keys. However, I’m encountering a couple of issues:

  1. The rotation works only when viewed from the top and not from the front.
  2. The rotation is not being applied during the split operation.

Here is my current playground:

https://playground.babylonjs.com/#UOKHO5

Any guidance or suggestions would be greatly appreciated!

Thanks in advance!

The issue here is that you are always rotating on the Z axis, but your line is rotated on different sides of the box.

Just the same way you are finding the angle to rotate the line, you can use the same method to find the right angle to rotate.

Another suggestion on my end is not to use window.addEventListener on the playground (unless you are removing them on scene dispose). The reason is that event listeners are kept if you run the playground over and over again, so eventually the callbacks will be called often with the wrong values. It would be better to use the babylon event listeners instead, as they are disposed correctly. You are also registering the event listeners on each pick info hit, which makes the entire playground very unexpected.
You can start here (you need to be sure the focus is on the canvas. However, the rest of your implementation makes it a bit hard to get it to work correctly)

Split mesh using a red line | Babylon.js Playground (babylonjs.com)