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.

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

Since the ellipse <strong>and</strong> mouse are moving closer (until deltaX <strong>and</strong> deltaY eventually reach<br />

0), we can be sure that the predator will never overshoot the prey. We can improve this<br />

sketch by varying the speed in which the predator chases the prey with an effect called<br />

easing. Easing simply adds some deceleration or acceleration to an object’s motion.<br />

In the next example, I’ll have the predator decelerate as it nears the prey; this is also<br />

referred to as easing out. Easing in would be the opposite—the predator would accelerate<br />

toward the prey. To accomplish the easing out, I’ll add an easing variable that will work<br />

very similarly to the damping variable used a few sketches back (shown in Figure 11-10).<br />

// Ravenous Ellipse II<br />

float x, y;<br />

float easing = .05;<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 />

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

// cause the predator to decelerate<br />

deltaX *= easing;<br />

deltaY *= easing;<br />

x += deltaX;<br />

y += deltaY;<br />

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

MOTION<br />

501<br />

11

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

Saved successfully!

Ooh no, something went wrong!