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

532<br />

adding the z component into the mix. I’m not going to go into the theory behind the dot<br />

product here, but I will discuss it when I cover 3D in Chapter 14. If you just can’t wait,<br />

though, Wikipedia has some good stuff to say about it at http://en.wikipedia.org/<br />

wiki/Dot_product.<br />

Following the dot product calculation, I calculate the reflection vector, plugging in the values<br />

I’ve previously calculated. Finally, I assign the reflection vector to the original direction<br />

vector <strong>and</strong> display the normal at the point of collision. If the code is working, you should<br />

see that the angle formed between the base edge normal <strong>and</strong> the incoming incidence vector<br />

is equal to the angle between the outgoing reflection vector <strong>and</strong> the normal. The<br />

remainder of the code h<strong>and</strong>les display window boundary detection <strong>and</strong> also resets the<br />

base’s top edge whenever the ellipse makes contact with the top of the display window.<br />

A better way to h<strong>and</strong>le non-orthogonal collisions<br />

Although the last example successfully h<strong>and</strong>led angled collisions, there are some challenges<br />

in trying to extend this solution to h<strong>and</strong>le object-object collisions as well trying to<br />

add simple physics, like gravity <strong>and</strong> drag. There is another collision solution that<br />

ActionScript guru Keith Peters writes about in his excellent book Foundation ActionScript<br />

Animation: Making Things Move! Although Keith’s book deals with ActionScript, not<br />

<strong>Processing</strong>, I highly recommend it as a general graphics coding guide.<br />

The solution Keith writes about deals with considering angled collisions as a rotated coordinate<br />

system problem, instead of focusing on trying to calculate the actual reflection<br />

angle. It works like this: if you take any 2D collision, you can always rotate the local coordinates<br />

so that the collision happens orthogonally (shown in Figure 11-20). For example,<br />

suppose a ball strikes a surface at a 40-degree angle. Rather than trying to calculate the<br />

angle of reflection, taking into account both the ball’s incidence vector <strong>and</strong> the angle of<br />

the collision surface, you can rotate the surface <strong>and</strong> velocity vector (in this case –40<br />

degrees) so that the collision surface is orthogonal (perfectly horizontal or vertical). Once<br />

rotated, you can h<strong>and</strong>le the reflection the same way you do with collisions against the<br />

edges of the display window—simply reverse the sign of the appropriate velocity vector<br />

coordinate; finally, the last step is rotating everything back to where it was prior to the<br />

rotation.<br />

Figure 11-20. H<strong>and</strong>ling non-orthogonal collisions by rotating coordinates

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

Saved successfully!

Ooh no, something went wrong!