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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

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

/* ground collision - check for surface<br />

collision <strong>and</strong> also that orb is within<br />

left/rights bounds of ground segment */<br />

if (groundYTemp > -orb.r &&<br />

orb.x > groundSegment.x1 &&<br />

orb.x < groundSegment.x2 ){<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 />

// 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 = groundSegment.x + deltaX;<br />

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

}<br />

The parameter groundSegment obviously needed to replace the variable ground in the<br />

original function. Other than that, the only additional change was in the conditional block<br />

that checks for the actual collision. In the original version of the function, I didn’t need to<br />

check if the orb was too far left or right, since there was only one collision surface spanning<br />

the entire display window. However, once I divided the ground into segments, I<br />

needed a way to limit the collision detection to the width of each respective segment. I<br />

accomplished this by simply adding a left <strong>and</strong> right boundary check in the conditional test<br />

(shown following):<br />

if (groundYTemp > -orb.r &&<br />

orb.x > groundSegment.x1 &&<br />

orb.x < groundSegment.x2 )<br />

...<br />

Make sure you try running this sketch <strong>and</strong> experimenting with (at least) the segments<br />

value <strong>and</strong> the peak heights r<strong>and</strong>om value range (displayed in bold in the following code).<br />

You can also of course mess with gravity, damping, orb, <strong>and</strong> velocity instantiation values.<br />

// calculate ground peak heights<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!