Hi,
I’ve loaded two objects in my scene. A matrix have been apply to one objet.
I want to move the second objet along a particular axis toward the second object until they intersect.
My goal is to display the distance between theses two objects.
I tried to use use the following code, but the collision is never detected. I’m not sure but the position of the non moving object seems to be its position before having applied the matrix transformation. Have you any idea to do what I’d like?
Thanks
let gobject1= scene3D.getObjectByName('object1')
let object2 = scene3D.getObjectByName('object2')
if(object2!== undefined && object1 !== undefined) {
var originPoint = objec1.position.clone() <-- this position is the position before applying the matrix
let collideStatus = false
let count = 0
const object2PositionAttribute = object2.geometry.getAttribute('position')
object2.position.x = 100
object2.updateMatrix() <-- so far in the 3D view this object has been moved
const localVertex = new THREE.Vector3()
while(!collideStatus && (count < 150)){
for(let vertexIndex=0; vertexIndex < object2PositionAttribute.count; vertexIndex++){
localVertex.fromBufferAttribute(object2PositionAttribute, vertexIndex)
let globalVertex = localVertex.applyMatrix4(object2.matrix)
let directionVector = globalVertex.sub(new THREE.Vector3(0.0,0.0,0.0))
let raycaster = new THREE.Raycaster(originPoint, directionVector.normalize())
let collisionResults = raycaster.intersectObjects(object1, true)
if(collisionResults.length > 0 && collisionResults[0].disance < directionVector.length()){
collideStatus = true
console.log(count)
break
} }
object2.translateX(1)
count++
}
}
}