Let's say we have two vectors A and B. Is there a way to use Three.js' Ray or Raycaster classes to cast a line between vector A and vector B and check is it passes through a mesh in the scene? I know from the documentation that both the Ray and Raycaster classes accept origin and direction vectors but I specifically need two points. Thanks in advance.

example image

  • 1
    \$\begingroup\$ Presumably you considered computing the offset vector from A to B, then passing A as the origin, (B - A) normalized as the direction, and the length of (B-A) as the far value? Did anything impede this from working in your tests? \$\endgroup\$
    – DMGregory
    May 29 at 15:10
  • \$\begingroup\$ @DMGregory Thank You so much. It's working. I didn't know it could be done so easily. \$\endgroup\$ May 29 at 15:15

As @DMGregory answered in the comment it can be done this way:

const a = new Vector3(xa, ya, za);
const b = new Vector3(xb, yb, zb);

const c = b.clone().sub(a);
const far = c.length();

const raycaster = new Raycaster(b, c.normalize(), 0, far);
const intersections = raycaster.intersectObjects(scene);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.