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.

Note that the if statement around the two incrementation lines just keeps the<br />

triangle from endlessly spinning when the mouse stops moving.<br />

The last step is damping the accelX <strong>and</strong> accelY values, which occurs in the final two lines:<br />

// slow down springing<br />

accelX *= damping;<br />

accelY *= damping;<br />

As always, I suggest playing with these values. Although it is relatively easy to implement<br />

the springing, it’s not easy to fully grasp how/why it’s working. Playing with the values will<br />

help you to better underst<strong>and</strong> the principle.<br />

There is so much more that can be done with both the following/easing <strong>and</strong> springing<br />

code. One really fun <strong>and</strong> interesting thing to try is to put a number of springy forms<br />

together in a series. In this next example (shown in Figure 11-12), I’ll create a worm composed<br />

of 60 overlapping springy ellipses that follow an invisible moving food source. The<br />

springing for the initial ellipse will be calculated based on the position of the food source;<br />

while the springing for each of the other 59 ellipses will be based on the position of the<br />

preceding ellipse—forming a reactive chain.<br />

/* Worm<br />

demonstrates springs in a series */<br />

// for worm<br />

int segments = 60;<br />

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

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

float[] accelX = new float[segments];<br />

float[] accelY = new float[segments];<br />

float[] springing = new float[segments];<br />

float[] damping = new float[segments];<br />

// for food<br />

float fx, fy;<br />

float fCntrX, fCntrY;<br />

float fAngle, fSpeedX = .25, fSpeedY = .5;<br />

void setup(){<br />

size(400, 400);<br />

smooth();<br />

// initialize array values<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!