"Mirroring" PShape objects (translation / rotation issue)


#1

Hi,

I’m trying to “mirror” a PShape object like in the picture below:

The tricky part is not to invert the shape but to rotate it (and maybe translating it ?) so as it sticks to the preceding shape. I’ve been trying to use angleBetween() or atan2() but without success.

So far here is what I’ve got.

int W = 20;
int H = 20;
int D = 20; 

PShape object;


void setup(){
    size(600, 600, P2D);
    smooth(8);
    
}
    
    
void draw(){
    background(255);
    
    pushMatrix();
    translate(width/2, height/1.3);
    
    int td = -1;
    for (int i = 0; i < 6; i++){
        translate(0, td*H*2);
        scale(-1, 1);
        rotate(PI);
        object();
        td *= -1;
    }
        

    popMatrix();
    
}
    
    
void object() {
    beginShape(QUADS);

    vertex(-20,  20);
    vertex(20,  0);
    vertex(20, -20);
    vertex(-20, -20);
    
    endShape();
}

Would appreciate any help.


#2

Hi,
maybe it would be easier to rotate when you start your shape at a corner (0,0) instead of in the middle. Then you have a nice reference for how you rotate?


#4

My last question about angles was also left unanswered. Please tell me if I’m not clear enough when formulating the issue or if the question is too complex for the forum.


#5

Trying again with pictures to be as clear as possible.

I would like to:

  • translate, invert and rotate a single quadrilateral (PShape object) multiple times
  • then change the height of one of its 2 top vertices

so as the whole thing act as an articulated arm that can be bent either to the right or the left.





I know that I can:

  • translate the quadrilateral using translate()
  • flip (invert) it with scale(1, -1)
  • rotate it using the atan2() function

PROBLEM

When combining the 3 together I end up with this:

The angle of rotation seems correct but obviously there’s something off with the translation (either on the X or Y axis) and I can’t figure out what exactly.

I would really appreciate if someone could help me understand what I’m doing wrong and how to fix this problem.

int W = 40;
int H = 40;
int Yoffset = 10; 
    
float[] p0 = {-W/2, -H/2};
float[] p1 = {W/2,  -H/2 - Yoffset};
float[] p2 = {W/2, H/2};
float[] p3 = {-W/2, H/2};
    
PShape object;
    
    
void setup(){
    size(600, 600, P2D);
    smooth(8);
    noFill();
}
    
        
void draw(){
    background(255);
        
    pushMatrix();
    translate(width>>1, height>>1);
        
    float angle = atan2(p1[1] - p0[1], p1[0] - p0[0]);
        
    for (int i = 0; i < 6; i++){
          
        int factor = (i % 2 == 0) ? 1 : -1;
            
        //Height translation
        translate(0, H*factor);
            
        //Flip all quads except 1st one
        if (i > 0){
          scale(1, -1);
        }
            
        //Rotate once every 2 quads
        if (i%2 == 1){
          rotate(-angle*2);
        }
            
        object();
      }
    
    popMatrix();
        
}
        
        
void object() {
    beginShape(QUADS);
    
    vertex(p0[0], p0[1]);
    vertex(p1[0], p1[1]);
    vertex(p2[0], p2[1]);
    vertex(p3[0], p3[1]);
        
    endShape();
}

#6

Could be the order of the transformations, I think you have to scale first, then translate and finally rotate


#7

Hi @bryan,

No, I’m afraid the problem here is not the order of the transformations.

I suspect a missing translation for the pivot but can’t say for sure. I’m really at loss.


#8

Finally found a workaround

Fixes :

  • the vertices order of the QUADS was incorrect
  • the computation of the pivot height was missing
  • the translation based on that height needs to be operated before and after the rotation (not sure to understand the reason)
  • the rotation angle has to be multiplied by -1 (negative angle) in order to change the bending side


Translating horizontally inverted quads