Thanks @prisoner849 and @hofk !!
The missing piece was to use the dot product (which I still have to get my head around) to get the result I was looking for. the method name could use a better name if anyone uses it.
function isPositive( start, end, intersection ){ // all parameters are THREE.Vector3()
let v1 = new THREE.Vector3().copy( end ).sub( start );
let v2 = new THREE.Vector3().copy( intersection ).sub( start );
return v1.dot( v2 ) >= 0;
}
// checks if two line segments will intersect on a point
// when traveling in one specific direction from start to end
// but not in the direction from end to start
// params (THREE.Line3, THREE.Line3)
function getIntersectionOnAPoint(line1, line2)
{
var intersection = null;
var A = line1.start;
var B = line1.end;
var C = line2.start;
var D = line2.end;
// Line AB represented as a1x + b1y = c1
var a1 = B.y - A.y;
var b1 = A.x - B.x;
var c1 = a1*(A.x) + b1*(A.y);
// Line CD represented as a2x + b2y = c2
var a2 = D.y - C.y;
var b2 = C.x - D.x;
var c2 = a2*(C.x)+ b2*(C.y);
var determinant = a1*b2 - a2*b1;
if (determinant == 0) {
// The lines are parallel.
} else {
var x = (b2*c1 - b1*c2)/determinant;
var y = (a1*c2 - a2*c1)/determinant;
intersection = new THREE.Vector3(x, y);
}
// if there is an intersection. verify intersection occurs on the two line segments
// when calculating from start to end
if (intersection) {
var line1result = isPositive( line1.start, line1.end, intersection );
var line2result = isPositive( line2.start, line2.end, intersection );
if ( line1result && line2result ) {
// do nothing when the intersection is not "false" as both results are "true"
}
else { //
// set intersection to null when the intersection is "false" as one of results is "false"
intersection = null;
}
}
return intersection;
}
more tweaks would be needed but do you guys think such a function deserves to be on the Ray class?
so someone can do
var ray1 = new THREE.Ray(); // set the origin and direction
var ray2 = new THREE.Ray(); // set the origin and direction
var intersection = ray1.intersectRay(ray2); // returns null if no intersection