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.

Asteroid shower in three stages<br />

Now we’ll apply these expressions to our non-orthogonal collision problem, for which<br />

we’ll slowly develop an asteroid shower animation. In my kinder, gentler version, asteroids<br />

pummeling Earth don’t cause cataclysmic destruction, but harmlessly bounce off the<br />

planet’s surface.<br />

In the first stage of the asteroid shower animation, I’ll create a single orb that bounces off<br />

a non-orthogonal surface.<br />

Stage 1: Single orb<br />

The sketch will be developed using six tabs, including the main sketch tab. Three of the<br />

tabs you’ll create will hold classes <strong>and</strong> the other two will hold functions. The first new tab<br />

I’ll create is for a Ground class. Create a new tab <strong>and</strong> name it Ground. Enter the following<br />

code into the tab:<br />

class Ground {<br />

float x1, y1, x2, y2;<br />

float x, y, len, rot;<br />

// default constructor<br />

Ground(){<br />

}<br />

// constructor<br />

Ground(float x1, float y1, float x2, float y2) {<br />

this.x1 = x1;<br />

this.y1 = y1;<br />

this.x2 = x2;<br />

this.y2 = y2;<br />

x = (x1+x2)/2;<br />

y = (y1+y2)/2;<br />

len = dist(x1, y1, x2, y2);<br />

rot = atan2((y2-y1), (x2-x1));<br />

}<br />

}<br />

This is a relatively simple class. Objects of the class are instantiated using four arguments,<br />

specifying the left <strong>and</strong> right coordinates of the top surface of the ground plane. In addition,<br />

within the constructor, a center point (x, y) of the surface is calculated, as is the<br />

ground surface length <strong>and</strong> the angle of rotation of the surface. Notice how I found the<br />

angle of rotation using the following expression:<br />

rot = atan2((y2-y1), (x2-x1));<br />

You looked at the atan2() function earlier in the chapter. Remember that you can use the<br />

st<strong>and</strong>ard trig functions sin(), cos(), <strong>and</strong> tan() to translate a rotation in the polar coordinate<br />

system to a specific coordinate value in the Cartesian system; <strong>and</strong> you can use the<br />

MOTION<br />

535<br />

11

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

Saved successfully!

Ooh no, something went wrong!