Generating and randomly placing many of the same node

Hello, this is my first post!

I'm trying to generate many copies of the same tree model (that I've managed to import from 3Dsmax) and place them randomly on the map.  I've only ever been able to get one tree in, but its floating in the air and doesn't rotate like I tell it to  :?



Here is my source code for the class I made:


public class Trees extends Node {

    int t = 0;
    TerrainPage tp;
    ArrayList<Vector3f> olde;
    Node TreeA;
    Quaternion rotate90;
   
    public Trees(TerrainPage tp)
    {
        super("tree");
        children = new ArrayList<Spatial>(21); 
        rotate90 = new Quaternion();
        rotate90.fromAngleAxis(FastMath.PI/2, new Vector3f(0,1,0));
        this.tp = tp;
        TreeA = LoadModel.loadModel("Models/tree.3ds");
        System.out.println(TreeA.getTriangleCount());
        olde = new ArrayList<Vector3f>();
    }
   
    public void plantTree()
    {
        float x = 0 + FastMath.nextRandomFloat() * 10;
        float z = 0 + FastMath.nextRandomFloat() * 10;
        float y = tp.getHeight(x,z) + 0f;
        if(t==0)   
        {
                    t++;
                    olde.add(new Vector3f(x,y,z));
                    TreeA.setLocalTranslation(x, y, z);
                    Node tree = new Node();
                    tree = TreeA;
                    tree.setLocalRotation(rotate90);
                    children.add(tree);
        }
        else
        {
            check:
            for(int i = 0;i<t;i++)
       
            {
                if(new Vector3f(x,y,z).equals(olde.get(i))==false)
                {
                    t++;
                    olde.add(new Vector3f(x,y,z));
                    TreeA.setLocalTranslation(x, y, z);
                    Node tree = new Node();
                    tree = TreeA;
                    tree.setLocalRotation(rotate90);
                    children.add(tree);
                    break check;
                }
                else
                {
                    plantTree();
                }
            }
        }
    }
    public void plantTree(float x, float z)
    {
                    float y = tp.getHeight(x,z) + 0f;
                    t++;
                    olde.add(new Vector3f(x,y,z));
                    TreeA.setLocalTranslation(x, y, z);
                    Node tree = new Node();
                    tree = TreeA;
                    tree.setLocalRotation(rotate90);
                    children.add(tree);   
    }   



As well, setting the local scale lower doesn't seem to affect the size of the tree.  Its absolutely gigantic.
Help would be very much appreciated!

Use the method getHeightFromWorld(Vector3f v). Otherwise the Y values get messed up because it's the TerrainPage's coordinates and not world coordinates.

I did what you said and it doesn't seem to make a bit of difference :(  There is still one gigantic, sideways, solitary tree floating in the air, and its always generated in the same place.

Oh–it's rotated that way because it's a 3DS file. 3DS models don't have their axes aligned how JME does, so you have to rotate it so it matches up.



As for scale, have you tried making it REALLY small? Like .0001f?



Why have you only been able to get 1 tree in? I am almost sure there's some Tree demo, as well…

how would I go about rotating it so it matches up? Do I have to do that from within 3DS Max?



Making the scale .0001f makes no difference :/  Is it also because its a 3ds file?

It only displays 1 tree because you only created the tree model once…



                    Node tree = new Node();
                    tree = TreeA;



You aren't creating a new tree every time you call plantTree().

If you want to create a new tree every time, you will either need to load the model again or clone the model's information. You might want to look at this thread: http://www.jmonkeyengine.com/jmeforum/index.php?topic=7408.0.

Also, you need to call Node.updateGeometricState() every time you update the scale.

EDIT: You can also use CloneImportExport to clone your model.

Import the model without rotation, and then see how you'll need to rotate to get it to stand upright. Then make a Quaternion from the same method as before and apply it to the tree using setLocalRotation(quat);

alright, I figured out all the problems except for the gigantism.  Thanks to both of you!

The updateGeometricState() didn't work?

i think you don't scale it :slight_smile:


Yeah it's not in the code you posted… I figured you probably have it elsewhere though.



Where are you calling it?

I had it elsewhere, but I'm putting it anywhere I can plug it in and its not having effect.  Even with the updateGeometricState().  (the parameters I'm putting in updateGeometricState are 0 and true, is that the problem?)



EDIT: Just as I say that, I got it working! Once again, thanks

Just so people know, the updateGeometricState arguments should be tpf and true, not 0.

To use several same model, use SharedMesh or SharedNode.