Multiple instances of a PShape in a GROUP

edited November 2016 in Library Questions

I understand from reading the JavaDocs, that Translate is not allowed within a createShape. I tried it anyways in the code below and it does not work, so my question is, what do I use to translate the multiple children that I want to add to the GROUP. I have also done some troubleshooting and it appears that no matter how many addChild() I use, it only adds a single instance of the "cube" that I created. My goal is to make 6 of the cubes in different colors and add one of the 6 cubes based on criteria, but one step at a time, first, how do I make a 20X20X20 3D matrix of these cubes. I can do it by drawing all of the cubes, but I am trying to make this completely object oriented.

I have tried commenting out the PeasyCam in order to make sure it not causing any issues. If I comment out the cam= statement, the program still only draws out ONE cube, but it will be in the top left corner instead of the center due to PeasyCam coordinates.

Any and all suggestions are welcomed.

import processing.opengl.*;
import peasy.*;
PeasyCam cam;
PShape cube, world, world2;
float CS = .25;
int CPS = 20;
float offset = 0;

void setup()  { 
  size(640, 640, P3D); 
  cam = new PeasyCam(this, CPS*10);  //If commented out, cube moves to top left corner

  cube = createShape();
  cube.fill(0x44FF0000); cube.vertex(-CS,  CS,  CS);cube.vertex( CS,  CS,  CS);cube.vertex( CS, -CS,  CS);cube.vertex(-CS, -CS,  CS);
  cube.fill(0x4400FF00); cube.vertex( CS,  CS,  CS);cube.vertex( CS,  CS, -CS);cube.vertex( CS, -CS, -CS);cube.vertex( CS, -CS,  CS);
  cube.fill(0x440000FF); cube.vertex( CS,  CS, -CS);cube.vertex(-CS,  CS, -CS);cube.vertex(-CS, -CS, -CS);cube.vertex( CS, -CS, -CS);
  cube.fill(0x44FFFF00); cube.vertex(-CS,  CS, -CS);cube.vertex(-CS,  CS,  CS);cube.vertex(-CS, -CS,  CS);cube.vertex(-CS, -CS, -CS);
  cube.fill(0x4400FFFF); cube.vertex(-CS,  CS, -CS);cube.vertex( CS,  CS, -CS);cube.vertex( CS,  CS,  CS);cube.vertex(-CS,  CS,  CS);
  cube.fill(0x44FF00FF); cube.vertex(-CS, -CS, -CS);cube.vertex( CS, -CS, -CS);cube.vertex( CS, -CS,  CS);cube.vertex(-CS, -CS,  CS);

  world.addChild(cube);// This is the cube in the center or top left corner.
  for (int i = 0; i < CPS; i++) {
      for (int j = 0; j < CPS; j++) {
          for (int k = 0; k < CPS; k++) { 
          cube.translate(i,j,k);               //This is not allowed and does not work.
          world.addChild(cube);           //This only adds the cube once.

void draw()  { 


  • edited November 2016

    Cube is a reference. I think you're adding the same reference to the world multiple times and every time you call translate you update ALL the references at the same time.

    Will think of a way around this.

  • Answer ✓

    Move lines 15-23 into the loop and make cube local to that loop to ensure all the cubes are distinct instances.

  • Thank you koogs for your response. My eventual goal is to have have 24 reference cubes, so I think I will have to move it to a Class and only create the 1 I need and not all 24 every loop. Let me go with this and see what happens. THANKS

Sign In or Register to comment.