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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

252<br />

Figure 7-10. Falling r<strong>and</strong>om bouncing str<strong>and</strong>s<br />

This example is similar to the last, but I achieved the curved motion using a slightly different<br />

algorithm. Instead of multiplying y by a ratio, I used a double arithmetic incrementation.<br />

The lines<br />

ySpeed[i] += accel[i];<br />

y[i] += ySpeed[i];<br />

create the acceleration. It may not be immediately apparent how this works. However, if<br />

you look at a few values during the loop iterations, it becomes clear what’s happening. To<br />

keep things simple, let’s assume the value assigned to accel, for one of the particles, is .1.<br />

Following are the ySpeed <strong>and</strong> y values after five iterations:<br />

iteration1: ySpeed= .1, y = .1<br />

iteration2: ySpeed = .2, y = .3 (y change: .2)<br />

iteration3: ySpeed = .3, y = .6 (y change: .3)<br />

iteration4: ySpeed = .4, y = 1.0 (y change: .4)<br />

iteration4: ySpeed = .5, y = 1.5 (y change: .5)<br />

Notice how the y value is changing by an increasing interval: .2, .3, .4, .5; this is what generates<br />

the acceleration.<br />

The display window collision detection allows the particles to remain active longer <strong>and</strong><br />

thus graphically add to the image. The detection is pretty straightforward: if a particle’s y<br />

position is greater than or equal to the height of the display window, then reverse the<br />

direction of the particle <strong>and</strong> also multiply ySpeed by the gravity variable. Here’s the code<br />

snippet:<br />

// check ground detection only<br />

if ( y[i]>=height){<br />

// reverse particle direction<br />

ySpeed[i]*=-1;

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

Saved successfully!

Ooh no, something went wrong!