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.

Figure 11-3. Painting Stripes II sketch<br />

Simple collision detection<br />

In the last example, when the rectangles pass the edge of the display window, they don’t<br />

stop. To improve the sketch, I could add simple boundary collision detection <strong>and</strong> also a<br />

timeout function. Before I add collision detection for all the rectangles in the last example,<br />

though, let’s first look at collision detection with a single moving rectangle:<br />

// Single rect with boundary collision detection<br />

float speedX, speedY;<br />

float x, y, w, h;<br />

void setup(){<br />

size(400, 400);<br />

x=width/2;<br />

y=height/2;<br />

w=70;<br />

h=w;<br />

speedX = 2;<br />

speedY = 1;<br />

}<br />

void draw(){<br />

background(255);<br />

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

MOTION<br />

487<br />

11

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

Saved successfully!

Ooh no, something went wrong!