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.

We’ll look at this process in steps. The first step is h<strong>and</strong>ling the rotation of the collision<br />

surface <strong>and</strong> velocity vector. Remember your trusted rotation expressions:<br />

x = cos(theta)*radius<br />

y = sin(theta)*radius<br />

These expressions hopefully look very familiar by now. They allow you to plot any point<br />

based on theta (the angle of rotation expressed in radians) <strong>and</strong> a radius. These equations<br />

are immensely h<strong>and</strong>y in code art. However, there is another somewhat more complicated<br />

form of the expressions that makes them even h<strong>and</strong>ier (<strong>and</strong> absolutely essential for calculating<br />

3D rotations). The two expressions just mentioned are actually derived from their<br />

more powerful trig brethren:<br />

new x = cos(theta)*x - sin(theta)*y<br />

new y = cos(theta)*y + sin(theta)*x<br />

Whoa! Now don’t freak out yet. The main difference between the two forms of the expressions<br />

is that the familiar shorter ones are used when the angle of rotation begins at 0. The<br />

longer forms of the expressions are used when the rotation represents a change of rotation<br />

(not necessarily beginning at 0 on the unit circle) <strong>and</strong> is also based on a specific point<br />

location (x <strong>and</strong> y). In the next example, I’ll use these expressions to rotate a simple line<br />

around the center point of the display window (press the mouse to make the line rotate<br />

from its current position):<br />

/* Trig Rotation Expressions<br />

-mouse press to rotate<br />

Special thanks to kirupa.com*/<br />

float[] x = new float[2];<br />

float[] y = new float[2];<br />

float[] newX = new float[2];<br />

float[] newY = new float[2];<br />

float theta;<br />

void setup(){<br />

size(400, 400);<br />

float t = r<strong>and</strong>om(TWO_PI);<br />

/* I used the simple trig rotation form<br />

to generate the original line coords */<br />

x[0] = cos(t)*-50;<br />

y[0] = sin(t)*-50;<br />

x[1] = cos(t)*50;<br />

y[1] = sin(t)*50;<br />

}<br />

void draw(){<br />

background(255);<br />

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

/* I used the longer trig rotation form to<br />

rotate based on the existing rotation */<br />

MOTION<br />

533<br />

11

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

Saved successfully!

Ooh no, something went wrong!