Random generation of spatials


Can you give a hint how to generate randomly cloned spatial?

  1. generate randomly on a mesh (for example Box (10,10,10) geometry )
  2. generate ramdomly on an area (for example I have 2 Vectors (0,0,0; 10,0,10))

    For example, I have:

    [java]Spatial geom;

    int ammountOfClone = 1000;



Math.Random() + minimum * (maximum-minimum) ?

and that vor every component of the vector?

That what you meant?

I’m not sure I’m following the question, but wouldn’t something like this work?


Vector3f center = new Vector3f(0, 0, 0);

Vector3f extent = new Vector3f(10, 10, 10);

for(int i=0; i<amountOfClone; i++){

Spatial clone = geom.clone(); // I’m not sure about object cloning in jME3… this may or may not work

float xValue = (float)(Math.random()*extent.x);

float zValue = (float)(Math.random()*extent.z);

clone.setLocalTranslation(new Vector3f(xValue, 0, zValue));



EDIT: @EmpirePhoenix You beat me by 14 seconds!

Guys, THANKS A LOT!!! And how to clone on a mesh (on polygons)?

@EmpirePhoenix , what do you mean about “reuse”?

[java]for (…) {

Spatial clone = geom.clone();

//is the same like

// you said: new Geometry(“name”,mesh)

Geometry clone = new Geometry(“clone”+i, geom.clone());


also why clone geometry?

why not just new Geometry(“name”,mesh) and reuse the mesh ?

Also YAY 14 seconds win :slight_smile:

Mesh box = new Box(whatever);


newone = new Geometry(“clone_n” , box);


no need to call clone on unanimated Meshes, they can be shared.

Sorry, but as far as I understand in instancing: clone() method shares its geometry (and mesh too).

Am I wrong?

And deepClone() method shares its Material, but does not share with its Mesh.

As Momoko_fan said: “Note that clone() will share the meshes if they are not animated, and duplicate them if they are animated.”

There: http://hub.jmonkeyengine.org/groups/general-2/forum/topic/spatial-clone-question/

Using clone() is preferred if e.g. you set some parameters on the geometry itself like its material or scale. Otherwise clone() is the same as sharing the mesh (for non-animated models)

sorry for long delay… I want to thnk you about the random generation tip.

Also, about cloning I noticed that clone(false) is very cool option. It shares a mesh and a material. :slight_smile:

Here is my code for those who will be interested in it:



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.shape.Box;

public class GenerateFieldOfInstances extends SimpleApplication {

public static void main(String[] args) {

GenerateFieldOfInstances app = new GenerateFieldOfInstances();



Geometry geom;

Node instNodes = new Node();


public void simpleInitApp() {

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

geom = new Geometry(“Box”, b);


Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setColor(“m_Color”, ColorRGBA.Red);



int numClones = 500;

for (int i=0; i<numClones; i++) {

Geometry gm = geom.clone(false);


gm.setLocalTranslation((float) Math.random() * 100.0f - 50f,(float) Math.random() * 10.0f,(float)Math.random() * 100.0f - 50f);

gm.rotate(0, (float) Math.random() * (float)Math.PI, 0);




mat.setColor(“m_Color”, ColorRGBA.Blue); //Check if material is shared by all objects





Another question is how to generate instances on a Mesh/Geometry? As far as I understand I should make Rays and Collision Detection with the Mesh/Geometry. But how to get the area of the Geometry to make Rays?


Uploaded with ImageShack.us