THREE.BoxBufferGeometry get intersection with raycaster.intersectObjects

I want to get the intersection of the ray with the box. What’s wrong with my code?

    const ggeo = new THREE.BoxBufferGeometry(100, 200, 60);
    const gmesh = new THREE.Mesh(ggeo, new THREE.MeshBasicMaterial({ color: 0x00ffff}));
    gmesh.position.set(-150, 0, -10);
    gmesh.visible = true;
    gmesh.updateMatrixWorld();
    scene.add(gmesh);

    // intersection
    var from = new THREE.Vector3(-10, 0, -10);
    var to = new THREE.Vector3(0, 0, -1);
    var raycaster = new THREE.Raycaster();
    raycaster.set(from, to);
    var intersects = raycaster.intersectObjects(gmesh as any, true);
    if (intersects.length > 0) {
          // no interseection !
    }

There’s no intersection because your ray is shooting in a direction that doesn’t touch the mesh. Your box is at -150x, and your ray shoots from -10x in a direction that will never reach the box.

2 Likes

I change parameters, still no intersection with

var to = new THREE.Vector3(-1, 0, 0);

Then I don’t know. With (-1, 0, 0) then your raycast should point in the right direction, so it may be something else. Maybe you could create a demo in JSFiddle.net or Codepen.io so we can debug it more easily.

here is a fiddle:

https://jsfiddle.net/jek75fLg/

Why do you recreate all things in render every time?

just for a quick test. here an adaption. That’s not the reason for my problem

https://jsfiddle.net/9bp071dg/1/

intersectObject-s

without ‘s’ solved the problem