- Home /
Formula for Terrain sampleHeight
I'm really struggling to find out how Unity interpolates between individual heights within my 2049x2049 height matrix. If, for example, you want to get the height of the terrain and the position you're looking for is halfway between 10,10 and 11,11 in the height map, I would assume that the height should be: find (height(10,10)+height(10,11))/2 and find (height(11,10)+height(11,11))/2, then add those two results together and divide by 2 (I'm saying 50% of the way between just to simplify my question). What happens is, if I'm on a flat part of the terrain, the result is right on. If I'm on a slight incline, the result is very close. If I'm on a steep gradient, the result can be as much as a couple meters off. What is going on? How does Terrain.activeTerrain.sampleHeight() calculate it?? Please keep in mind that in my explanation above, I'm not using world coordinates and I know sampleHeight uses world coords. I do that conversion from world width/height to 2049x2049 and I know that isn't the problem.
The reason I need to know this is, the scale of my MMO is becoming too large for Unity to handle the server side so, I want my C# code to run on it's own, without the Unity Engine. So, I'll have to calculate (on my own) the exact height on the terrain to place players and monsters. If I can figure this out, I'll be most of the way there. The only other thing holding me back from separating my server from Unity is that I depend on Unity's NavMesh for pathfinding right now.
I remember needing this too. See if this is helpful: http://answers.unity3d.com/questions/469151/reproducing-terrainsampleheight-for-custom-mesh-te.html
Your situation is different, but I'm pretty fuzzy on this kind of operation; been a while... it's gotta be something about how high a height value of 1 is on the terrain in world space; using that is the key for the space conversion.
You're not scaling the terrain axes separately, are you? Because this is exactly what would happen if you were.
I read that post. Thanks Sunny. I think I did come across that thread a while back but I wasn't sure if this was the same case. But, I just read it again and I guess my problem is that I am not interpreting anything as a triangle or quad. I'm only doing a weighted interpolation between the four points I'm standing within. I guess this is my problem. Below is output from my game. In this case, my height is only off by 0.8f, which is still significant though. The 3x3 matrix of values above that are all the heights in a 3x3 area around me, with the center value being the X,Y you would get by dropping the decimal portion of my calculated position on the height map. I guess I don't understand interpolating on quads or tris enough. I can't seem to figure out HOW one can get 212.8821 from this height map data :(
207.7825, 207.3613, 207.929
214.9057, 212.7815, 214.7226
221.443, 218.1469, 218.3666
sampleheight = 212.8821 my height = 212.0078
As far as scaling goes, I did check just now and my terrain is set to (1,1,1).
In the end, the terrain is a bunch of triangles; treating it like quads might indeed account for the behavior you're observing. You could perhaps visually inspect the terrain to see the orientation of the hypotenuse of each quad's pair of tris. You could probably discover the winding order experimentally and build off the info I discussed in that linked question.
If this operation is only necessary a handfull of times per frame, you could always raycast down at the terrain collider for the point. As a bonus, you'd get the normal without additional effort.
I suppose it would help if I mentioned the fraction part too so you know what values I'm interpolating on: X is 0.4578247 of the way to the next value on the height map and Y is 0.5102539 of the way to the next value. I'm not asking you to do calculations for me and figure it out but I'm mentioned this data just in case you're curious. I guess if you or anyone knows the general formula I can use based on my world location (or heightmap location), I can try it and see if it works. I greatly appreciate any help you can give :)
Your answer
Follow this Question
Related Questions
Problem with heightmap and terrain height maxing out at 10000, proportions *Solved* 0 Answers
Best way to get terrain positions from a given height range 2 Answers
Trying to tile terrain, encountering strange axis flipping 0 Answers
Terrain resolution for a MMO 4 Answers
Having some serious issues making height maps work. 1 Answer