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.

Springy Dude is not an accurate physics simulation. Instead, I used some r<strong>and</strong>om values as<br />

an approximation for the spring <strong>and</strong> deformation. When the mouse moves, the entire<br />

polygon springs, based on the original springing code we looked at earlier in the chapter.<br />

The block of code controlling this overall movement is the following:<br />

// move center point<br />

float deltaX = mouseX-centerX;<br />

float deltaY = mouseY-centerY;<br />

// create springing effect<br />

deltaX *= springing;<br />

deltaY *= springing;<br />

accelX += deltaX;<br />

accelY += deltaY;<br />

// move polygon's center<br />

centerX += accelX;<br />

centerY += accelY;<br />

// slow down springing<br />

accelX *= damping;<br />

accelY *= damping;<br />

I utilized a parenting approach with regard to the relationship between the polygon’s five<br />

perimeter points (which aren’t rendered) <strong>and</strong> the shape’s central point. If you haven’t<br />

done much computer animation before, this may not initially mean much to you. The central<br />

problem in a sketch like Hybrid Springy Dude is how to maintain the shape, while<br />

allowing both the form to move as a whole <strong>and</strong> its individual parts to move independently.<br />

In a program like Flash, there are MovieClip data structures that can explicitly have parent<br />

<strong>and</strong> nested child clips. Most 3D animation applications employ similar constructs. In<br />

<strong>Processing</strong>, we don’t have this built-in structure (though of course we could build one).<br />

Instead, in the example I used the variables centerX <strong>and</strong> centerY to represent the overall<br />

coordinating point of the entire shape, which in a sense becomes the parent (or control<br />

node).<br />

I used trig functions both to calculate the nodes’ starting positions (relative to the shape’s<br />

center point) <strong>and</strong> to create the spring effect on the shape’s vertices. To enable the shape<br />

to return to its original polygonal structure after the springing, I needed to capture the<br />

original point locations of the shape (relative to the control node). Here’s the block of<br />

code that does that:<br />

// calculate node starting locations<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!