Intersection testing with vector to the intersection point


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();

    TriMesh mesh = (TriMesh)bpr.getPickData(i).getTargetMesh();

    for (Object j : al)


    mesh.getTriangle((Integer)j, vertices);

    Vector3f res = it.rayIntersectsTriangle(lineOfSight.origin.subtract(mesh.getWorldTranslation()),

    lineOfSight.direction, vertices[0], vertices[1], vertices[2]);

    if (res != null)


    if ((res.x > 0) && (res.x < nearestTarget))


    nearestTarget = res.x;

    inTarget = mesh.getParent();

    sceneTarget = it.getLastIntersectionVector().add(mesh.getWorldTranslation());




    sceneTarget is then the intersection point.

    Note: You have to correct the resulting vector with getWorldTranslation() if you need the world coords.

    Here is the class:

    package de.worldofmystery.common;

    import com.jme.math.Vector3f;

    public class IntersectionTests


Here is a screenshot of the result:

I haven’t read your code, but it seems you want the same like described here - it’s available in the Ray class, now.

Yes, it was added one day after I posted the function :wink:

Just one thing stop me from discarding the function I posted and use the intersectWhere():

To get the height over terrain I need the calculated t to update the camera position.

For picking it looks very good though.

Just to clarify, the function was pasted directly from another project I've been working on for jsr 184.  :slight_smile:

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).

Hmm, because you know the direction of the Ray, you could figure out the distance without an expensive sqrt call…  basically:

boolean hits = myRay.intersectWhere(t1, t2, t3, loc);
if (hits) {
    float distance = (loc.subtract(myRay.getOrigin).divideLocal(myRay.getDirection())).x;

(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?

This is the link to the Client (game client of a planned MMORPG):

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.