Control for adjusting height not always working properly

Hi, I’ve created a Control for terrain that should gradually set the heightmap of a terrain to a specified height on a position defined by Vector3f loc.

It basically works most of the times, but I also get weird behaviour, like many spikes rising from the floor…

I suspect this has something to do with the interpolation of the loc.

(I’ve also tried with Terrain.setHeight(), but it skip to the final result)

This is the code of the control:

[java]public class RullalControl extends AbstractControl{

Vector3f loc;
TerrainQuad terrain;
float radius=64;
float tpf=0;
List<Vector2f> locs = new ArrayList<Vector2f>();
List<Float> heights = new ArrayList<Float>();
float height=10;

public RullalControl(Vector3f loc, TerrainQuad terrain){
    this.loc=loc;
    this.terrain=terrain;
    //start = System.currentTimeMillis();
}

@Override
protected void controlUpdate(float tpf) {
    
    //long tTime=System.currentTimeMillis()-start;
    //radius-=(tpf);
    
    // offset it by radius because in the loop we iterate through 2 radii
    int radiusStepsX = (int) (radius / terrain.getLocalScale().x);
    int radiusStepsZ = (int) (radius / terrain.getLocalScale().z);

    float xStepAmount = terrain.getLocalScale().x;
    float zStepAmount = terrain.getLocalScale().z;
    
    for (int z = -radiusStepsZ; z < radiusStepsZ; z++) {
        for (int x = -radiusStepsX; x < radiusStepsX; x++) {

            float locX = loc.x + (x * xStepAmount);
            float locZ = loc.z + (z * zStepAmount);

            //if (isInRadius(locX - loc.x, locZ - loc.z, radius)) 
            {
                // see if it is in the radius of the tool
                float hLocal=terrain.getHeight(new Vector2f(locX, locZ));
                if ((hLocal-height)>=1){
                    heights.add(-0.5f);
                }
                else if((hLocal-height)<0){
                    heights.add(+0.5f);
                }
                else heights.add(0f);
                //float h = calculateHeightRulla(radius, height, locX - loc.x, locZ - loc.z);
                locs.add(new Vector2f(locX, locZ));
                //heights.add(height);
            }
        }
    }
    terrain.adjustHeight(locs, heights);
    //System.out.println("Modified "+locs.size()+" points, took: " + (System.currentTimeMillis() - start)+" ms");
    heights.clear();
    locs.clear();
    terrain.updateModelBound();
    this.tpf+=tpf;
    if (this.tpf>5){
        spatial.removeControl(this);
    }
}
private boolean isInRadius(float x, float y, float radius) {
    Vector2f point = new Vector2f(x, y);
    // return true if the distance is less than equal to the radius
    return point.length() <= radius;
}
@Override
protected void controlRender(RenderManager rm, ViewPort vp) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
[/java]