Possible bug with ray collision with terrain quad

Hello monkeys,

I am relatively new to JME, but I suspect I may have found a bug, or I’m missing something stupid.

I have a Sphere floating above a terrainquad, moving with vector (1, 0, 0). The terrain quad is made from a hill height map, but not very hilly at all, nearly flat with rolling hills.

I am using a ray casting technique similar to @norman’s video tutorial on Don Quixote.

Problem: When I cast an infinite ray from the sphere’s position, (sphere.getLocalTranslation()), direction (0,-1,0), it finds a collision, but always at (0,0,0).

Based on this topic: http://hub.jmonkeyengine.org/forum/topic/terrain-ray-collision/ , I tried altering the direction to (new Vector3f(0.0000001f, -1, 0)).normalize(), and magically, it started working as intended.

Here is code working, only difference is the 0.00000001f --> 0f:

Thanks Monkeys!

[java]
if (terrain != null) {
Ray ray = new Ray(spatial.getLocalTranslation(), (new Vector3f(0.00001f, -1, 0)).normalize());

        System.out.println("Ray "+ ray.origin+ ray.direction+ ray.limit);

        CollisionResults results = new CollisionResults();
        terrain.collideWith(ray, results);
        for (CollisionResult result : results) {
            Vector3f loc = result.getContactPoint();
            spatial.getLocalTranslation().setY(loc.y);
            System.out.println(loc.toString());
            System.out.println(results.size());
        }
        
    }
    else {
        throw new IllegalArgumentException("Terrain not initialized for control");
    }

[/java]

Here is how I create the terrain

[java]

    AbstractHeightMap heightmap = null;
    try {
        heightmap = new HillHeightMap(1025, 1000, 100, 300, 1);
        heightmap.load();
    } catch (Exception ex) {
        Logger.getLogger(EvolSim.class.getName()).log(Level.SEVERE, null, ex);
    }

    terrain = new TerrainQuad("Terrain", 65, 1025, new Vector3f(1,1,1), heightmap.getHeightMap());
    terrain.setMaterial(boxMat);
    terrain.setLocalScale(2f, 1f, 2f);
    terrain.setLocalTranslation(0, -150, 0);
    terrain.setModelBound(new BoundingBox());
    terrain.updateModelBound();
    rootNode.attachChild(terrain);

[/java]

To anyone investigating this, there is a precision bug that I recently fixed in BoundingBox that may have fixed this use case, also. Kind of depends on what is really happening but in my case valid rays were being rejected by the BoundingBox check prior to even trying the mesh.

In my case, the rays were pointing in many different directions, though. So it may not be related but worth a try, I guess. (fix is in SVN but I don’t know if it made it into 3.0.1 or not.)

OP, what version of JME are you running?

I’m using jMonkeyEngine SDK 3.0RC2

3.0 has been released for real. There is a thread about it here: http://hub.jmonkeyengine.org/forum/topic/jmonkeyengine-3-0-release/

…I think there has even been one update since then. 3.0 didn’t have my fix. 3.0.1 might have… the next update certainly will. You won’t get these updates with just stock RC2.