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

550<br />

translate(width/2, height/2);<br />

beginShape();<br />

texture(PimageObjectNameGoesHere)<br />

vertex(-100, -100, 0, 0);<br />

vertex(100, -100, 200, 0);<br />

vertex(100, 100, 200, 200);<br />

vertex(-100, 100, 0, 200);<br />

endShape(CLOSE);<br />

To map the image larger on the same geometry, you’d decrease the uv coordinates values<br />

(only values greater than 0 will have any effect). Increasing uv values beyond the actual<br />

size of the image will have no effect.<br />

Returning to the Asteroid Shower sketch, it wasn’t easy to map the Earth image onto the<br />

irregular ground shape. I created the two variables, as follows:<br />

float imageShiftX = 20;<br />

float imageShiftY = -240;<br />

to help ensure that the image would map properly. I recommend messing with these values<br />

to see what happens.<br />

Next in the draw() function is the drawing <strong>and</strong> moving of the orbs, which is pretty<br />

straightforward. What might be less obvious, though, is how to h<strong>and</strong>le the collisions<br />

between the multiple ground segments <strong>and</strong> the multiple orbs. The crux of this problem is<br />

remembering to check the collision between each orb against every ground segment.<br />

When you come up against a programming problem with two sets of objects that need to<br />

interact like this, the solution is often a nested loop—which is precisely what I used to<br />

solve the collision problem. Thus, each orb gets born within the outer for loop <strong>and</strong> then<br />

gets passed to the checkGroundCollision() function, which is called within another<br />

nested for loop that executes the number of times there are ground segments. Each time<br />

the inner loop is called, the current orb, velocity, <strong>and</strong> damping values (controlled by<br />

the outer loop), <strong>and</strong> the ground segment (specified by the inner loop) are passed to the<br />

checkGroundCollision() function. Again, this inner loop will run the number of times<br />

there are segments <strong>and</strong> then control will return to the outer loop to begin the process<br />

over again.<br />

The last conditional in the draw() function<br />

if (emitter < orbCount-1){<br />

emitter += birthRate;<br />

}<br />

simply ensures that the emitter used to control the birthrate of orbs doesn’t exceed the<br />

total orb count specified earlier in the sketch. We’re almost done.<br />

To keep your workload down some, I edited the checkGroundCollision() <strong>and</strong><br />

checkWallCollision() functions in such a way that all you need to do is add some parameters<br />

to the two functions. Thus, between stage 2 <strong>and</strong> stage 3, you just need to replace<br />

void checkGroundCollision(Ground groundSegment) {

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

Saved successfully!

Ooh no, something went wrong!