How to move an object toward another object until collision?

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++
}
}
}

I see no one has answered so far.

To be honest, I have no time to view your code in details. At a glance, it appears that you are doing things in a much harder way than it is necessary. Maybe I do not fully grasp the situation.

In any case, if I have two moving objects, and if I want to detect their collision, I will first try to do it simple:

  • I’d change their position not with a matrix, but via their .position properties. Then, for collision I will only check the distance between the positions of the two objects. If it is smaller than some threshold, I will consider there is a collision.
  • Only if this is not sufficient (e.g. the objects have very strange shape, or I need high accuracy), I’g dive into more advanced collision detection, like some ray casting or even physics engine, etc.
1 Like

Thanks for the reply.

I’m using the matrix only to put the objects in their initial start. One was made using a specific software. All I have is its vertex position (x,y,z) and its triangles. The positions were given in a particular frame, which is not very convinient for me. I managed to compute the matrix transformation so that this object can be displayed in my three.js scene. This part is working fine.

After, I display my second object in my scene, a little away in the X axis.

I want to move this second object toward the first object until one of its vertex enters the first object. As you said, these object have some complex shape, I can’t use some boxing or use a sphere around them to compute the collision between them.

I’m just asking if someone have already done this or have an idea to do this…
Thanks again

1 Like