getHeightAtCoordinates does not return good value when the land is not in the center of the world

Hi,

I’ve had a problem with getHeightAtCoordinates() for several days that I haven’t been able to resolve.

Explanation :

When we create a terrain and give it a position 0,5,0 getHeightAtCoordinates works as it should. But if I move the terrain on the x and z axis, getHeightAtCoordinates no longer behaves correctly.

Why am I moving the terrain ?

For my project, I created several plots of land one next to the other of 512x512 in order to create a large terrain of 2048x2048.
I then have a tool (a brush) to paint the textures (I use MixMaterial) This brush so when it is on a terrain far from the center of the world obtains coordinates which exceed 512 (size of a terrain). This is where getHeightAtCoordinates is no longer correct.

In this PG I created two examples :

  • Example 1: shows a terrain that I moved to -110, 5, 110
  • Example 2: He is in comments. This works by setting the ground to coordinate 0, 5, 0.

Thanks for your help.

Seems to me like it is working a expected (unless I am missing something from your playground), what value would you expect in this case?

Hello :slight_smile:

On my side I don’t see any problem on the value returned by getHeightAtCoordinates
Have a look at this Playground I did from your code : I placing a sphere on the returned value, to make sure. I’m moving the ground arround, and also the picked coordinate is not zero (10,10)

++
Tricotou

Indeed, you are right. I thought I was onto something on my project. I’m going to continue my research into what’s wrong with my project.

Just in case it helps to debug : API about GroundMesh.getHeightAtCoordinates precises this : returns the ground y position if (x, z) are outside the ground surface.

In your previous playground by adding a sphere on the picked point I see that you are picking the exact limit

I’m not sure I understand what this means: returns the ground y position if (x, z) are outside the ground surface.

how would she be outside the ground surface ? The terrain must be greater than 0 in Y? This could be my problem. Some of my terrain are -10.

In fact I think it comes from the conversion.

My terrains are created with 3ds max, so they are meshes and not terrains. Then I convert them using this function:

convertMeshToGround(groundMesh, size, subdivision, isUpdatable, scene) {	
		const ground = new BABYLON.MeshBuilder.CreateGround(groundMesh.name, { width: size.x, height: size.z, subdivisions: subdivision }, scene);	
		
		const vertexData = BABYLON.VertexData.ExtractFromMesh(groundMesh, true);
		vertexData.applyToMesh(ground);
		
		// Rend les données des vertices modifiables
		ground.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.PositionKind, true);
		ground.markVerticesDataAsUpdatable(BABYLON.VertexBuffer.NormalKind, true);

		// Rafraîchit les informations de collision pour le terrain		
		ground.updateCoordinateHeights();
		
		groundMesh.dispose();

		return ground;
	}

Do you see something missing so that coordinating them is correct?

Here’s what’s happening on my project.

If I place a sphere to know the least Y that I should have, it would be 3.5.
But with getHeightAtCooerdinate, the value I get back is 0.03xx

I tried to set the terrain to 0 in Y, so it goes up by 10 units. and my sphere is 13.5. The value of getHeightAtCooerdinate is -1.5xx

It’s completely weird. I don’t know what’s going on, maybe it’s the conversion function between 3ds max and Babylon.Ground

I dropped the terrain conversion. This creates too much worry for me.
I’m going to change my approach to create the terrain so that I no longer have this problem.