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

536<br />

inverse of these functions (referred to as the arc functions) to go in the other direction<br />

(find the angle of rotation beginning with some Cartesian coordinates).<br />

Next I’ll add an Orb class, which is very simple <strong>and</strong> self-explanatory. Add the following code<br />

to a new tab named Orb:<br />

class Orb{<br />

float x, y, r;<br />

// default constructor<br />

Orb() {<br />

}<br />

Orb(float x, float y, float r) {<br />

this.x = x;<br />

this.y = y;<br />

this.r = r;<br />

}<br />

}<br />

I created a really simple 2D vector class for the orb’s velocity. Create a tab named Vect2D<br />

<strong>and</strong> enter the following code:<br />

class Vect2D{<br />

float vx, vy;<br />

// default constructor<br />

Vect2D() {<br />

}<br />

Vect2D(float vx, float vy) {<br />

this.vx = vx;<br />

this.vy = vy;<br />

}<br />

}<br />

These are all the classes you’ll need. To h<strong>and</strong>le the collision detection between the orb <strong>and</strong><br />

the non-orthogonal surface, as well as the display window edges, you’ll use two functions.<br />

First you’ll tackle the easier, orthogonal (display window edge) collision. Create a new tab<br />

called checkWallCollision <strong>and</strong> enter the following code:<br />

void checkWallCollision(){<br />

if (orb.x > width-orb.r){<br />

orb.x = width-orb.r;<br />

velocity.vx *= -1;<br />

velocity.vx *= damping;<br />

}<br />

else if (orb.x < orb.r){<br />

orb.x = orb.r;<br />

velocity.vx *= -1;

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

Saved successfully!

Ooh no, something went wrong!