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.

momentum at impact has to go somewhere. In the real world, the force of this impact<br />

turns into heat, sound, cool explosions, <strong>and</strong> new motion. In a virtual world, we can control<br />

how the force is transferred. For example, when two objects collide, if we force all the<br />

momentum upon impact to go back into only new motion, then somehow the motion<br />

after the impact alone needs to be equal to the total momentum of the two objects at the<br />

point of impact. The easiest way to pull this off in code, as I did in the last example, is simply<br />

to have the two objects exchange their velocities upon impact.<br />

Less simple 1D collision<br />

This last example, although relatively simple, didn’t take into account the mass of the<br />

objects. I used the term “transfer of motion” to describe how the balls simply switched<br />

their velocities. But what if you want to account for balls of different mass? Introducing<br />

mass requires those scary equations I mentioned earlier. Again, here’s the first:<br />

obj 1 final velocity = (obj 1 mass – obj 2 mass) * obj 1 velocity + 2 * obj 2 mass * obj 2 velocity<br />

MOTION<br />

------------------------------------------------------------------------------------------------------------------------------------<br />

And here’s the second:<br />

obj 1 mass + obj 2 mass<br />

obj 2 final velocity = (obj 2 mass – obj 1 mass) * obj 2 velocity + 2 * obj 1 mass * obj 1 velocity<br />

------------------------------------------------------------------------------------------------------------------------------------<br />

obj 1 mass + obj 2 mass<br />

I know the equations are long, but converting them to code is not a big deal, as you’ll see<br />

shortly. The hard part of course would be attempting to derive the equations, which I<br />

won’t bother doing. Keith Peters’s book goes into some detail about where the equations<br />

come from. If you’re really interested in the math, here’s a link you might enjoy as well:<br />

www.euclideanspace.com/physics/dynamics/collision/oned/index.htm.<br />

The two equations are based on the law of conservation of momentum, which I’ve sort of<br />

already discussed. For our purposes, the law states that in a closed system (such as the<br />

type we’re coding), the total momentum of objects before a collision is equal to the total<br />

momentum after the collision. By using these equations, you’ll be able to now account for<br />

mass in your ball-ball collisions.<br />

The next example, shown in Figure 11-25, requires only some minor tweaks to the previous<br />

sketch. The sketch can be divided into five tabs like the last, with the main tab code,<br />

Vect2D class, <strong>and</strong> checkBoundaryCollision() function all remaining exactly as they were<br />

in the previous sketch, so I won’t bother repeating that code. All you need to do is update<br />

the Ball class <strong>and</strong> checkObjectCollision() function. Here’s the revised Ball class, with<br />

the new changes displayed in bold:<br />

555<br />

11

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

Saved successfully!

Ooh no, something went wrong!