Randomize images as textures in 3D solids

Asked

Viewed 43 times

3

Good, I have 30 solids and I have a different image for each of them. I created one array for the images, and if you choose to use the same image at all (as I left here in the code, use the bandeira [0]) he runs and everything is fine!

I now want to make one Random running through the array of the images and that assigns a different image to each of the solids, but for some reason when I tried to make (int index = int(random(0, bandeira.length)); - and take the index in place of [0]) he continues to create solids all with the same image.

MAIN

int numPaises = 30;

float theta = 0;
PImage logo, chao;
PShape pessoa;

Paises [] solidos = new Paises [numPaises];  //Array solidos!

PImage[] bandeira = new PImage[30];


void setup() {

  bandeira[0] = loadImage("portugal.jpg");
  bandeira[1] = loadImage("espanha.jpg");
  bandeira[2] = loadImage("franca.jpg");
  bandeira[3] = loadImage("italia.jpg");
  bandeira[4] = loadImage("inglaterra.jpg");
  bandeira[5] = loadImage("alemanha.jpg");
  bandeira[6] = loadImage("brasil.jpg");
  bandeira[7] = loadImage("estadosUnidos.jpg");
  bandeira[8] = loadImage("china.jpg");
  bandeira[9] = loadImage("russia.jpg");
  bandeira[10] = loadImage("quenia.jpg");
  bandeira[11] = loadImage("argelia.jpg");
  bandeira[12] = loadImage("africaSul.jpg");
  bandeira[13] = loadImage("armenia.jpg");
  bandeira[14] = loadImage("austria.jpg");
  bandeira[15] = loadImage("bahamas.jpg");
  bandeira[16] = loadImage("belgica.jpg");
  bandeira[17] = loadImage("benin.jpg");
  bandeira[18] = loadImage("bulgaria.jpg");
  bandeira[19] = loadImage("chile.jpg");
  bandeira[20] = loadImage("congo.jpg");
  bandeira[21] = loadImage("cuba.jpg");
  bandeira[22] = loadImage("filandia.jpg");
  bandeira[23] = loadImage("jamaica.jpg");
  bandeira[24] = loadImage("noruega.jpg");

  size (1200, 650, P3D);

  for (int j=0; j < solidos.length; j++) {
    float angle = j * TWO_PI / numPaises;
    float x = 0 + 2000 * cos(angle);  
    float z = 0 + 2000 * sin(angle);
    int index = int(random(0, bandeira.length));     // *******
    solidos[j] = new Paises(x, 700, z, bandeira[0]);
  }
}

void draw() {

  background (0);

  scale(0.8); //escala de visão
  translate(700, 0, -3500);

  for (int j =0; j < solidos.length; j++) {
    solidos[j].render();
  }

  theta += 0.020;
}

CLASS COUNTRIES

class Paises {
  float x, y, z;
  float w, h, d;

  //CONSTRUTOR

  Paises(float nx, float ny, float nz, PImage img ) {
    x =nx;
    y = ny;
    z =nz;
    w= random(130, 200);
    h= random(400, 500);
    d= random(130, 200);

    noStroke();

    pessoa = createShape(BOX, w, h, d);
    pessoa.setTexture(img);
  }

  void render() {
    pushMatrix();
    rotateY(theta);
    translate(x, y-h/2, z);
    shape(pessoa);
    //box(w, h, d);
    popMatrix();
  }
}
  • Shouldn’t be solidos[j] = new Paises(x, 700, z, bandeira[index]);?

  • that’s what I tried, just left the [0], to say that if you choose 1 in particular it works correctly with all of them, is evidently and always the same.

  • try declaring "person" in the scope of the class, it may be that you are rewriting on top of the same object (there would be only the last flag). Take advantage for [Dit] the question and put more relevant language tags.

  • Thank you very much, it’s already working properly :)

  • what was the solution? You can use the field below to post as answer if you want.

  • was really getting the last flag! thanks again :)

Show 1 more comment

1 answer

2

The problem is that the way pessoa had been declared, the class was overwriting its value to each instance.

It is already working correctly, just move the declaration of pessoa inside class:

class Paises {

  PShape pessoa;
  float x, y, z;
  float w, h, d;

in the setup() gets like this:

int index = int(random(0, bandeira.length));     
solidos[j] = new Paises(x, 700, z, bandeira[index]);
  • I made a slight adjustment in the answer, if you think it is not good, you can [Edit] again, or revert to your previous version at this link: http://answall.com/posts/135240/revisions

  • no problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.