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.

As complicated as this last sketch might have seemed, it only scratches the surface of<br />

what’s possible. For example, a more advanced version of the sketch could include physics<br />

calculations: gravity, collisions, angular or rotational velocities, <strong>and</strong> so on. Well, don’t<br />

worry, I won’t torture you further with this example. However, before moving on, there is<br />

one more fundamental concept used in the last example that I’d like to discuss: 3D<br />

rotations.<br />

3D rotations<br />

Thus far in the chapter, I’ve been happily using <strong>Processing</strong>’s 3D rotation functions:<br />

rotateX(), rotateY(), <strong>and</strong> rotateZ(). These are really easy to use, which is a good<br />

thing—but it’s also valuable to underst<strong>and</strong> how these functions operate beneath the hood.<br />

I actually already covered the basic math that controls 3D rotation when I discussed 2D<br />

rotation around the z-axis. In 2D, when you want to rotate around the origin, you use the<br />

following trig functions:<br />

y = sin(theta)*radius<br />

x = cos(theta)*radius<br />

Hopefully, these expressions seem like old friends by now (if so, you’re well on your way to<br />

geek certification). In 3D, the situation isn’t much more complicated than applying these<br />

same rotation expressions to the x- <strong>and</strong> y-axes. To perform rotations around each of the<br />

three individual axes, you’d use the following expressions:<br />

z-axis rotation<br />

x = cos(theta)*radius<br />

y = sin(theta)*radius<br />

x-axis rotation<br />

y = cos(theta)*radius<br />

z = sin(theta)*radius<br />

y-axis rotation<br />

z = cos(theta)*radius<br />

x = sin(theta)*radius<br />

These expressions work fine for single-axis rotations, but if you want to put a bunch of<br />

these together, or allow a user to r<strong>and</strong>omly rotate a shape around any axis in real time,<br />

you need slightly more complicated versions of these equations. Before looking at those,<br />

here’s a simple implementation that rotates three rectangles around the x-, y-, <strong>and</strong> z-axes<br />

(see Figure 13-10):<br />

// Single Axis 3D Rotation<br />

// rectangle vertices<br />

float[][]rectX = new float[3][4];<br />

float[][]rectY = new float[3][4];<br />

float[][]rectZ = new float[3][4];<br />

3D<br />

635<br />

13

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

Saved successfully!

Ooh no, something went wrong!