x


How to translate WORLD coordinates to TERRAIN coordinates?

Recently I've begun messing around with altering the terrain in real-time. One of the hiccups I've run into is that world coordinates are not the same as terrain coordinates (obviously). How can I take a position in the world and translate it to the nearest point on the terrain that I can then alter the height of? I hope my question is clear enough. Any advice is appreciated!

more ▼

asked Dec 20 '09 at 06:44 PM

user-614 (google) gravatar image

user-614 (google)
23 1 1 6

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

First, subtract the terrain gameobject's world position, then divide by your terrain's size (which you can access in terrainData.size). This will give you a position with values somewhere in the range of zero to one.

You can then multiply this by your heightmap resolution (using the terrainData heightmapWidth and heightmapHeight variables), and then convert to integers to find the correct heightmap array position to feed into the GetHeights and SetHeights functions.

more ▼

answered Dec 20 '09 at 07:15 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor) { Vector3 vecRet = new Vector3(); Terrain ter = Terrain.activeTerrain; Vector3 terPosition = ter.transform.position; vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z = ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight; return vecRet; } Is this right? or i should change it like this: vecRet.x *= ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;

Mar 12 '11 at 12:30 PM Yanger_xy
(comments are locked)
10|3000 characters needed characters left

You could also do a terrain.collider.Raycast:

RaycastHit intersection = new RaycastHit();
Ray originUp = new Ray(new Vector3(0,0,0), Vector3.up);
terrain.collider.Raycast(originUp, out intersection, 100000.0f);
if (intersection.collider != null)
{ ball.transform.position = intersection.point;
ball.rigidbody.position = intersection.point;
}

more ▼

answered Jan 12 '10 at 03:03 PM

darkmath gravatar image

darkmath
2 1 1 1

That just gives a world position again. Terrain position is the little blue squares when you paint it.

Apr 22 '11 at 07:45 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left
private Vector3 ConvertWordCor2TerrCor(Vector3 wordCor)
{
    Vector3 vecRet = new Vector3();
    Terrain ter = Terrain.activeTerrain;
    Vector3 terPosition =  ter.transform.position;
    vecRet.x = ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth;
    vecRet.z = ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;
    return vecRet;
}

Is this right? or i should change it like this: vecRet.x *= ((wordCor.x - terPosition.x) / ter.terrainData.size.x) * ter.terrainData.alphamapWidth; vecRet.z *= ((wordCor.y - terPosition.z) / ter.terrainData.size.z) * ter.terrainData.alphamapHeight;

more ▼

answered Mar 12 '11 at 12:30 PM

Yanger_xy gravatar image

Yanger_xy
270 22 25 33

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5057
x1467

asked: Dec 20 '09 at 06:44 PM

Seen: 4987 times

Last Updated: Dec 20 '09 at 06:44 PM