Closed AshhadDevLab closed 1 week ago
You sould call .updateMatrixWorld()
after dragging the object.
Tip: check collisions on drag event to prevent querying every render:
dragcontrols.addEventListener("drag", (event) => {
boxMesh1.position.y = 0;
// position was changed, update world matrix before checking collision
boxMesh1.updateMatrixWorld();
transformMatrix = new THREE.Matrix4()
.copy(boxMesh4.matrixWorld)
.invert()
.multiply(boxMesh1.matrixWorld);
hit = boxMesh4.geometry.boundsTree.intersectsGeometry(
boxMesh1.geometry,
transformMatrix
);
if (hit) {
boxMesh1.material.color.set(0xe91e63);
boxMesh1.material.emissive.set(0xe91e63).multiplyScalar(0.25);
boxMesh1.position.copy(lastSafePosition);
} else {
console.log(false);
boxMesh1.material.color.set(0x666666);
boxMesh1.material.emissive.set(0xe91e63);
lastSafePosition.copy(boxMesh1.position);
}
});
You sould call
.updateMatrixWorld()
after dragging the object.Tip: check collisions on drag event to prevent querying every render:
dragcontrols.addEventListener("drag", (event) => { boxMesh1.position.y = 0; // position was changed, update world matrix before checking collision boxMesh1.updateMatrixWorld(); transformMatrix = new THREE.Matrix4() .copy(boxMesh4.matrixWorld) .invert() .multiply(boxMesh1.matrixWorld); hit = boxMesh4.geometry.boundsTree.intersectsGeometry( boxMesh1.geometry, transformMatrix ); if (hit) { boxMesh1.material.color.set(0xe91e63); boxMesh1.material.emissive.set(0xe91e63).multiplyScalar(0.25); boxMesh1.position.copy(lastSafePosition); } else { console.log(false); boxMesh1.material.color.set(0x666666); boxMesh1.material.emissive.set(0xe91e63); lastSafePosition.copy(boxMesh1.position); } });
Well thanks for the code it turns out to be working as expected!
Description
Hi there, I am trying to create a collision effect using
.shapecast
to prevent objects to phase through each other. So far I have created 2 meshes namelyboxMesh1
andboxMesh4
.boxMesh1
is made draggable usingDragControls
where asboxMesh4
is idle and I want to make the moveable mesh to collide with the idle mesh instead of phasing through it.Issue
Currently as soon as the meshes collide the moveable mesh gets stuck. Also even if I try the previous position method the meshes still phase through a little bit.
Demo
Here's a working demo with code for better visuals: Glitch
Code Snippet
Also here's the snippet that's responsible for checking and preventing the phase:
I'll be very grateful and thankful if anyone can help me regarding this.