0
\$\begingroup\$

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

\$\endgroup\$
2
  • 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\$ Commented May 29, 2021 at 15:10
  • \$\begingroup\$ @DMGregory Thank You so much. It's working. I didn't know it could be done so easily. \$\endgroup\$ Commented May 29, 2021 at 15:15

1 Answer 1

1
\$\begingroup\$

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);
```
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.