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.

velocity.vx *= damping;<br />

}<br />

}<br />

There’s nothing new here. You’ve dealt with these types of orthogonal collisions a number<br />

of times before. The steps to h<strong>and</strong>le the wall collision, as a refresher, are as follows:<br />

1. Keep the orb from moving too far through the wall by assigning the precise value<br />

of the point of collision to the orb’s x property (orb.x = width-orb.r;).<br />

2. Reverse the orb’s velocity (along the x-axis).<br />

3. Optionally dampen the velocity along the x-axis upon collision.<br />

Finally, the ground collision function is where you’ll rotate the coordinate space to temporarily<br />

convert the challenging non-orthogonal collision problem to a much simpler<br />

orthogonal one. Create a tab named checkGroundCollision <strong>and</strong> enter the following code:<br />

void checkGroundCollision() {<br />

// get difference between orb <strong>and</strong> ground<br />

float deltaX = orb.x - ground.x;<br />

float deltaY = orb.y - ground.y;<br />

// precalculate trig values<br />

float cosine = cos(ground.rot);<br />

float sine = sin(ground.rot);<br />

/* rotate ground <strong>and</strong> velocity to allow<br />

orthogonal collision calculations */<br />

float groundXTemp = cosine * deltaX + sine * deltaY;<br />

float groundYTemp = cosine * deltaY - sine * deltaX;<br />

float velocityXTemp = cosine * velocity.vx + sine * velocity.vy;<br />

float velocityYTemp = cosine * velocity.vy - sine * velocity.vx;<br />

// ground collision<br />

if (groundYTemp > -orb.r){<br />

// keep orb from going into ground<br />

groundYTemp = -orb.r;<br />

// bounce <strong>and</strong> slow down orb<br />

velocityYTemp *= -1.0;<br />

velocityYTemp *= damping;<br />

}<br />

// reset ground, velocity <strong>and</strong> orb<br />

deltaX = cosine * groundXTemp - sine * groundYTemp;<br />

deltaY = cosine * groundYTemp + sine * groundXTemp;<br />

velocity.vx = cosine * velocityXTemp - sine * velocityYTemp;<br />

velocity.vy = cosine * velocityYTemp + sine * velocityXTemp;<br />

orb.x = ground.x + deltaX;<br />

orb.y = ground.y + deltaY;<br />

}<br />

MOTION<br />

537<br />

11

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

Saved successfully!

Ooh no, something went wrong!