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

500<br />

Object interactions<br />

The last sketch can be thought of as a very (very) simplified particle system. Particle<br />

systems are used to render organic effects <strong>and</strong> phenomena usually without a discrete <strong>and</strong><br />

rigid form. For example, by generating a group of particles moving in certain wave patterns,<br />

we can simulate things like waving grass, a sheet blowing in the wind, or even liquid.<br />

Aside from the type of speed <strong>and</strong> acceleration variables we’ve been looking at, particle<br />

systems might also include more complex calculations for inter-particle attraction/<br />

repulsion, inertia, <strong>and</strong> so on.<br />

We’ll begin by considering the interaction between just two particles <strong>and</strong> build from there.<br />

One simple but interesting type of interaction is following behavior, as exhibited in a<br />

simple predator/prey dynamic. In the next two sketches, I’ll make the mouse the prey <strong>and</strong><br />

a ravenous ellipse its predator. The first example simply has the ellipse follow the mouse<br />

with a slight delay. Notice that I used <strong>Processing</strong>’s pmouseX <strong>and</strong> pmouseY variables, which<br />

hold the coordinate values of the mouse one frame in the past.<br />

Easing<br />

// Ravenous ellipse I<br />

float x, y;<br />

void setup(){<br />

size(400, 400);<br />

x = width/2;<br />

y = height/2;<br />

smooth();<br />

}<br />

void draw(){<br />

// repaint background<br />

fill(255, 40);<br />

rect(0, 0, width, height);<br />

/* find distance for x <strong>and</strong> y<br />

between prey <strong>and</strong> predator */<br />

float deltaX = (pmouseX-x);<br />

float deltaY = (pmouseY-y);<br />

x += deltaX;<br />

y += deltaY;<br />

ellipse(x, y, 15, 15);<br />

}<br />

The core principle in this last (pretty dull) sketch is that we are calculating deltaX <strong>and</strong><br />

deltaY each frame in the draw() loop, with the following lines:

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

Saved successfully!

Ooh no, something went wrong!