Loading...
Logo
Processing Forum
I'm trying to rotate some vertices/PVectors using matrices.
As far as I understand, all I need to do is create a matrix, store the rotation, then multiply it by the PVectors.

If I create a PMatrix3D and use rotateX(HALF_PI), then multiply a PVector, I would expect to get the PVector rotated by 90 degrees. I did a simple test drawing a quad and a clone of that quad rotated using a PMatrix3D. The clone quad gets distorted and I don't understand why. Also, I've tried using ArbitraryAxisRotation matrices, one with rotations applied in x,y,z order and one with rotations applied in z,y,x order.
I've placed the 3 matrix rotation variants within //* //*/ blocks to make it easier to comment/uncomment a block by removing/adding a /.

My question is: how do I rotate a vector using a matrix in Processing ?

In my sketch, the red quad is the original quad and the green one is the one that should be rotated by 90 degrees. Here's my code so far:
Copy code
  1. PVector[] clone,face = {new PVector(50.0, 0.0, 49.999994039535522),new PVector(-49.999982118606567, 0.0, 50.000017881393433),new PVector(-50.000011920928955, -7.4505805969238281e-07, -49.999988079071045),new PVector(50.0, -7.4505805969238281e-07, -49.999994039535522)};
    color faceC = color(255,0,0),cloneC = color(0,255,0);

    void setup(){
      size(400,400,P3D);
      smooth();strokeWeight(1.1610855);noFill();
      clone = rotateVerts(face,new PVector(90,0,0));
    }
    void draw(){
      background(255);
      translate(width*.5,height*.5);
      rotateX(map(mouseY,height*.5,-height*.5,0,TWO_PI));
      rotateY(map(mouseX,0,width,0,TWO_PI));
        drawQuad(face,faceC);
        drawQuad(clone,cloneC);
      stroke(128,0,0);line(0,0,0,100,0,0);stroke(0,128,0);line(0,0,0,0,-100,0);stroke(0,0,128);line(0,0,0,0,0,100);
    }
    void drawQuad(PVector[] verts,color c){
      stroke(c);
      beginShape(QUADS);
        for(int i = 0 ; i < 4; i++) vertex(verts[i].x,verts[i].y,verts[i].z);
      endShape();
    }
    PVector[] rotateVerts(PVector[] verts,PVector rot){
      int vl = verts.length;
      PVector[] clone = cloneVerts(verts);
      PMatrix3D rMat = getRotationMatrix(rot);
      for(int i = 0; i<vl;i++) rMat.mult(clone[i],clone[i]);
      return clone;
    }
    PVector[] cloneVerts(PVector[] verts){
      int vl = verts.length;
      PVector[] clone = new PVector[vl];
      for(int i = 0; i<vl;i++) clone[i] = PVector.add(verts[i],new PVector());
      return clone;
    }
    PMatrix3D getRotationMatrix(PVector rot){
      PVector r = new PVector(radians(rot.x),radians(rot.y),radians(rot.z));
      //*
        PMatrix3D result = new PMatrix3D();
        result.rotateX(r.x);result.rotateY(r.y);result.rotateZ(r.z);
      //*/
      //using http://inside.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html
      float a = r.x, b = r.y, c = r.z;
      //x,then y, then z
      /*
        PMatrix3D result = new PMatrix3D(cos(b)*cos(c),cos(c)*sin(a)*sin(b)-cos(a)*sin(c),cos(a)*cos(c)*sin(b)+sin(a)*sin(c),0,
                                         cos(b)*sin(c),cos(a)*cos(c)+sin(a)*sin(b)*sin(c),-cos(c)*sin(a)+cos(a)*sin(b)*sin(c),0,
                                         -sin(b)      ,              cos(b)*sin(a)       ,               cos(a)*cos(b)       ,0,
                                              0       ,                    0             ,                    0              ,1);
      //*/
      /*
        PMatrix3D result = new PMatrix3D(cos(a)*cos(b),-cos(c)*sin(a)+cos(a)*sin(b)*sin(c),cos(a)*cos(c)*sin(b)+sin(a)*sin(c),0,
                                         cos(b)*sin(a),cos(a)*cos(c)+sin(a)*sin(b)*sin(c),cos(c)*sin(a)*sin(b)-cos(a)*sin(c),0,
                                            -sin(b)   ,              cos(b)*sin(c)       ,           cos(b)*cos(c)          ,0,
                                               0      ,                    0             ,                 0                ,1);
      //*/
      return result;
    }

Replies(2)

I tried to use the PMatrix3D's rotate() method, but it seems to deform/skew a quad instead of rotating it.
You can see demo here.

Am I doing it wrong ? How do you rotate vectors using matrices in Processing ?
Solution:
"the source is changing as the operation proceeds."
Copy code
  1. PVector[] dst = new PVector[vl];
    for(int i = 0; i<vl;i++) dst[i] = new PVector();
    for(int i = 0; i<vl;i++) rMat.mult(clone[i],dst[i]);
    return dst;