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.

x+=xSpeed;<br />

println("x = " + x + " increase = " + (x-x2));<br />

x2 = x;<br />

}<br />

}<br />

The output shows the increase in y is changing while the increase in x remains constant at<br />

3, which causes the curved motion in the example.<br />

In the previous accelerated bouncing rectangle example, the rectangle collided with the<br />

ground <strong>and</strong> bounced, <strong>and</strong> then the process repeated itself while the sketch continued running.<br />

In the real world, if we dropped a bouncy box, when the box collided with the<br />

ground, there would be a transfer of some of its bouncing energy (into heat, sound, etc.),<br />

causing the box to eventually come to a rest on the ground. You may remember Newton’s<br />

law of conservation of energy, which states that energy in a closed system remains constant—it<br />

can be transferred but not created or destroyed. So, in technical terms, we could<br />

say that when the rectangle collides with the ground, there is a transfer of its kinetic<br />

energy into thermal (heat) <strong>and</strong> mechanical (sound) energy. Rather than deal with the real<br />

physics of this type of energy transfer, I’ll simply create a damping variable (a fractional<br />

value) <strong>and</strong> multiply ySpeed by it each time the rectangle hits the ground.<br />

The damping variable will eventually stop the rectangle’s motion along the y-axis, but I also<br />

need to stop the rectangle’s movement along the x-axis—if I don’t, the rectangle will continue<br />

to slide back <strong>and</strong> forth along the bottom of the display window. To fix this, I’ll create<br />

a friction variable, which will gradually slow the rectangle’s sideways motion using the<br />

same approach as with the damping variable. Each time the rectangle collides with the bottom<br />

of the display window, xSpeed is multiplied by the friction variable (also a fractional<br />

value). In the real world, friction would also slow the rectangle as it moved through the air<br />

<strong>and</strong> made contact with the side walls—but we’ll ignore this for now (although you could<br />

easily add in these calculations as well).<br />

// Simple Motion Physics I<br />

float speedX, speedY;<br />

float x, y, w, h;<br />

// acceleration force<br />

float gravity;<br />

Figure 11-7.<br />

Output of Acceleration Calculations sketch<br />

MOTION<br />

495<br />

11

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

Saved successfully!

Ooh no, something went wrong!