I’m using three-mesh-bvh to check collision between 2 object in Threejs.
The function intersectsGeometry working for collision.
I edited the TransformControl.js to check collision when dragging, like:
checkCollision(item1, item2) {
if (item1.isMesh && item2.isMesh) {
item1?.geometry?.computeBoundsTree()
item2?.geometry?.computeBoundsTree()
const transformMatrix = new THREE.Matrix4().copy( item1.matrixWorld ).invert().multiply( item2.matrixWorld );
const hit = item1.geometry.boundsTree.intersectsGeometry( item2.geometry, transformMatrix )
return hit;
}
return false
}
And, in the pointerMove function, with translate mode, I added below function to prevent drag after collision, like:
checkOverlap() {
const spheres = editor.scene.children.filter((item) => item.uuid !== object.uuid) // get object is not selecting
// let's say, i just get the first object to test
const hit = this.checkCollision(object, spheres[0]);
if (!hit) {
object.position.copy(this._offset).add(this._positionStart)
}
}
Of course, it stop after collision. But I can drag it again after collision, because it checked collision first.
I look forward to any helpful
Thank you~