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

488<br />

x+=speedX;<br />

y+=speedY;<br />

// Check display window edge collisions<br />

if (x > width-w){<br />

x = width-w;<br />

speedX*=-1;<br />

}<br />

else if (x < 0){<br />

x = 0;<br />

speedX*=-1;<br />

}<br />

else if (y > height-h){<br />

y = height-h;<br />

speedY*=-1;<br />

}<br />

else if (y < 0){<br />

y = 0;<br />

speedY*=-1;<br />

}<br />

}<br />

If you run the sketch, you should see a single rectangle bouncing off the four boundaries<br />

of the display window. If it isn’t obvious, the key block of code that makes the rectangle<br />

bounce off the walls is the following:<br />

// Check display window edge collisions<br />

if (x > width-w){<br />

x = width-w;<br />

speedX*=-1;<br />

}<br />

else if (x < 0){<br />

x = 0;<br />

speedX*=-1;<br />

}<br />

else if (y > height-h){<br />

y = height-h;<br />

speedY*=-1;<br />

}<br />

else if (y < 0){<br />

y = 0;<br />

speedY*=-1;<br />

}<br />

Each of the four if statements controls detection on one of the display window boundaries<br />

(right, left, bottom, <strong>and</strong> top). Notice that the conditionals check if the rectangle is<br />

either greater than (>) or less than (

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

Saved successfully!

Ooh no, something went wrong!