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

554<br />

Vect2D vTemp = new Vect2D();<br />

vTemp.vx = v[0].vx;<br />

v[0].vx = v[1].vx;<br />

v[1].vx = vTemp.vx;<br />

}<br />

}<br />

Figure 11-24. 1D Collision with Swapping Velocities sketch<br />

Nearly all of this is straightforward. In fact, the only lines that may need some clarification<br />

are in the checkObjectcollision() function—specifically the following lines:<br />

Vect2D vTemp = new Vect2D();<br />

vTemp.vx = v[0].vx;<br />

v[0].vx = v[1].vx;<br />

v[1].vx = vTemp.vx;<br />

I needed to create the Vect3D object (vTemp) to help me switch the velocities. Looking at<br />

the assignment operations, it should be apparent how the velocity switcheroo works. Try<br />

running the previous sketch as it’s written, <strong>and</strong> then change the line<br />

to<br />

Vect2D[] vels = {<br />

new Vect2D(1.3, 0), new Vect2D(-1.3, 0) };<br />

Vect2D[] vels = {<br />

new Vect2D(6.3, 0), new Vect2D(-1.3, 0) };<br />

Try running the sketch again. Starting with different initial velocities makes it easier to see<br />

the transfer of motion.<br />

In a moment, I’m going to present you with some pretty scary-looking equations (insert<br />

evil laugh here) to allow you to more accurately calculate the ball-ball collision. The basic<br />

principle I’ll be using is as follows: a moving object carries a certain amount of momentum<br />

related to both its velocity <strong>and</strong> its mass. When two objects collide, all that combined

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

Saved successfully!

Ooh no, something went wrong!