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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

540<br />

To run the completed sketch (shown in Figure 11-21), you’ll need the Orb, Ground, <strong>and</strong><br />

Vect2D classes, the checkWallCollision() <strong>and</strong> checkGround() functions, <strong>and</strong> the following<br />

code, which should be entered in the leftmost main tab:<br />

/****************************************<br />

Non-orthogonal Collision<br />

Based on Rotated Coordinates<br />

*****************************************<br />

* Based on Keith Peters's Solution in<br />

* Foundation ActionScript Animation:<br />

* Making Things Move!<br />

* http://www.friendsofed.com/book.html?➥<br />

* isbn=1590597915<br />

*****************************************/<br />

Orb orb;<br />

Vect2D velocity;<br />

float gravity = .05, damping = 0.6;<br />

Ground ground;<br />

void setup(){<br />

size(400, 400);<br />

smooth();<br />

orb = new Orb(50, 50, 3);<br />

velocity = new Vect2D(.5, 0);<br />

// r<strong>and</strong>om ground slope<br />

ground = new Ground(0, r<strong>and</strong>om(250, 390), ➥<br />

width, r<strong>and</strong>om(250, 390));<br />

}<br />

void draw(){<br />

// background<br />

noStroke();<br />

fill(0, 15);<br />

rect(0, 0, width, height);<br />

// move orb<br />

orb.x += velocity.vx;<br />

velocity.vy += gravity;<br />

orb.y += velocity.vy;<br />

// draw ground<br />

fill(127);<br />

beginShape();<br />

vertex(ground.x1, ground.y1);<br />

vertex(ground.x2, ground.y2);<br />

vertex(ground.x2, height);<br />

vertex(ground.x1, height);<br />

endShape(CLOSE);

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

Saved successfully!

Ooh no, something went wrong!