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.

vFinal[0].vx = ((b[0].m - b[1].m) * vTemp[0].vx + 2 * b[1].m *<br />

vTemp[1].vx) / (b[0].m + b[1].m);<br />

vFinal[0].vy = vTemp[0].vy;<br />

// final rotated velocity for b[0]<br />

vFinal[1].vx = ((b[1].m - b[0].m) * vTemp[1].vx + 2 * b[0].m *<br />

vTemp[0].vx) / (b[0].m + b[1].m);<br />

vFinal[1].vy = vTemp[1].vy;<br />

// hack to avoid clumping<br />

bTemp[0].x += vFinal[0].vx;<br />

bTemp[1].x += vFinal[1].vx;<br />

/* Rotate ball positions <strong>and</strong> velocities back<br />

Reverse signs in trig expressions to rotate<br />

in the opposite direction */<br />

// rotate balls<br />

Ball[] bFinal = { new Ball(), new Ball() };<br />

bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;<br />

bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;<br />

bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;<br />

bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;<br />

// update balls to screen position<br />

b[1].x = b[0].x + bFinal[1].x;<br />

b[1].y = b[0].y + bFinal[1].y;<br />

b[0].x = b[0].x + bFinal[0].x;<br />

b[0].y = b[0].y + bFinal[0].y;<br />

// update velocities<br />

v[0].vx = cosine * vFinal[0].vx - sine * vFinal[0].vy;<br />

v[0].vy = cosine * vFinal[0].vy + sine * vFinal[0].vx;<br />

v[1].vx = cosine * vFinal[1].vx - sine * vFinal[1].vy;<br />

v[1].vy = cosine * vFinal[1].vy + sine * vFinal[1].vx;<br />

}<br />

}<br />

Figure 11-26. 2D Collision Using Conservation of<br />

Momentum sketch<br />

MOTION<br />

559<br />

11

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

Saved successfully!

Ooh no, something went wrong!