I use the following class to do intersection tests of a ray and a triangle.
rayIntersectsTriangle() returns three floats of t, u and v in a Vector3f. The result is not really a vector but the distance from the origin to the intersection point (t) and the triangle coords u and v in the triangle.
getLastIntersectionVector() returns a real vector of the last rayIntersectsTriangle() call
Let's say bpr is a TrianglePickResult and lineOfSight is a Ray(cam.getLocation(), cam.getDirection()).
ArrayList al = bpr.getPickData(i).getTargetTris();
Just to clarify, the function was pasted directly from another project I've been working on for jsr 184.
As for your other need, the function does not return something like your functions u,v in the plane of the triangle. Instead, world coordinates for the triangle are passed in and a world coordinate intersection point is returned. If you want the distance, it's relatively simple to do that: lengthOf(rayStartPoint - intersectionPoint)
I assume when you are saying you are using this for HOT you are not using our TerrainPage/Block classes, or you'd just be using the pre-existing getHeight(Vector3f) method. (or perhaps you have other requirements?)
Ah yes, I could use the length of the difference as you said, but that would mean one more length calculation where the value is already known in the function. In worst case this function is called once per frame.
I need this for HOT as I use modelled scenes as terrain. The whole virtual world is partitioned in scenes and in the end there will be villages, cities, dungeons and so on. That's why I use models instead of the terrain functions in jME. The "terrain" will have multiple heights per x and z (or x and y in my example).
(kept it simple, but I'd also use a storage vector in the subtract method.)
Or we could add the t,u,v return as another method type. Could you give an example of it's usage when you want world coordinates of the intersection point?
It's still a bit clumsy as I'm testing lots of things. The HOT check is in simpleUpdate(). I kept the name but it's not SimpleGame anymore.
There is one more problem with the intersection. I won't let a player walk if there is no ground underneath. If I use the getTargetTris() it's blocked then and now and I don't get any triangle intersections.
I see where you use the x value of your intersection vector, but not y and z (u, v) Or maybe you don't?
In any case, I've added the method intersectWherePlanar which stores t,u,v in the return method as explained in the javadoc below:
/**
* <code>intersectWherePlanar</code> determines if the Ray intersects a
* triangle defined by the specified points and if so it stores the point of
* intersection in the given loc vector as t, u, v where t is the distance
* from the origin to the point of intersection and u,v is the intersection
* point in terms of the triangle plane.
*
* @param v0
* first point of the triangle.
* @param v1
* second point of the triangle.
* @param v2
* third point of the triangle.
* @param loc
* storage vector to save the collision point in (if the ray
* collides) as t, u, v
* @return true if the ray collides.
*/