HTML and CSS Reference
In-Depth Information
function
function collideBalls ( ball1 , ball2 ) {
var
var dx = ball1 . nextx - ball2 . nextx ;
var
var dy = ball1 . nexty - ball2 . nexty ;
var
var collisionAngle = Math . atan2 ( dy , dx );
var
var speed1 = Math . sqrt ( ball1 . velocityx * ball1 . velocityx +
ball1 . velocityy * ball1 . velocityy );
var
var speed2 = Math . sqrt ( ball2 . velocityx * ball2 . velocityx +
ball2 . velocityy * ball2 . velocityy );
var
var direction1 = Math . atan2 ( ball1 . velocityy , ball1 . velocityx );
var
var direction2 = Math . atan2 ( ball2 . velocityy , ball2 . velocityx );
var
var velocityx_1 = speed1 * Math . cos ( direction1 - collisionAngle );
var
var velocityy_1 = speed1 * Math . sin ( direction1 - collisionAngle );
var
var velocityx_2 = speed2 * Math . cos ( direction2 - collisionAngle );
var
var velocityy_2 = speed2 * Math . sin ( direction2 - collisionAngle );
var
var final_velocityx_1 = (( ball1 . mass - ball2 . mass ) * velocityx_1 +
( ball2 . mass + ball2 . mass ) * velocityx_2 ) / ( ball1 . mass + ball2 . mass );
var
var final_velocityx_2 = (( ball1 . mass + ball1 . mass ) * velocityx_1 +
( ball2 . mass - ball1 . mass ) * velocityx_2 ) / ( ball1 . mass + ball2 . mass );
var
var final_velocityy_1 = velocityy_1 ;
var
var final_velocityy_2 = velocityy_2 ;
ball1 . velocityx = Math . cos ( collisionAngle ) * final_velocityx_1 +
Math . cos ( collisionAngle + Math . PI / 2 ) * final_velocityy_1 ;
ball1 . velocityy = Math . sin ( collisionAngle ) * final_velocityx_1 +
Math . sin ( collisionAngle + Math . PI / 2 ) * final_velocityy_1 ;
ball2 . velocityx = Math . cos ( collisionAngle ) * final_velocityx_2 +
Math . cos ( collisionAngle + Math . PI / 2 ) * final_velocityy_2 ;
ball2 . velocityy = Math . sin ( collisionAngle ) * final_velocityx_2 +
Math . sin ( collisionAngle + Math . PI / 2 ) * final_velocityy_2 ;
ball1 . nextx = ( ball1 . nextx += ball1 . velocityx );
ball1 . nexty = ( ball1 . nexty += ball1 . velocityy );
ball2 . nextx = ( ball2 . nextx += ball2 . velocityx );
ball2 . nexty = ( ball2 . nexty += ball2 . velocityy );
}
var
var numBalls = 50 ;
var
var maxSize = 12 ;
var
var minSize = 3 ;
var
var maxSpeed = maxSize + 5 ;
Search WWH ::

Custom Search