1

I am making a game in unity that allows you to destroy terrain, The problem I am having is that the farther away that I get from the origin (0,0), The more inaccurate the point where the destruction occurs is. Is there a way to make it more accurate?

// Use this for initialization
void Start ()
{
    //The width and height of the heightMap
    xRes = TerrainMain.terrainData.heightmapWidth;
    yRes = TerrainMain.terrainData.heightmapHeight;

    //Getting the heightMap points
    saved = TerrainMain.terrainData.GetHeights(0, 0, xRes, yRes);
    heights = TerrainMain.terrainData.GetHeights(0, 0, xRes, yRes);
}

void TerrainDestruction()
{

    Vector3 actualPosition;

    actualPosition.x = MyPosition.x;
    actualPosition.y = MyPosition.y;
    actualPosition.z = MyPosition.z;

    //Getting the x and y position of the player
    int xPosition = (int)(actualPosition.x);
    int zPosition = (int)(actualPosition.z);



    Debug.Log("x: " + xPosition);
    Debug.Log("z: " + zPosition);

    heights[zPosition, xPosition] -= 0.001f;


    TerrainMain.terrainData.SetHeights(0, 0, heights);




    TerrainMain.terrainData.SetHeights(0, 0, heights);

}

void OnApplicationQuit()
{
    TerrainMain.terrainData.SetHeights(0, 0, saved);
}

// Update is called once per frame
void Update ()
{
    //Getting the actual Position of the player with respect to the terrain
    MyPosition = MyMouse.mousePosition;

    if (Input.GetMouseButtonDown(0))
    {
        TerrainDestruction();
    }
}
Vaillancourt
  • 16,325
  • 17
  • 55
  • 61
Jake
  • 11
  • 1
  • The usual solution to precision issues far from the origin is to re-center the world and player back to the origin anytime they stray more than a certain distance away (moving everything at once makes it look like nothing moved at all). Have you tried this, or does it not apply for your particular case? (Also, how far are we talking? If it's under 16000 units or so then your problem might be something other than floating point precision) – DMGregory Mar 11 '17 at 23:37

1 Answers1

1

From what I understand of your vague question... Your player's position is a float. You're trying to keep pixel-level precision on texture/terrain edits.

So what happens is, the further you are from the texture/terrain's origin point, the more inaccuracy piles up?

If that's so: This is just how floats work. Floats jump in value by several units or something, and dynamically scale their decimal point.

Basically, if you're between coordinate 0f and 1f, you get like 0.00001f precision. But if you're between coordinate 0f and 10000000f, you get like 1.0f precision.

Floats are, like, 32bit, meaning your accuracy can just represent 2147483647 different coordinates. But float can be larger than that.

That, and floats have an annoying problem with being indecisive about their value. Hence why most software that uses floats keeps them between 0f and 1f for the high "approximate" decimal precision.

Basically floats are garbage for precision, get some 64 bit longs in there. In programming, you need to design TO the computer and language; not twist the language to your design. Think of your SMALLEST coordinate and LARGEST coordinate, then figure out which data types are suitable to that.

Sorry if my answer sounds like trash, I tried :(