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

530<br />

This is not a simple example, so I expect some of you may be scratching your heads a bit<br />

reviewing the code. Notice at the top of the sketch, where I declared global variables, that<br />

I created separate variables for directionX, directionY, <strong>and</strong> ellipseSpeed. Similarly, as in<br />

the last example, I conceptualized the ellipse’s movement as a vector quantity, keeping<br />

direction <strong>and</strong> speed as separate quantities. I also declared initial arbitrary values for<br />

directionX <strong>and</strong> directionY. These two variables represent the direction of the incident<br />

(incoming) vector. This vector must be normalized to be useful in the larger reflection calculation<br />

(R = 2N(N • L) – L). I normalized the vector at the bottom of the setup() function,<br />

with the following lines:<br />

// normalize direction vector<br />

float directionVectLength = sqrt(directionX*directionX + ➥<br />

directionY*directionY);<br />

directionX /= directionVectLength;<br />

directionY /= directionVectLength;<br />

You’ll remember that to normalize a vector, you need to divide each of the vector’s components<br />

by the overall length of the vector, which I did in the previous code snippet. Since<br />

the variables directionX <strong>and</strong> directionY are r<strong>and</strong>omly assigned values, I couldn’t assume<br />

that they formed a vector with a length of 1 (the length of a normalized vector).<br />

Within the draw() function, I used the following lines to calculate the coordinate values of<br />

the top of the base, which were used in the collision detection:<br />

// calculate length of base top<br />

baseLength = dist(baseX1, baseY1, baseX2, baseY2);<br />

xCoords = new float[ceil(baseLength)];<br />

yCoords = new float[ceil(baseLength)];<br />

// fill base top coordinate array<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!