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

560<br />

OK, hopefully we’re still on speaking terms. Part of the reason this function is so long, with<br />

a slew of temporary object arrays, is that I wanted to make all the processes as explicit as<br />

possible. There are many ways of optimizing this code <strong>and</strong> reducing the number of lines;<br />

however, that economy comes with a cost of potential confusion. (Again, Keith Peters’s<br />

book discusses a bunch of these optimizations, so I encourage you to take a look at it for<br />

a deeper discussion on this topic.)<br />

I added a lot of comments directly within the code to help you decipher it. In addition, I’ll<br />

provide a brief overview.<br />

The code in the function combines a number of concepts we’ve looked at throughout this<br />

chapter, including vectors, coordinate rotation, <strong>and</strong> of course the law of conservation of<br />

momentum. I began the function by assigning the vector described by the distance between<br />

the two balls to bVect. I used this vector to determine the angle of rotation between the<br />

balls, as well as to define the reference point for rotation. Vectors, you’ll remember, tell us<br />

about both direction <strong>and</strong> magnitude. I calculated the magnitude of bVect (assigned to<br />

bVectMag) <strong>and</strong> used it to determine when the balls were colliding, since the vector<br />

between the balls is also conveniently the distance between the balls.<br />

By plugging the components of bVect into the atan2() function, I was able to find the<br />

angle (in radians) of the vector’s rotation, which is precisely the value you need to rotate<br />

the ball <strong>and</strong> velocity coordinates, allowing you to treat the 2D collision between the balls<br />

as a 1D (orthogonal) collision.<br />

When rotating the ball <strong>and</strong> velocity coordinates, you don’t want to rotate in reference to<br />

the screen origin, but rather locally around the point of collision. I accomplished this by<br />

using bVect (the vector describing the distance between the balls) as the reference point<br />

of rotation. The two lines I’m referring to are the following (please note that I created a<br />

bunch of temporary Ball <strong>and</strong> Vect2D arrays (including bTemp) in the function to try to<br />

help clarify the different steps in the collision process):<br />

bTemp[1].x = cosine * bVect.vx + sine * bVect.vy;<br />

bTemp[1].y = cosine * bVect.vy - sine * bVect.vx;<br />

b[1] will rotate around b[0], setting the vector between them perfectly horizontal. Thus,<br />

b[0] will act, in a sense, as the origin for the rotation, so I don’t need to assign any value<br />

to bTemp[0].x <strong>and</strong> bTemp[0].y, which were both automatically assigned 0.0 when<br />

bTemp[0] was instantiated (since float values—which the x <strong>and</strong> y properties are—default<br />

to 0.0 when declared).<br />

Next, I rotated the velocities, which should be self-explanatory, using the good old long<br />

form trig expressions.<br />

The rotated velocities were assigned to the vTemp objects. Next, the ugly conservation of<br />

momentum expressions come into play. Notice that I only used them to find the final<br />

rotated velocities along the x-axis. The velocities along the y-axis were calculated previously,<br />

so I simply assigned vTemp.vy to vFinal.vy (for both objects).

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

Saved successfully!

Ooh no, something went wrong!