Randomly positioned objects on the surfaces from file

Hello everybody.
I would like to create randomly positioned objects on the scene from file (j3o/.xml). I can do that, but i want place objects on the surfaces (ground, roofs etc.). My scene is a small village. I know i can use getHeight() method with Terrain type but i don’t know how should i get height (y) in specified point (x,z) for Spatial, Geometry,Node or cast to Terrain.

Here is my actual code (fragments). I used fixed y coordinate:

private void makeCubes(int number) { 
         for (int i = 0; i < (int) number; i++) { 
                  Vector3f loc = new Vector3f( 
                  FastMath.nextRandomInt(-40, 40), 1.5f,FastMath.nextRandomInt(-40, 40));    
                  Geometry geom = myBox("Cube" + i, loc, ColorRGBA.randomColor()); 
                  itemsNode.attachChild(geom); 
         }
}
makeCubes(40);

Thank you for all replies!

You could do a raytrace from somwhere certainly about to somewhere certainly below the terrain.

Via the boundingVolume you can approximate the size of your objects, and offset them accordingly.

I used raycasting from point over the object to the terrain. Is it good try?

for (int i = 0; i < (int) number; i++) { 
            Vector3f loc = new Vector3f(FastMath.nextRandomInt(-20, 20),1.5f,      
                     FastMath.nextRandomInt(-20, 20)); 
             ray = new Ray(new Vector3f(loc.x , 6.1499743f , loc.z), 
                     Vector3f.UNIT_Y.negate());
             sceneModel.collideWith(ray, results); 
             if (results.size() > 0) {  
                dist = results.getClosestCollision().getDistance();
                if(dist >=1.0) {
                      //positioning through offset
                      loc.setY(7-dist);
                      Geometry geom = myBox("Cube" + i, loc, ColorRGBA.White); 
                      itemsNode.attachChild(geom);
                  } else {
                    i++;
                }

         }
             else {
                    i++;
                }
         }