HTML and CSS Reference
In-Depth Information
else
else iif ( ball . nextx - ball . radius < 0 ) {
ball . velocityx = ball . velocityx * 1 ;
ball . nextx = ball . radius ;
} else
else iif ( ball . nexty + ball . radius > theCanvas . height ) {
ball . velocityy = ball . velocityy * 1 ;
ball . nexty = theCanvas . height - ball . radius ;
} else
else iif ( ball . nexty - ball . radius < 0 ) {
ball . velocityy = ball . velocityy * 1 ;
ball . nexty = ball . radius ;
}
}
}
Collisions with balls
The collide() functionteststoseewhetheranyballshavehitanother.Thisfunctionusestwo
nested loops, both iterating through the balls array to ensure that we test each ball against
every other ball. We take the ball from the first loop of the balls array and place it into the
ball variable.Thenweloopthrough balls again,placingeachballinthe testBall variable,
one at a time. When we have both ball and testBall , we make sure that they are not equal
to one another. We do this because a ball will always have a false positive collision if we test
it against itself. When we are sure that they are not the same ball, we call hitTestCircle()
totestforacollision.Ifwefindone,wecall collideBalls() ,andthenallheckbreaksloose.
(OK, not really, but the balls do collide, and some really interesting code gets executed.) See
that code here:
function
function collide () {
var
var ball ;
var
var testBall ;
for
for ( var
var i = 0 ; i < balls . length ; i ++ ) {
ball = balls [ i ];
for
for ( var
var j = i + 1 ; j < balls . length ; j ++ ) {
testBall = balls [ j ];
iif ( hitTestCircle ( ball , testBall )) {
collideBalls ( ball , testBall );
}
}
}
}
Search WWH ::




Custom Search