Creating multiple objects with arrays

I want to create about 6 bricks with same properties. So I tried to create them with arrays.
Here is the code snippet:

Box box = new Box(Vector3f.ZERO, 0.5f, 0.5f, 0.5f);
brick = new Geometry(“brick”, box);
brick.setMaterial(mtl);

Node BrNode = new Node();

for(i=1;i>=6;i++){
bricks[i]= brick.clone();
BrNode.attachChild(bricks[i]);
bricks[i].setLocalTranslation(i/2,0,-2);
}
rootNode.attachChild(BrNode);
When running the program Every thing else except these bricks appear in the scene. There are no bricks. Also I don’t get any Error message.
I do not know what is the problem?
Is there any other way to create multiple objects o9f same type.(Geometry or Spatials)

leorius

@leorius said: for(i=1;i>=6;i++){

Look really closely at that.

Note: one popular way of debugging is either to put System.out.println() or set breakpoints and run in a debugger to see if the code is even being run. If you’d done that in this case then you’d see that the loop is never entered.

1 Like

i have never used a debugger. is there one available for use in jMP?

@Decoy said: i have never used a debugger. is there one available for use in jMP?

The SDK is based on netbeans so this tutorial should apply even though it says netbeans: Bad URL | School of Computing

1 Like

@jmaasing thanks. exactly what i needed.

Yes, it is true. I tried with system.out.println() as well as debugger. in both cases I found that the for loop is not executed. But, I do not know why. what is the reason? How rectify this. (OR) is there any other method to create multiple copies of the same object( spatial or geometry) . I need about 20 bricks which I can refer in collisions separately.

leo

@leorius said: Yes, it is true. I tried with system.out.println() as well as debugger. in both cases I found that the for loop is not executed. But, I do not know why. what is the reason? How rectify this. (OR) is there any other method to create multiple copies of the same object( spatial or geometry) . I need about 20 bricks which I can refer in collisions separately.

leo

Look closely at:
for(i=1;i>=6;i++){

…and well me what you think it does.

Thank you very much. in the code it should be
for(i=1;i<=8;i++)
now I got the bricks.
Now I have a problem with collision detection.
here is my code:
for(i=1;i<=8;i++){
if(ball.getModelBound().intersects(bricks[i].getModelBound()))
{
vz=-vz;
rootNode.detachChild(bricks[i]);
System.out.println(bricks[i]+“removed”);
}
}
Now while running the program the bricks are not removed when collision happens. But once all the eight bricks are hit then all the bricks are removed all at a time.

here is the log:

Bricks1 (Geometry)removed
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
Bricks2 (Geometry)removed
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
Bricks3 (Geometry)removed
INFO: Root Node (Node): Child removed.
Oct 2, 2013 10:50:42 PM com.jme3.scene.Node detachChildAt
Bricks4 (Geometry)removed
INFO: Root Node (Node): Child removed.
Bricks5 (Geometry)removed
Bricks6 (Geometry)removed
Bricks7 (Geometry)removed
Bricks8 (Geometry)removed

I do not find what is wrong in the code. Actually each brick should be removed when that brick is hit by the ball.
What is the problem.
I am actually trying a 3D breakout game now.

Leo

getModelBound() will return the bricks bounds in local space. You probably want the bounds in world space, I guess.