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

552<br />

Inter-object collision<br />

In the ActionScript book I mentioned earlier, Foundation ActionScript Animation: Making<br />

Things Move! Keith Peters goes into glorious detail on the joys of billiard ball physics, or<br />

what happens when objects collide, taking into account not only their velocity vectors but<br />

their respective masses. This gets into a discussion on Newtonian physics, which is really<br />

good stuff (stop making faces). However, as Keith has beat me to the punch (not to mention<br />

that his elucidation on the subject rocks), <strong>and</strong> of course since we share the same publisher,<br />

in this chapter I’ll include only some simple (<strong>and</strong> brief) examples dealing with the<br />

subject. However, I strongly recommend that you check out Keith’s book to learn more.<br />

Simple 1D collision<br />

The simplest way to begin to underst<strong>and</strong> these types of collisions is to move two balls<br />

along a single axis in opposite directions, not taking into account any rotation, mass, drag,<br />

wind, <strong>and</strong> so forth. In fact, we’ll begin with both balls moving at the same speed as well.<br />

I’ve divided the sketch (shown in Figure 11-24) into five tabs: the main tab, the Ball class,<br />

the Vect2D class, the checkBoundaryCollision() function, <strong>and</strong> the checkObjectCollision()<br />

function. Of course, you can just put all the classes <strong>and</strong> functions in the main tab, below<br />

the draw() function, instead. Here’s all the code, divided by tab:<br />

// main sketch tab:<br />

// 1D Collision with Swapping Velocities<br />

Ball[] balls = {<br />

new Ball(100, 200, 30), new Ball(300, 200, 30) };<br />

Vect2D[] vels = {<br />

new Vect2D(1.3, 0), new Vect2D(-1.3, 0) };<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!