Place Spacial on a given terrain

I create this simple class that place a given Spatial correctly on the terrain (according to y height)



it would be interesting i think to put it in jme.



here is the code:



import java.util.logging.Logger;

import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jmex.terrain.TerrainBlock;

public class SpatialOnTerrain extends Node {
    private static final Logger logger = Logger.getLogger(SpatialOnTerrain.class
            .getName());

    private static final long serialVersionUID = 1L;

    //reference to the level terrain for placement
    private TerrainBlock tb;
    //
    private Spatial model;

    private float modelHeight = 0.0f;

    /**
     * Constructor builds the node, taking the terrain as the parameter. This
     * is just the reference to the game's terrain object so that we can
     * randomly place this node on the level.
     * @param tb the terrain used to place the node.
     */
    public SpatialOnTerrain(String name, TerrainBlock tb, Spatial model) {
        super(name);
        this.tb = tb;
        setModel(model);
        this.attachChild(model);
        this.updateGeometricState(0, true);
        this.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
    }

    /**
     * reset randomly places the node
     * on the terrain.
     *
     */
    public void reset() {

        placeNodeOnTerrain();
    }

    /**
     * place node picks a random point on the terrain and places the node there. I
     * set the values to be between (45 and 175) which places it within the force field
     * level.
     *
     */
    public void placeNodeOnTerrain() {
        float x = 45 + FastMath.nextRandomFloat() * 130;
        float z = 45 + FastMath.nextRandomFloat() * 130;
        float y = tb.getHeight(x, z) + modelHeight;
        localTranslation.x = x;
        localTranslation.y = y;
        localTranslation.z = z;
    }

    public void placeNodeOnTerrain(Vector3f vector3f) {
        localTranslation.x = vector3f.x;
        localTranslation.y = vector3f.y;
        localTranslation.z = vector3f.z;
    }

    public Spatial getModel() {
        return model;
    }

    private void setModel(Spatial model) {
        this.model = model;
    }

    public float getModelHeight() {
        return modelHeight;
    }

    public void setModelHeight(float modelHeight) {
        this.modelHeight = modelHeight;
    }

}



regards

Here in user code is a great place for it.  Thanks!

I don’t know how i should take it? is it that bad ?  (kidding)  :mrgreen:



by the way, here is a screen shot:

No, just that a random terrain population tool is well suited for the wiki or forum, but not so much for the engine core.