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.

placement of the vector within the larger coordinate system. This is actually a feature of<br />

vectors; it doesn’t matter where you put them—their direction <strong>and</strong> magnitude will remain<br />

constant.<br />

Once I got the vector’s components, I simply calculated the vector’s overall length <strong>and</strong><br />

then divided each component by this length, giving me the base direction ratio, which I<br />

used in the calculation to move the ellipse, independent of the ellipse’s speed.<br />

Applying vectors in collisions<br />

Let’s now apply some of these vector principles to collisions. You already know how to<br />

h<strong>and</strong>le collisions against orthogonal surfaces, such as the display window boundaries.<br />

However, the problem is quite a bit more complex when the collision surface is not orthogonal.<br />

The reason for this added complexity essentially boils down to a rotated coordinate<br />

system problem. For example, in Figure 11-17, the collision depicted on the left takes place<br />

against an orthogonal surface. The collision on the right, against a non-orthogonal surface,<br />

can also be thought of as an orthogonal collision in a rotated coordinate system. As you<br />

might guess, one solution to solving a non-orthogonal collision problem is to factor in this<br />

coordinate rotation. I’ll discuss this solution shortly. First, however, I want to look at<br />

another approach, borrowing a principle from physics: the law of reflection.<br />

Figure 11-17. Orthogonal vs. non-orthonal collisions<br />

The law of reflection<br />

Figure 11-18 illustrates the law of reflection, which simply states that when light strikes a<br />

surface, the angle of reflection is equal to the angle of incidence, relative to the surface<br />

normal. The angle of incidence is the angle of the incoming ray striking the surface. A normal<br />

line is any line perpendicular to a surface. (It actually doesn’t matter where on the surface<br />

this line is, since all perpendicular lines off a flat surface will be parallel.) Please also<br />

note that the terms surface normal <strong>and</strong> normalizing a vector are unrelated. Remember<br />

that normalizing involves dividing a vector’s components by the length of the vector. In<br />

fact, in the next example, I’ll actually be normalizing the surface normal. Although this law<br />

of reflection relates to how light reflects off of a surface, it works the same way for an<br />

object bouncing off a surface.<br />

MOTION<br />

525<br />

11

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

Saved successfully!

Ooh no, something went wrong!