04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

620<br />

Figure 13-3. Order of Transformations example<br />

The problem becomes a little trickier when using multiple objects. For example, the next<br />

sketch tries to rotate four cubes as one larger block. Each cube will be 100 ✕ 100 ✕ 100,<br />

so I need to offset each block by 50. For example, I want the top-left block to be at –50 on<br />

the x-axis <strong>and</strong> –50 on the y-axis; the top-right block at 50 on the x-axis <strong>and</strong> –50 on the<br />

y-axis; the bottom-left block at –50, 50; <strong>and</strong> the bottom-right one at 50, 50. In addition, I<br />

want the entire group of four blocks to be centered in the display window. The intuitive<br />

thing to try is to initially translate everything to the center of the display window, as I did<br />

in the previous example, <strong>and</strong> then call a series of translations for each block, using the offset<br />

values I just mentioned. Unfortunately, this won’t work as expected. But don’t take my<br />

word for it; give it a try (see Figure 13-4):<br />

// Multiple Translations<br />

void setup(){<br />

size(400, 400, P3D);<br />

}<br />

void draw(){<br />

background(0);<br />

translate(width/2, height/2);<br />

rotateY(frameCount*PI/60);<br />

translate(-50, -50);<br />

fill(255, 0, 0);<br />

box(100, 100, 100);<br />

translate(50, -50);<br />

fill(255, 255, 0);<br />

box(100, 100, 100);<br />

translate(-50, 50);<br />

fill(0, 0, 255);<br />

box(100, 100, 100);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!