Help with Terrain Ray cast from one patch to the next

Use Case: have a sphere with a control that keeps it on top of Terrain as it moves accross Terrain

I am following Norman’s old youtube video on don quixote. It works fine while for awhile but as soon as it switches to a new patch of the terrain it moves to 0 on the Y like it can no longer see the Terrain.

I am having issue when it moves from one patch to the next. When it hits Patch 2 it shoots up to 0

Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.73636
Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.81961
Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.90274
Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.986084
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0

Code
EntityControl

public class EntityControl extends AbstractControl {
private long speed = 10;

@Override
protected void controlUpdate(float tpf) {   
    spatial.move(tpf*speed, 0, 0);
}

TerrainTrackControl

 public class TerrainTrackControl extends AbstractControl {
private final Ray ray = new Ray(Vector3f.ZERO.clone(), new Vector3f(0,-1,0));
private final Vector3f up = new Vector3f(0,400,0);
private final CollisionResults results = new CollisionResults();
private Spatial terrain;



private float offset = 2f; //sphere radius


@Override
protected void controlUpdate(float tpf) {
    terrain = spatial.getParent();
    if(terrain!=null){
        ray.setOrigin(spatial.getWorldTranslation().add(up));
        ray.setLimit(400);
        results.clear();
        terrain.collideWith(ray, results);
        for(Iterator<CollisionResult> it = results.iterator(); it.hasNext();){
            CollisionResult collisionResult = it.next();
            if(isTerrain(collisionResult.getGeometry())){
                Vector3f loc = collisionResult.getContactPoint();
                System.out.println("Geometry Name 2 : " + collisionResult.getGeometry().getName() + " Loc Y" + loc.getY());
                spatial.setLocalTranslation(spatial.getLocalTranslation().setY(loc.getY() + offset));

                return;
            }
        }
    }
}


private boolean isTerrain(Spatial spatial) {
    while(true) {
        if(spatial == null) {
            return false;
        }else if(TestTerrian.TERRAIN_NAME.equals(spatial.getName())) {
            return true;
        }
        spatial = spatial.getParent();
    }
}

What did I miss?

I looked at your code but couldn’t find the problem yet…
What do you get when you System.out.println(loc);?

What is spatial.getParent()? Is it a node that contains all of the terrain? Does breakpoint debugging show where your logic fails?

@Domenic

It appears that when the Geometry moves to the Patch2 part from Patch3 of the terrain the collisionResult contact point goes to 0,0,0

Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.8833
collisionResult.getContactPoint() Loc : (255.7666, -72.8833, 0.0)
Spatial Loc : (255.7666, -70.80005, 0.0)
Geometry Name 2 : myTerrainQuad4Quad1Patch3 Loc Y-72.96657
collisionResult.getContactPoint() Loc : (255.93314, -72.96657, 0.0)
Spatial Loc : (255.93314, -70.8833, 0.0)
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0
collisionResult.getContactPoint() Loc : (0.0, 0.0, 0.0)
Spatial Loc : (256.0993, -70.96657, 0.0)
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0
collisionResult.getContactPoint() Loc : (0.0, 0.0, 0.0)
Spatial Loc : (256.26764, 2.0, 0.0)
Geometry Name 2 : myTerrainQuad3Quad4Patch2 Loc Y0.0
collisionResult.getContactPoint() Loc : (0.0, 0.0, 0.0)
Spatial Loc : (256.4344, 2.0, 0.0)

@jayfella spatial.getParent() is the rootNode

    terrain = new TerrainQuad(TERRAIN_NAME, patchSize, 513, heightmap.getHeightMap());

    /** 4. We give the terrain its material, position & scale it, and attach it. */
    terrain.setMaterial(mat_terrain);
    terrain.setLocalTranslation(0, -100, 0);
    terrain.setLocalScale(2f, 1f, 2f);
    rootNode.attachChild(terrain);

    ...
    //sphereGeo is the entity moving  that I am using as the origin of the ray 
    sphereGeo.addControl(terrainTrackControl); 
    this.getRootNode().attachChild(sphereGeo);

I will try stepping through the terrain.collideWith and see if I can figure out why it thinks it hitting something at 0,0,0