Create objects in a loop

Hi gang,
Sorry for only contributing by not contributing and asking question after question, but I’m pretty new to the engine and java in general… nevertheless my today’s question is; Can I create similar (well, the same) objects (that change only with their position) in a loop? What I mean - I want to create for example 5 cylinders, that change only their local translation through z axis. The same problem is with lines, but that’s another story…
I sense that spatial system is the answer, but I’m not convinced only by myself. Can someone help?

something like this?

[java]final int NUM_OBJECTS = 5;
for (int i = 0; i < NUM_OBJECTS; ++i) {
Spatial model = assetManager.loadModel ("Models/cylinder.j3o");
model.move (0, 0, -i * 2 ); // every cylinder will be 2 Z units away from the last one created
rootNode.attachChild (model);


Thanks for the answer, that should do the trick. Still, I have two more questions:

Is cylinder.j3o default primitive or is this a model, that I have to create (in blender or some other program) beforehand? Because when I try to execute this I get AssetNotFound error… Or is this the path to the folder fault. I’m using jME in netbeans, just to clarify (although I don’t think that matters).

My second question is about lines (and partially about other figures). Conception is the same, but the difficulty for me comes with the fact, that I want to have lines moving not only through z axis but also with different height. I was using this code for simple line creation:
Mesh line1 = new Mesh();
line1.setBuffer(VertexBuffer.Type.Position, 3, new float[]{ 0, 0, 0, 0, 1, 0}); // here, if possible, I would like to change even up to 3 parameters (move through z axis - start and end, also transformation in x and y axis)

	line1.setBuffer(VertexBuffer.Type.Index, 2, new short[]{ 0, 1 });

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

So, is it even possible to create lines using this code? Can I, for example, export it as a default (or maybe create a function - like draw line() with some parameters and then us it in a loop? Or a better idea is to create lines with end in on, starting point and then move them in the loop?

Create a mesh (however you want).

Reuse it in as many Geometries as you want. They are shared.

1 Like

Thank you very much for all the help, it really accelerated my poor attempts to create something. :wink:
But, of course, I’m stuck with “next” step in my model. What I want to do, is, well, let the image speak for itself:

So, my goal is to create lines, that get different values from array (which I can do, but only for 1 line) and to be more than one of them (I can create a whole lot of them, but they’re just one and the same line, rotated). I thought about creating array of lines (is it even possible) and split them. Or maybe clone the base line and… Or maybe use spatials? The concept would be something like:

create line[1 verse, 1 column] -> move -> create second line[2 verse, 1 column] …
create line[1 verse, 2 column] -> …

Can someone maybe give me a hint? Any suggestions will be appreciated. :]

Depending on your needs, i would consider using a custom mesh (see wiki) with line mode, that way every two vertices are connected by a line.

I’m not sure i understand your image however.

1 Like
@Empire Phoenix said: [..] i would consider using a custom mesh (see wiki) [..]

1 Like
@normen said:

Oh haha ^^ we have a quick help link thingy now, that will come in handy.

haha, that quick link helper is nice =P now it’s even easier to “help” out in the forums =P

Thanks for response, Empire Phoenix. I thought that simple line would do the trick. I’ll look into this custom mesh. In the meantime, maybe this picture can clear some more (hopefully):

The color and thickness of the lines is (for now) for the presentation purposes only. So basically I want to add the lines (their length and number depends upon the values from array) but I want to rotate them, so the angle between them is the same (so the image is kinda screwed, because it should be 120 degrees between them). Hence the sort of circular shape from my previous post.

I would not be suprised if that cleared exactly nothing. ;p

Ok, so you have X lines with a given length, where X is determined by the size of the array, and the lenght by the values inside the array, yes?
If i understand the first picture, the long lie is just as a scale kinda like in a graph from array index 0 to max?
The other lines standing on it then represent the value of the first number?

So you basically have kinda like a Y (if i loock parralel to your axis?)
So if i get this correctly this is a 4 dimensonal graph? 1 axis depth and 3 other axis (red, lila, and blue)?
And you are stuck at the point on how to rotate those axis cleanly?

(I assume you are suing real 3d lines not 2d pseudo like the flat ones above are)?

I would start with 2d logic (only one array index)
Via angle calculations determine (quaternion rom angels) a quaternion that can rotate one 0,1,0 to the orientation of a red one, ignore lenght for now.
Now you can do all lines like the blue one and apply to the local rotation that quaternion and should get the old rotation + that relative rotation.

Eg blue rotation with one left rotation should give the rotation for the the red one. If you apply that rotation 2 times you should be 240° further, and with 3 times you should be at the original value again.

This should work at least if I understood correctly how quaternions work (and what you actually trying to do)

1 Like

Empire Phoenix, you’ve got it right. The long line is just kind of center point axis. Although the other lines represent next numbers in array, column-oriented (and here is my problem). Hopefully this image clears all confusion:

The values are, of course length of the each line. So as you can see I’ve many different values and cannot be sure that first, initial line has always value 1. So here it is what I’ve previously written - it works up to particular moment. I know that it looks horrible (3 for loops are almost blasphemy). I beg for forgiveness, I’m really bad at this…

float[][] array = new float[][]{{1,2,3},{1.1f,2.3f,3.13f},{0,2,3.3f},{1,2.1f,2.8f},{1.3f,2.4f,3.15f}};
final int obj=5; // total number of verses (from array)
float ile = 360/3 ; // by what degree I have to multiply
float grub = 0.5f; // just the distance between the lines

// first loop - total number of lines, for the example it will be 5
for (int i = 0; i < obj; ++i) {

//second loop - going through columns
for (int k=0; k<3; k++){
float value = array[i][k]; //here I load value from array, later I use in line end point description

//third loop, kinda redundant - for rotation calculations
for (float j=1; j<=360; j++){
Mesh p = new Mesh();
p.setBuffer(VertexBuffer.Type.Position, 3, new float[]{ 0, 0, 0, value 0, 0}); //
p.setBuffer(VertexBuffer.Type.Index, 2, new short[]{ 0, 1 });

	Geometry radiusGeometry = new Geometry("line", p);
	Material radius_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
                       radiusGeometry.setLocalTranslation(0, 0, 0);
                       radiusGeometry.move(0,0,(-grub)*i);  // just moving the lines by specified distance
                       radiusGeometry.rotate(0, 0, ((j*pi)/180)*ile);  // rotation by exactly 120 degrees


Nothing wrong with nested for loops - especially when iterating through nested data in multiple dimensions.

It’s what they are for :slight_smile:

Although having said that…I’ve no idea what you are using that inner loop for, it seems a bit odd.

The third loop - as you’ve pointed out and stated in the code - redundant. It’s true, I could live without it, but it’s my laziness - I use it to calculate from 1 to 360 degrees, just to not deal with radians, when invoking rotate() function. Now that’s called plain stupidity or evil. ;p

I don’t see why you need a loop for that…
… and anyway, why not just multiply the degree value by FastMath.DEG_TO_RAD to get radians?

1 Like

I thought that method was more accurate. Although now… holy shit (excuse my french), but that probably solved all the problems :open_mouth:
God damn, and to think my laziness was the biggest issue. Mr zarch, I can’t thank you enough! Also thanks to wezrule, pspeed and Empire Phoenix

In your case i would start with a very simple object, consisting out of 3 lines then. (in the right angles, all in 2d , so all z values = 0)

using the colors in the array above

so you should have
startpoint always at 0,0,0

endpoint at
black = 0,black,0
Since you have 120°, we can always say the the x is = the y (ignoring -)
point(-0.5,-0.5,0) would be (pythagoras) around pink of 0.7
so lets assume we have pink a with 2.
reverse phytagors with out assumtion
2^2 = 4
4/2 (a^2+b^2) assuming a=b -> a^2 = 2 = b^2
sqrt(2) = 1.4 = a = b

so pink is
and red

calcA(flaot rawvalue)
return sqrt(((rawvalue*rawvalue)/2))

Then stack multiple of that in a (Batch)Node in the z dimension, add a axis line with that goes in the z dimnsion as well(, call batch() ) and move the whole node wherever you need it.

I think this should do the trick

1 Like

Oh, hi there. Again, I’m having problems with similar issue and I’m stuck (that’s probably because my low understanding of geometry rules). And my only solution (for now) is turn to professionals. So, now, instead of lines. I’m rotating cylinders (or it’s spatials) around z-axis. My problem is - the initial/default cylinder position is along z-axis (maybe a better explanation will be a picture). So, first, I rotate the figures so they are along x-axis (so around 90 degrees) and, to achieve my goal another 90 degrees, but with a minus. But the problem is, when I do this, I end up as shown in the graphic - the (0,0,0) is in the middle (so when I want to rotate and create other objects it rotates their halves, if it makes any sense). I rotate, using pivot rotation around specified Quaternion. And to rotate I have to change LocalTranslation to (0.5f,0f,0f). Nevertheless, my goal and question is - how do I achieve that kind of rotation, as seen in the picture?

There is also other thing I wanted to ask (about collisions) - can I create (a better solution would be a non-visible thing) a ray, object or a specifc line of sight, which, when I select first spatial/object then selects (or just simply apply the same instructions that I did to the selected, like changing material - nothing too complicated) through all the objects that are on “the same path” as first, selected one?

Thank you very much for any suggestions.

Place the cylinder in a node. Set the local translation to move the cylinder so the origin of the node is at one end of the cylinder.

Rotate the node.

Go back through the scene graph tutorial (particularly scene graph for dummies) again. :slight_smile:

1 Like