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

526<br />

Figure 11-18. Law of reflection<br />

There is a very h<strong>and</strong>y equation you can use to apply this principle: R = 2N(N • L) – L, where<br />

R is the reflection vector, N is the surface normal (also a vector quantity), <strong>and</strong> L is the incident<br />

vector. Both the normal vector <strong>and</strong> the incident vector need to be normalized as well.<br />

The expression (N • L) represents the dot product of the surface normal <strong>and</strong> the incident<br />

vector. Since N <strong>and</strong> L are both vector quantities, you can’t simply multiply them together<br />

the way you do two individual values (e.g., 5 ✕ 4). Instead, you have two options for multiplying<br />

vectors; one approach returns a single non-vector value (the dot product) <strong>and</strong> the<br />

other approach returns another vector (the cross-product). For this example, you only<br />

need to use the dot product calculation. When you get to Chapter 14, you’ll use the crossproduct<br />

along with the dot product.<br />

The beauty of using this equation is that you don’t have to worry about rotating coordinates.<br />

In the next example (shown in Figure 11-19), I created a non-orthogonal base plane<br />

for the ellipse to deflect off of. To h<strong>and</strong>le the actual collision detection, I treated the top<br />

of the base as an array of point coordinates <strong>and</strong> checked the distance between the ellipse<br />

<strong>and</strong> each of the individual points. Each time the ellipse hits the top of the display window,<br />

the base top is recalculated.<br />

/*********************************************<br />

* Non-orthogonal Reflection:<br />

* Based on the equation R = 2N(N•L)-L<br />

* R = reflection vector<br />

* N = normal<br />

* L = incidence vector

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

Saved successfully!

Ooh no, something went wrong!