Do I need a PShape GROUP?

Sorry if this has been answered in another post, n00b city over here. I want an array of PShape objects all to follow mouseX and mouseY. However, when I try to use translate(mouseX,mouseY) within the for loop that displays the objects, instead of all drawing at the mouse position, they move with the mouse but are offset. I know this has to do with the for loop, but can't figure out why. Do I need to getVertex on each of these and move each vertex using add()? Do I need separate pShapes? Is it a problem with trying to use translate() with an array? Hopefully someone will point out the obvious :-D. P.S. I know the name of my class is Square and i'm drawing stars.

Square[] theSquares;
void setup(){
size(500,500,P2D);
theSquares = new Square[4];
for (int i=0;i<=theSquares.length -1; i++){
  theSquares[i] = new Square();
  }
}

void draw(){
background(255);

for (int s=0;s<=(theSquares.length-1);s++){
  theSquares[s].display();
  theSquares[s].moveSquare();
}
}

class Square {
  PShape coolShape;
  int x;
  int y;

 //constructor
 Square(){
  coolShape = createShape();
  coolShape.beginShape();
  coolShape.noStroke();
  coolShape.fill(0, 127);
  coolShape.vertex(0, -50);
  coolShape.vertex(14, -15);
  coolShape.vertex(47, -15);
  coolShape.vertex(23, 7);
  coolShape.vertex(29, 40);
  coolShape.vertex(0, 25);
  coolShape.vertex(-29, 40);
  coolShape.vertex(-23, 7);
  coolShape.vertex(-47, -15);
  coolShape.vertex(-14, -15);
  coolShape.endShape(CLOSE);
 }

 void moveSquare(){
   x=mouseX;
   y=mouseY;
   translate(x,y);
  }

 void display(){
   shape(coolShape);
 }
 }

Answers

  • Answer ✓

    translate is accumulative - call it twice and the second time the object will be twice as far away.

    see pushMatrix() and popMatrix() for the way around this.

  • Perfect, Thanks!

Sign In or Register to comment.