[Solved] Raycast against a math line, or THREE.Line3

raycaster
math

#1

I found it interesting that while THREE.Ray class has methods to detect collisions against Triangle, Plane, Box, and Sphere, doesn’t have a method for Line, such as like intersectsLine3(). Must I write my own or is there a way around it. My goal is to get an intersection on a line, from a ray cast.

So I want to ray cast against a 2D linear line who has a start and end point.

Currently, i’m thinking to get around it without having to write my own, I can create a THREE.Line mesh out of a Line3, and then use the THREE.Raycaster instead to do intersectObjects(myLine).


#2

This step is not necessary. You can directly use Ray.distanceSqToSegment. When performing a ray/line segment intersection test, you actually calculate the distance between both entities. There is an intersection if the distance is smaller than a defined value.


#3

Thank you! I see your point now.


#4

Hi @Mugen87, I thought I had it in my head when i read your instructions. I tried to some attempts but couldn’t get to the logic you described implemented.
here is a jsfiddle
https://jsfiddle.net/blackstrings/2xq4meaf/

see if you can connect the dots for me, on how I can get to knowing if an intersection occurred and where the intersection occured. You will be interested in the createScene() method.


#5

There were two problems in your code: https://jsfiddle.net/2xq4meaf/17/

  • Direction vectors always have unit length.
  • Ray.distanceSqToSegment calculates the squared euclidean distance. It’s not valid to compare this metric with the euclidean distance.

The remaining differences in the result (dist vs. calcDist) are due to floating point inaccuracies.


#6

Ah thanks again @Mugen87 you saved the day!