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.

the distance the rectangle moves are not perfectly synchronized; the rectangle may very<br />

well be past the boundary when the collision check occurs. By checking for collision past<br />

the boundary, we’re ensured of (eventually) catching it. However, this solution is not without<br />

some problems.<br />

Aesthetically, the rectangle may appear to go through the wall before bouncing off it. A<br />

worse problem is that if the rectangle goes too far past the edge of the window, the detection<br />

can actually catch it more than one time—causing it to get stuck (<strong>and</strong> sometimes<br />

appear to shake) on the boundary. The quick fix for these problems is to immediately<br />

place the rectangle against the display window wall after the detection, as I did in the<br />

example code. Technically, this fix is not precisely accurate, but generally good enough for<br />

“creative” coding purposes. For the more ambitious (or precise) reader, there is a more<br />

complex (<strong>and</strong> accurate) way of fixing this overlap problem, by analyzing an object’s path<br />

over time <strong>and</strong> finding precisely when it makes initial contact with the boundary.<br />

Implementing such an approach is beyond this book, but most (computer science–<br />

oriented) computer graphics texts will cover this approach.<br />

Finally, after the collision has been detected <strong>and</strong> the rectangle has been put neatly back<br />

against the display window edge, I simply reverse the sign of the speed variable by multiplying<br />

it by –1. If it’s been a while since math class, remember that a negative multiplied by<br />

another negative becomes a positive (<strong>and</strong> of course a negative times a positive equals a<br />

negative).<br />

Next, I’ll just add the collision detection <strong>and</strong> a timeout feature to the previous multirectangle<br />

example. I’ll also add a background(255) call so that we can see the discrete rectangles<br />

bouncing around (shown in Figure 11-4):<br />

// Collision Detection <strong>and</strong> Timeout<br />

int shapes = 200;<br />

float[]speedX = new float[shapes];<br />

float[]speedY = new float[shapes];<br />

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

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

float[]w = new float[shapes];<br />

float[]h = new float[shapes];<br />

color[]colors = new color[shapes];<br />

int timeLimit = 15;<br />

void setup(){<br />

size(400, 400);<br />

frameRate(30);<br />

noStroke();<br />

// fill arrays will r<strong>and</strong>om values<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!