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.

This linear motion would plot as a straight line, as its rate of change is constant. If, however,<br />

I initially move a ball 30 pps, <strong>and</strong> then I keep doubling the rate at which I move the<br />

ball (1 second equals 30 pps, 2 seconds equals 30 pps, 3 seconds equals 120 pps, 4 seconds<br />

equals 240 pps, etc.) based on a geometric progression, I end up with acceleration. Here’s<br />

the same sketch converted from linear motion to accelerated motion. However, to keep<br />

the speed down, rather than continuously doubling the rate, I continuously increase it by<br />

10 percent—which still gets the rectangle cruising. Don’t worry if you can’t follow the<br />

code yet—I’ll be going over this again later in the book, in painstaking detail.<br />

//Example 1: accelerated motion using multiplication<br />

int w = 20;<br />

int h = 10;<br />

int x = 0;<br />

int y;<br />

float speed = 1;<br />

void setup(){<br />

size(400, 400);<br />

y = height/2;<br />

frameRate(30);<br />

}<br />

void draw(){<br />

background(255);<br />

speed*=1.1;<br />

rect(speed, y, w, h);<br />

}<br />

There is another simple way to code acceleration, which uses addition <strong>and</strong> double implementation.<br />

I’ve included it here for your viewing pleasure:<br />

// Example 2:<br />

// accelerated motion using addition<br />

int w = 10;<br />

int h = 10;<br />

int x = 0;<br />

int y;<br />

float speed;<br />

float acceleration = .1;<br />

void setup(){<br />

size(400, 400);<br />

y = height/2;<br />

}<br />

void draw(){<br />

background(255);<br />

speed += acceleration;<br />

rect(x+=speed, y, w, h);<br />

}<br />

COMPUTER GRAPHICS, THE FUN, EASY WAY<br />

129<br />

4

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

Saved successfully!

Ooh no, something went wrong!