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.

ground collision<br />

if(groundYTemp > -orb.r){<br />

// keep orb from going into ground<br />

groundYTemp = -orb.r;<br />

// bounce <strong>and</strong> slow down orb<br />

velocityYTemp *= -1.0;<br />

velocityYTemp *= damping;<br />

}<br />

Up until now, when h<strong>and</strong>ling orthogonal collisions, I’ve been checking if the object’s x or y<br />

property was past its respective x or y boundary. However, in the ground collision conditional<br />

block, I’m checking if the difference (along the y-axis) between the orb <strong>and</strong> the<br />

ground is greater than the negative of the orb’s radius; this reads like one of those annoying<br />

logic problems on a st<strong>and</strong>ardized test (sorry). You’ll remember that, a few paragraphs<br />

back, the variable deltaY was assigned the difference between the orb’s y position <strong>and</strong> the<br />

ground’s y position; that value was then rotated <strong>and</strong> assigned to the variable groundYTemp.<br />

As the orb nears the ground, the difference between their y values will decrease, until the<br />

orb passes through the ground, at which time this value will begin increasing. Before the<br />

orb actually hits the ground, though, as the difference between the orb <strong>and</strong> ground’s y<br />

value continues to decrease, there will be a point when the orb’s center point is the same<br />

distance from the ground as the orb’s radius—which would make the expression orb.<br />

y-ground.y equal to -orb.r. When the orb then moves one more pixel toward the<br />

ground, the difference between orb.y <strong>and</strong> ground.y, though still negative, will be greater<br />

than -orb.r, <strong>and</strong> the conditional will evaluate to true. You may have to read that a few<br />

times to get it; or just embrace the temporary confusion—I do it all the time. Once the<br />

conditional evaluates to true, the steps to reflect the orb should be old hat <strong>and</strong> selfexplanatory.<br />

Now that the ground <strong>and</strong> velocity have been rotated, allowing you to successfully detect<br />

<strong>and</strong> reflect the orb, all that’s left to do is reset the ground <strong>and</strong> velocity back to their initial<br />

non-orthogonal rotations <strong>and</strong> update the position of the orb:<br />

// reset ground, velocity <strong>and</strong> orb<br />

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

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

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

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

orb.x = ground.x + deltaX;<br />

orb.y = ground.y + deltaY;<br />

The first four lines of code use the same long form trig functions we looked at earlier, but<br />

the signs in the expressions are reversed, <strong>and</strong> the ground <strong>and</strong> velocity temp values are now<br />

used as the current coordinates. Remember, reversing the signs in the expression will have<br />

the effect of causing the rotation to go in the opposite direction—putting the ground <strong>and</strong><br />

velocity back in their original non-orthogonal positions. Of course, as I mentioned earlier,<br />

the ground <strong>and</strong> velocity were not actually affected, rather the four temp variables<br />

(groundXTemp, groundYTemp, velocityXTemp, <strong>and</strong> velocityYTemp) were assigned the<br />

rotated values. Now, however, the orb’s velocity is updated by the trig expressions, as is<br />

the orb’s position, based on the ground coordinates <strong>and</strong> the updated deltaX <strong>and</strong> deltaY<br />

variables.<br />

MOTION<br />

539<br />

11

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

Saved successfully!

Ooh no, something went wrong!