Procedural Terrain Help

I am extending the HillHeightMap class to customize it. I want to be able to place things on the terrain after its generated. In HillHeightMap I saw this code in the load() method

// transfer temporary buffer to final heightmap
for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
        setHeightAtPoint(tempBuffer[i][j], j, i);
    }
}

So I used the same loop in my class that is extending HillHeightMap

private val heightMapData: MutableMap<String, Float> = TreeMap()
for (z in 0 until size) {
    for (x in 0 until size) {
        heightMapData["${z.toFloat()},${x.toFloat()}"] = heightMap[x + (z * size)]
    }
}

I thought that the height would corelate directly to a y value that I could use. My function below does not even come close to being on the terrain.

fun placeOnTerrain(light: Light, x: Float, z: Float) {
        when (light.type) {
            Light.Type.Probe -> {
                val lightProbe = (light as LightProbe)
                val height = heightMap.getHeightAt("$z","$x")
                lightProbe.position = Vector3f(x, height, z)
            }

            Light.Type.Directional -> {}
            Light.Type.Point -> {}
            Light.Type.Spot -> {}
            Light.Type.Ambient -> {}
            null -> {}
        }
    }

Does the height value directly corelate to a y value? If so what am I missing? Is there a better way to accomplish this task? Is there a formula I can use to get the y from the height as I see this in the setHeightAtPoint()?

public void setHeightAtPoint(float height, int x, int z) {
    heightData[x + (z * size)] = height;
}

You could perform a raycast to get the height of the terrain.

CollisionResults results = new CollisionResults();
Ray ray = new Ray(positionToTest, Vector3f.UNIT_Y.negate());
terrainSpatial.collideWith(ray, results);
if (results.size() > 0) {
    Vector3f pointOfCollision = results.getClosestCollision().getContactPoint();
}
1 Like

What is “heightMap” for a type? Maybe it uses an interpolated height? For example, TerrainQuad has getHeightmapHeight and HeightMap has getTrueHeightAtPoint.

1 Like

float getHeight(Vector2f xz)

Get the real-world height of the terrain at the specified X-Z coordinate.

Edit:
Another option would be to examine this technical paper I’ve prepared. A basic understanding of engine operations and JME terrain characteristics is necessary for a thorough understanding

This is the final outcome:

1 Like

Thanks for the ideas and suggestions. I figured there would likely have been a better approach to accomplish the task.

1 Like

I tried both getHeight() and heightmap height and got the same result as before. I finally got it working using the raycast.