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.

Figure 13-10. Single Axis 3D Rotation sketch<br />

I coded this procedurally, but of course I could have created a custom rectangle class to<br />

do the same thing. The rectangles are plotted using the two trig equations by incrementing<br />

the angle[] variable in the trig functions by 90 degrees. To make the rectangles spin, I<br />

just incremented angle[] by the rot[] variable. Again, the order of translations <strong>and</strong><br />

placement of the pushMatrix() <strong>and</strong> popMatrix() functions are significant; try commenting<br />

out the functions or moving them to see what effect this has on the output. You’ll<br />

notice I also added an extra offset statement (rectX[i][j] = 10; <strong>and</strong> rectY[i][j] = 10;)<br />

for the x-axis <strong>and</strong> y-axis rotations, respectively. I only did this so that you could better see<br />

the rotations. If you comment these lines out, two of the rotations will be perpendicular to<br />

the screen <strong>and</strong> thus look like two shifting lines instead of planes.<br />

The problem with the last implementation is that you can’t do much with it, unless of<br />

course you’re really into single-axis rotations. It would be better to be able to rotate<br />

around all three axes. To do this, you need to, in a sense, keep track of where the vertices<br />

are as they are rotating so that you can smoothly transition them to a new rotation.<br />

Internally, <strong>Processing</strong> does this with its own 3D rotation functions, allowing you to call<br />

each of the functions in succession, without the last one overriding the previous ones. The<br />

three x, y, <strong>and</strong> z rotation calls blend into a seamless <strong>and</strong> interesting rotation. I’ve discussed<br />

this previously—but here’s another really simple example, using <strong>Processing</strong>’s box() function<br />

<strong>and</strong> three built-in rotation functions:<br />

void setup(){<br />

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

}<br />

void draw(){<br />

background(150);<br />

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

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

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

rotateZ(PI*frameCount/100);<br />

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

}<br />

And here’s an interactive version, allowing the user to perform axis rotations by moving<br />

the mouse. (Since the mouse normally only moves along the x- <strong>and</strong> y-axes, I only included<br />

rotations around these axes.)<br />

3D<br />

637<br />

13

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

Saved successfully!

Ooh no, something went wrong!