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-25. 1D Collision Using Conservation of<br />

Momentum sketch<br />

Try running the sketch a few times <strong>and</strong> changing the radius of one of the balls. The greater<br />

the difference between the two radii, the more obvious the effect of the conservation of<br />

momentum will be.<br />

2D collisions<br />

Now that we got most of the easy stuff over with (I’m winking, don’t worry), we’ll tackle<br />

the holy grail of collisions: non-orthogonal 2D collisions. In actuality, you already know<br />

how to do this (well, sort of). I’ll be using the same coordinate rotation approach I used for<br />

the Asteroid Shower sketch. The main difference is that there are now two objects colliding<br />

(shown in Figure 11-26).<br />

The main sketch tab reuses much of the existing code from the previous example, with the<br />

changes shown in bold, as follows:<br />

// Main Sketch Tab<br />

// 2D Collision Using Conservation of Momentum<br />

Ball[] balls = {<br />

new Ball(100, 400, 10), new Ball(700, 400, 40) };<br />

Vect2D[] vels = {<br />

new Vect2D(4.3, -2.7), new Vect2D(-3.3, .95) };<br />

void setup(){<br />

size(400, 400);<br />

smooth();<br />

noStroke();<br />

}<br />

void draw(){<br />

background(255);<br />

fill(150);<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!