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.
\$\begingroup\$
\$\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\$DMGregory– DMGregory ♦2021-05-29 15:10:56 +00:00Commented 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\$Rick Stanley– Rick Stanley2021-05-29 15:15:26 +00:00Commented May 29, 2021 at 15:15
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
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);
```
