game-engine/src/main/java/com/jzy/game/engine/math/Intersector.java
/**
	 * Intersects the given ray with list of triangles. Returns the nearest
	 * intersection point in intersection
	 * 
	 * @param ray
	 *            The ray
	 * @param triangles
	 *            The triangles, each successive 3 elements from a vertex
	 * @param intersection
	 *            The nearest intersection point (optional)
	 * @return Whether the ray and the triangles intersect.
	 */
Intersects the given ray with list of triangles. Returns the nearest
intersection point in intersection

@param ray
The ray
@param triangles
The triangles, each successive 3 elements from a vertex
@param intersection
The nearest intersection point (optional)
@return Whether the ray and the triangles intersect.
Intersects the given ray with list of triangles . Returns the nearest intersection point in intersection @param ray The ray @param triangles The triangles each successive 3 elements from a vertex @param intersection The nearest intersection point ( optional ) @return Whether the ray and the triangles intersect .
public static boolean intersectRayTriangles(Ray ray, float[] triangles, Vector3 intersection) {
		float min_dist = Float.MAX_VALUE;
		boolean hit = false;
		if (triangles.length / 3 % 3 != 0)
			throw new RuntimeException("triangle list size is not a multiple of 3");
		for (int i = 0; i < triangles.length - 6; i += 9) {
			boolean result = intersectRayTriangle(ray, tmp1.set(triangles[i], triangles[i + 1], triangles[i + 2]),
					tmp2.set(triangles[i + 3], triangles[i + 4], triangles[i + 5]),
					tmp3.set(triangles[i + 6], triangles[i + 7], triangles[i + 8]), tmp);
			if (result == true) {
				float dist = ray.origin.dst2(tmp);
				if (dist < min_dist) {
					min_dist = dist;
					best.set(tmp);
					hit = true;
				}
			}
		}
		if (hit == false)
			return false;
		else {
			if (intersection != null)
				intersection.set(best);
			return true;
		}
	}
public static boolean intersectRayTriangles ( Ray ray , float [ ] triangles , Vector3 intersection ) { float min_dist = Float . MAX_VALUE ; boolean hit = false ; if ( triangles . length / 3 % 3 != 0 ) throw new RuntimeException ( "triangle list size is not a multiple of 3" ) ; for ( int i = 0 ; i < triangles . length - 6 ; i += 9 ) { boolean result = intersectRayTriangle ( ray , tmp1 . set ( triangles [ i ] , triangles [ i + 1 ] , triangles [ i + 2 ] ) , tmp2 . set ( triangles [ i + 3 ] , triangles [ i + 4 ] , triangles [ i + 5 ] ) , tmp3 . set ( triangles [ i + 6 ] , triangles [ i + 7 ] , triangles [ i + 8 ] ) , tmp ) ; if ( result == true ) { float dist = ray . origin . dst2 ( tmp ) ; if ( dist < min_dist ) { min_dist = dist ; best . set ( tmp ) ; hit = true ; } } } if ( hit == false ) return false ; else { if ( intersection != null ) intersection . set ( best ) ; return true ; } }
Intersects the given ray with list of triangles.
Intersects the given ray with list of triangles .