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 />

638<br />

float rotX, rotY;<br />

void setup(){<br />

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

}<br />

void draw(){<br />

background(150);<br />

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

rotateX(rotX);<br />

rotateY(rotY);<br />

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

}<br />

void mouseMoved(){<br />

rotX = radians(mouseY);<br />

rotY = radians(mouseX);<br />

}<br />

So that was the easy way. Of course, you by now know that I can’t leave well enough alone.<br />

Next, I’ll show you how to re-create the functionality of <strong>Processing</strong>’s three rotation functions,<br />

which mostly just means altering the trig functions. Here they are, along with some<br />

declared variables to help illustrate how they work. (Please note that this code is not<br />

meant to be run.)<br />

float originalX, originalY, originalZ;<br />

float x2, y2, z2;<br />

float x3, y3, z3;<br />

float finalX, finalY, finalZ;<br />

// rotation around x-axix<br />

y2 = cos(angleX) * originalY - sin(angleX) * originalZ;<br />

z2 = sin(angleX) * originalY + cos(angleX) * originalZ;<br />

// rotation around y-axis<br />

z3 = cos(angleY) * z2 - sin(angleY) * x2;<br />

x3 = sin(angleY) * z2 + cos(angleY) * x2;<br />

// rotation around z-axis<br />

finalX = cos(angleZ) * x3 - sin(angleZ) * y3;<br />

finalY = sin(angleZ) * x3 + cos(angleZ) * y3;<br />

Although the expressions may at first seem a little confusing, if you look at them carefully,<br />

you’ll see that they’re pretty redundant. A key feature to using these expressions, which<br />

some books don’t seem to clarify properly, is that the expressions use existing x, y, <strong>and</strong> z<br />

values as inputs. So, for example, if you wanted to do rotations around all three axes,<br />

you’d need to keep feeding the new values calculated from the previous axis rotations into<br />

the next set of expressions, as I illustrated by using the different variables. I accomplish this<br />

in the next example by using three separate vertices arrays in addition to the original array<br />

of vertices; it’s mostly an organizational problem.

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

Saved successfully!

Ooh no, something went wrong!