Make a object class with random size

I’m trying to make the size random in an object class Peixe (designed with BeginShape, and moving with pop Matrix ). I tried with scale, but didn’t work. Please Could you help me?


void setup() {
  size (1000, 800);
  peixes = new ArrayList<Peixe>();
  for (int i=0; i <10; i++) {Preformatted text
  
  peixes.add(new Peixe( random(width), random(height), random(3,7)));
  }
}//retirei a variável cor, pois como o fill() está dentro da forma, não mudava a cor. Também não sei como variar o tamanho, pois os números do vertex é q desenham o peixe por meio de triângulos
void mousePressed(){
Peixe peixe_novo = new Peixe( mouseX, mouseY, 3);
peixes.add(peixe_novo);
}
void keyPressed(){
int num_peixes = peixes.size();
if (key == ' '  && num_peixes >1 ){
peixes.remove(num_peixes - 1);
}

}


void draw() {
  background(0);
  for (Peixe p : peixes) {
  p.display();
  p.drive();
  }
}

class Peixe{
color c;
float xpos;
float ypos;
float xspeed;

Peixe( float tempXpos, float tempYpos, float tempXspeed){

xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;

}
void display(){
  pushMatrix();
  translate(xpos, ypos);
  strokeWeight(2);
beginShape(TRIANGLES);
fill(14,224,155);
vertex(100,300);
vertex(250,200);
vertex(250,300);
fill(161, 3, 170);
vertex(250,200);
vertex(250, 300);
vertex(500,300);
fill(14,224,155);
vertex(500,300);
vertex(550,250);
vertex(550, 350);
fill(14,224,155);
vertex(100, 300);
vertex(150, 350);
vertex(500, 300);
fill(161, 3, 170);
vertex(150, 350);
vertex(220,420 );
vertex(350, 321);
fill(255, 137, 18);
ellipse(150,285, 30, 30);
fill(0);
ellipse(150,285, 5, 5);
fill(255, 137, 18);
quad(180,347,240,410,300,420, 215, 340 );
endShape(CLOSE);
popMatrix();
}

void drive(){
xpos += -xspeed;
if(xpos<0-800){
xpos = -xpos + 800;

}
ypos --;
if (ypos<0-300){
  ypos = - ypos +300;
}

}
  
  
  
}
2 Likes

Hi @PabloTryAnimations

Scaling works fine for me :wink:
Try substituting your class by the following one

class Peixe {
  color c;
  float xpos;
  float ypos;
  float xspeed;
  float scaleV;
  Peixe( float tempXpos, float tempYpos, float tempXspeed) {

    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
    scaleV = random(0.1,1.5); //Declara a escala como random
  }
  void display() {
    pushMatrix();
    translate(xpos, ypos);
    strokeWeight(2);
    scale(scaleV); // Adiciona scale antes de fazer o shape
    beginShape(TRIANGLES);
    fill(14, 224, 155);
    vertex(100, 300);
    vertex(250, 200);
    vertex(250, 300);
    fill(161, 3, 170);
    vertex(250, 200);
    vertex(250, 300);
    vertex(500, 300);
    fill(14, 224, 155);
    vertex(500, 300);
    vertex(550, 250);
    vertex(550, 350);
    fill(14, 224, 155);
    vertex(100, 300);
    vertex(150, 350);
    vertex(500, 300);
    fill(161, 3, 170);
    vertex(150, 350);
    vertex(220, 420 );
    vertex(350, 321);
    fill(255, 137, 18);
    ellipse(150, 285, 30, 30);
    fill(0);
    ellipse(150, 285, 5, 5);
    fill(255, 137, 18);
    quad(180, 347, 240, 410, 300, 420, 215, 340 );
    endShape(CLOSE);
    popMatrix();
  }

  void drive() {
    xpos += -xspeed;
    if (xpos<0-800) {
      xpos = -xpos + 800;
    }
    ypos --;
    if (ypos<0-300) {
      ypos = - ypos +300;
    }
  }
}

Best regards

1 Like

Hello Miguel, How are you?
Yes, its works perfectly!
Thanks! Gracias
Pablo

Hi @PabloTryAnimations

Great!

Please mark it as a solution.