We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › apply transform with PMatrix
Page Index Toggle Pages: 1
apply transform with PMatrix (Read 506 times)
apply transform with PMatrix
Dec 10th, 2008, 9:27pm
 
I've been experimenting with the PMatrix classes and haven't been able to figure out how to apply transformations.

For example, I have a PMatrix3D called m:

m.translate(100,0,0);

Then I want to apply a rotation that is not affected by the translation.

So basically I want to maintain the position and orientation of the matrix while having the origin for any future transformations reset.
Re: apply transform with PMatrix
Reply #1 - Dec 12th, 2008, 10:06pm
 
That example probably isn't very good. The PMatrix classes seem to operate differently depending on how / where you are using them. I would also be interested in different matrix libraries if anyone can point them out.

Here is another example:
Quote:
PMatrix3D one, two;
Timer timer = null;
boolean go = false;

void setup() {
  size(400,400,P3D);
  rectMode(CENTER);
  one = new PMatrix3D();
  getMatrix(one);
  two = new PMatrix3D();
  getMatrix(two);
}
void draw() {
  background(196);    
  line(200,0,200,400);
  line(0,200,400,200);  
  translate(200,200,0);

  one = new PMatrix3D();
  pushMatrix();
  translate(5,5,0);
  getMatrix(one);
  popMatrix();
  
  if(frameCount == 1) {  
    two = new PMatrix3D();
  }
  
  if(go) {
    if(timer!=null && timer.time()) {
      two.rotateZ(HALF_PI/30f);
    } else {
      //<------------------------------------
      go = false;
      timer = null;
    }
  }
  
  one.preApply(two);
  pushMatrix();
  setMatrix(one);
  rect(0,0,10,10);
  popMatrix();
}

void mouseClicked() {
  if(timer == null) {
    timer = new Timer(30);
    go = true;
  } else {
    return;
  }
}

class Timer {
  int stime = 0;
  int time = 0;
  int frames;
  
  Timer(int f) {
    stime = frameCount;
    frames = f;
  }
  boolean time() {
    time = frameCount - stime;
    if(time == frames+1) {
      return false;
    }
    return true;
  }
}



Click and the rectangle rotates around its upper left corner. Its upper left corner then becomes its upper right in the transformed space so subsequent transformations basically rotate it around the center of the screen.

I'm looking for some way to reset the axes of the matrix called "one" (at the point indicated probably) so after the first click the other transformations will be exactly the same as the first. So the square rolls on the upper left corner each time.

There are probably other ways to do this. I just thought, naively, that it would be easier doing it with a matrix. I'm still wondering if its possible.
Re: apply transform with PMatrix
Reply #2 - Dec 12th, 2008, 11:14pm
 
Rick wrote on Dec 10th, 2008, 9:27pm:
...
So basically I want to maintain the position and orientation of the matrix while having the origin for any future transformations reset.


the classic way to rotate around a point (x, y) is to translate that point to the origin (translate(-x, -y)) then rotate through the desired angle, then translate back to x, y

(this is flash but is very clear)
http://www.joelconnett.com/flex-rotation-around-a-point-using-a-matrix.html
Re: apply transform with PMatrix
Reply #3 - Dec 12th, 2008, 11:23pm
 
(can't get your code to work, my version (135) doesn't know about getMatrix / setMatrix. documentation on website doesn't either...)
Re: apply transform with PMatrix
Reply #4 - Dec 13th, 2008, 12:29am
 
Yeah, I wrote that using 152. They changed how PMatrix works after 135. There are various matrix functions in the dev.processing.org documentation that aren't in the regular one. Thanks for the link and info.
Page Index Toggle Pages: 1