processing

Out of context: Reply #2

  • Started
  • Last post
  • 4 Responses
  • stewdio0

    You're going to love these three commands:

    pushMatrix()
    http://processing.org/reference/…

    popMatrix()
    http://processing.org/reference/…

    translate()
    http://processing.org/reference/…

    You'll love them because what you can do is design your guy at any position on the screen, and then simply move (or "translate") that position to elsewhere. For example, let's say you've drawn the head of this creature so that it's centered on point (0, 0) like so :

    ellipseMode( CENTER );
    ellipse( 0, 0, 15, 15 );

    You can easily "translate" that point of (0, 0) to something random like let's say... (23, 79) by sticking in one "translate" command :

    ellipseMode( CENTER );
    translate( 23, 79 );
    ellipse( 0, 0, 15, 15 );

    Anything that comes after that call to translate() will be moved over by (23, 79). PushMatrix and PopMatrix allow you to group translations together. (Think of this as like grouping a bunch of objects together in Illustrator and then moving them around as a group while other objects stay where they are.) Let's say you want one circle at (23, 79) and one that's actually at (0, 0). You could make that happen by doing this :

    ellipseMode( CENTER );
    pushMatrix();
    translate( 23, 79 );
    ellipse( 0, 0, 15, 15 );
    popMatrix();
    ellipse( 0, 0, 15, 15 );

    Notice how the first time we call the Ellipse function we're within a matrix that's been translated. But then we pop that matrix off the stack and so when we call Ellipse a second time we're back to our fresh matrix where (0, 0) really means (0, 0). You can push, pop, and translate to your heart's content within the draw loop so go wild. You can even nest push / pops.

    Also, just in case you're working from a book and not the Processing website, this page is extremely useful :
    http://processing.org/reference/…

View thread