To uncover the solution, it is crucial to understand that the line connecting the two points closest to each other is perpendicular to the two rays.
The initial step involves determining the direction vector of the line formed by the two closest points. This vector can be obtained using the cross product since it is normal to both rays. For example, let's consider rayA
and rayB
as objects of type THREE.Ray
:
let Nv = rayA.direction.clone().cross(rayB.direction);
Proceeding further, a plane needs to be identified for each ray, encompassing the ray itself along with the closest point on the opposite ray. A plane consists of two vectors - the directional vector of the plane and Nv
. However, an alternative representation of the plane is needed, involving a point and a normal vector. The point corresponds to the origin of the ray. Once again, the normal vector can be derived through the cross product. To facilitate subsequent calculations, these vectors must be normalized to become unit vectors (with a length of 1):
let Na = rayA.direction.clone().cross(Nv).normalize();
let Nb = rayB.direction.clone().cross(Nv).normalize();
The primary concern now shifts to finding the intersection of a ray and a plane. Consider ptA
and ptB
as THREE.Vector3
objects representing the closest points on the respective rays:
let Da = rayA.direction.clone().normalize();
let Db = rayB.direction.clone().normalize();
let da = rayB.origin.clone().sub(rayA.origin).dot(Nb) / Da.dot(Nb);
let db = rayA.origin.clone().sub(rayB.origin).dot(Na) / Db.dot(Na);
let ptA = rayA.origin.clone().add(Da.multiplyScalar(da));
let ptB = rayB.origin.clone().add(Db.multiplyScalar(db));
Diving deeper into the explanation of the intersection between a ray and a plane:
https://i.sstatic.net/C9AQT.png
A ray is described by a point Ra
and a direction Da
. On the other hand, the plane is defined by a point Rb
and a normal vector Nb
.
The perpendicular distance n
from point Ra
to the plane is calculated using the dot product:
n = | Rb - Ra | * cos(alpha) = dot(Rb - Ra, Nb)
This leads to the determination of the distance da
from the intersection point ptA
to the origin of ray Ra
:
da = n / cos(beta) = n / dot(Da, Nb)
The coordinates of the intersection point ptA
are then given by:
ptA = Ra + Da * da = Ra + Da * dot(Rb - Ra, Nb) / dot(Da, Nb)