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

538<br />

I suspect this function needs some going over. The initial lines in the function<br />

// get difference between orb <strong>and</strong> ground<br />

float deltaX = orb.x - ground.x;<br />

float deltaY = orb.y - ground.y;<br />

calculate the difference between the orb <strong>and</strong> the center point of the line defining the<br />

ground plane. These values, assigned to deltaX <strong>and</strong> deltaY, will be fed into the long form<br />

of the trig expressions I discussed earlier. Remember, the long form trig expressions rotate<br />

existing coordinates (represented by deltaX <strong>and</strong> deltaY) by adding to their current rotation.<br />

The current rotation we’re interested in is calculated in the Ground class’s constructor,<br />

which defines the amount that the ground edge is rotated from being perfectly<br />

horizontal (orthogonal).<br />

Next in the function, the values for cosine <strong>and</strong> sine are calculated using the current rotation<br />

value (accessible through the property ground.rot):<br />

float cosine = cos(ground.rot);<br />

float sine = sin(ground.rot);<br />

These two lines may look odd <strong>and</strong> even unnecessary. In truth, they are unnecessary, as you<br />

could call the individual trig functions in all the rotation expressions following in the code.<br />

However, there is a processing cost every time a trig function is called; <strong>and</strong> more importantly,<br />

each frame, all the trig-based expressions (eight in total) will use the same trig<br />

values. (Remember, by default, <strong>Processing</strong> runs your sketches at 60 frames per second.) As<br />

such, precalculated trig values make the program more efficient.<br />

Now the fun begins. The following lines perform the rotation of the ground <strong>and</strong> the orb’s<br />

velocity:<br />

/* rotate ground <strong>and</strong> velocity to allow<br />

orthogonal collision calculations */<br />

float groundXTemp = cosine * deltaX + sine * deltaY;<br />

float groundYTemp = cosine * deltaY - sine * deltaX;<br />

float velocityXTemp = cosine * velocity.vx + sine * velocity.vy;<br />

float velocityYTemp = cosine * velocity.vy - sine * velocity.vx;<br />

These expressions hopefully look familiar. The ground <strong>and</strong> orb’s velocity are rotated the<br />

same amount, so that the ground surface is perfectly horizontal. This is only a temporary<br />

calculation, allowing you to perform a simpler reflection calculation (following shortly).<br />

The ground <strong>and</strong> velocity are not actually rotated. In fact, notice that the values returned<br />

from the four expressions are returned to four temporary variables: groundXTemp,<br />

groundYTemp, velocityXTemp, <strong>and</strong> velocityYTemp. Remember also that the signs in<br />

the four trig expressions relate to the direction of rotation. When I eventually rotate the<br />

ground <strong>and</strong> velocity back, I’ll use the same long form trig expressions with the signs<br />

reversed.<br />

Now that you have some values based on an orthogonal collision, you can h<strong>and</strong>le the collision<br />

nearly the same way you did for the walls:

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

Saved successfully!

Ooh no, something went wrong!