An implementation of the Watertight Ray/Triangle Intersection algorithm
Background / Initial Motivation
I've been using substack/ray-triangle-intersection for some of my mouse picking, but Möller–Trumbore ray-triangle intersection algorithm that it uses does not satisfy my use case this time around.
I'm mousing over a grid based terrain and determining which tile in the terrain is moused over. Since the Möller–Trumbore algorithm isn't watertight, when I mouse in between two tiles neither tile is selected and thus it's as if you aren't mousing over the terrain at all.
The goal of this
watertight-ray-triangle-intersection module is to implement the algorithm found in the Water Ray/Triangle Intersection
in order to solve this problem for myself, and hopefully you too.
$ npm install --save watertight-ray-triangle-intersection
var rayTriIntersect =var rayOrigin = 3 4 01var ray = -3 -4 -01var triangle =-10 0 1010 0 100 0 -10var intersection =console// [0, 0, 0]
intersection(intersectionCoords, rayOrigin, ray, triangle, options) ->
The start location of your ray
var rayOrigin = [500, 25, 17]
// Example rayvar ray = 1 0 0
A vector that specifies your ray's direction in 3d space.
An array of 3 arrays.
Each of the sub arrays is an array of 3 points.
// Example Trianglevar triangle =-1 0 11 0 10 0 -1
var options =backfaceCulling: false
Whether or not to use back-face culling when testing for intersections.
- fix divide by zero issues when a ray had a 0 as
x, y, or z(see code comments for further details)