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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

634<br />

Figure 13-9. Cubes Contained Within a Cube sketch<br />

This example is long—but again, it’s stuff you’ve looked at before. The most complex part<br />

of the sketch is the following code snippet, within the for loop of the draw() function:<br />

pushMatrix();<br />

translate(x[i], y[i], z[i]);<br />

rotateX(frameCount*PI/xRot[i]);<br />

rotateY(frameCount*PI/yRot[i]);<br />

rotateX(frameCount*PI/zRot[i]);<br />

noStroke();<br />

c[i].create(quadBG[i]);<br />

x[i]+=xSpeed[i];<br />

y[i]+=ySpeed[i];<br />

z[i]+=zSpeed[i];<br />

popMatrix();<br />

It is sometimes confusing, when using the pushMatrix() <strong>and</strong> popMatrix() functions, to<br />

determine what comm<strong>and</strong>s need to be called between them. Since I’m using a loop to<br />

process an array of items, I need to make sure that each transformation only affects its<br />

respective item; otherwise, the transformations will become cumulative, so objects stored<br />

later in the array will be affected much more than the earlier ones. It is helpful to break<br />

down the last example by commenting out the pushMatrix() <strong>and</strong> popMatrix() calls <strong>and</strong><br />

then rerunning the sketch to see the effect. In addition, I recommend playing with some<br />

of the values in the sketch, especially the variables in the following code:<br />

int cubies = 50;<br />

float bounds = 300;<br />

float cubieSize = r<strong>and</strong>om(5, 15);<br />

xSpeed[i] = r<strong>and</strong>om(-2, 2);<br />

ySpeed[i] = r<strong>and</strong>om(-2, 2);<br />

zSpeed[i] = r<strong>and</strong>om(-2, 2);

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

Saved successfully!

Ooh no, something went wrong!