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.

In two dimensions, the surface normal of a vector is really just a line perpendicular to the<br />

vector. In three dimensions, the situation is a bit more complicated <strong>and</strong> requires the use of<br />

the cross-product calculation mentioned previously. We’ll look at the cross-product in the<br />

book’s final chapter. Calculating the perpendicular line to a vector is very simple. To get<br />

the x component of the normal, you simply reverse the sign of the vector’s y component,<br />

<strong>and</strong> to get the y component of the normal, you take the vector’s x component, as illustrated<br />

in the last two lines of the previous snippet of code. Be aware, though, that every<br />

vector actually has two perpendicular lines, facing opposite directions (once you know<br />

one, just reverse its sign to find the other).<br />

The block of code that h<strong>and</strong>les most of the real heavy lifting in the draw() function is the<br />

following:<br />

// normalized incidence vector<br />

float incidenceVectorX = -directionX;<br />

float incidenceVectorY = -directionY;<br />

// detect <strong>and</strong> h<strong>and</strong>le collision<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!