3d - How do I rotate vectors using matrices in Processing? -


i trying rotate vectors using matrices, got confused.

i thought needed create rotation matrix , multiply vector rotated vector.

here can see simple test i've done using processing:

processing text

use a increment rotation, , x/y/z changes axis.

and here source:

pvector[] clone,face = {new pvector(50,0,50),new pvector(-50,0,50),new pvector(-50, 0, -50),new pvector(50, 0, -50)}; color facec = color(255,0,0),clonec = color(0,255,0); float angle = 90; pvector x = new pvector(1,0,0),y = new pvector(0,1,0),z = new pvector(0,0,1), axis = x; pfont ocr;  void setup(){   size(400,400,p3d);   strokeweight(1.1610855);smooth();   clone = rotateverts(face,angle,axis);   ocr = loadfont("ocr.vlw"); } void draw(){   background(255);   fill(0);textfont(ocr,10);text("a = increment rotation\nx/y/z = change axis\nrotation: " + angle + "\naxis: " + axis,10,10,200,200);fill(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 keypressed(){   if(key == 'a') angle += 15;   if(angle > 360) angle -= 360;   if(key == 'x') axis = x;   if(key == 'y') axis = y;   if(key == 'z') axis = z;   clone = rotateverts(face,angle,axis); } pvector[] rotateverts(pvector[] verts,float angle,pvector axis){   int vl = verts.length;   pvector[] clone = new pvector[vl];   for(int = 0; i<vl;i++) clone[i] = pvector.add(verts[i],new pvector());   //rotate using matrix   pmatrix3d rmat = new pmatrix3d();   rmat.rotate(radians(angle),axis.x,axis.y,axis.z);   for(int = 0; i<vl;i++) rmat.mult(clone[i],clone[i]);   return clone; } void drawquad(pvector[] verts,color c){   stroke(c);   beginshape(quads);     for(int = 0 ; < 4; i++) vertex(verts[i].x,verts[i].y,verts[i].z);   endshape(); } 

processing comes pvector , pmatrix3d i've used.

am not using pmatrix should used or bug ? hints ?

this culprit:

rmat.mult(clone[i],clone[i]); 

this not safe do, because unlike in c or java, source changing operation proceeds.

changing end of function to:

pvector[] dst = new pvector[vl]; for(int = 0; i<vl;i++) dst[i] = new pvector(); for(int = 0; i<vl;i++) rmat.mult(clone[i],dst[i]); return dst; 

will resolve issue.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -