09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

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.

118 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

Robot 6: Motion<br />

In this example, the techniques for random and circular motion<br />

are applied to the robot. The background() was removed to<br />

make it easier to see how the robot’s position and body change.<br />

At each frame, a random number between –4 and 4 is added to<br />

the x coordinate, and a random number between –1 and 1 is<br />

added to the y coordinate. This causes the robot to move more<br />

from left to right than top to bottom. Numbers calculated from<br />

the sin() function change the height of the neck so it oscillates<br />

between 50 and 110 pixels high:<br />

float x = 180;<br />

// x coordinate<br />

float y = 400;<br />

// y coordinate<br />

float bodyHeight = 153; // Body height<br />

float neckHeight = 56; // Neck height<br />

float radius = 45; // Head radius<br />

float angle = 0.0; // Angle for motion<br />

void setup() {<br />

size(360, 480);<br />

ellipseMode(RADIUS);<br />

background(0, 153, 204); // Blue background<br />

}

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

Saved successfully!

Ooh no, something went wrong!