Guest User

Untitled

a guest
Jan 22nd, 2016
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cfc = function(player, ball) {
  2.     //Rechtecke auf Überlappung überprüfen
  3.     if((ball.x+ball.size/2 < player.x || ball.x-ball.size/2 > player.x+player.size ||
  4.         ball.y-ball.speed/2 > player.y+player.speed || ball.y+ball.speed/2 < player.y)) {
  5.         return;
  6.     }
  7.  
  8.     //Kollisionsabstände berechnen um die Richtung zu bestimmen
  9.     var diff = [ball.x+ball.size/2 - player.x, player.x+player.size - ball.x,
  10.                 ball.y+ball.speed/2 - player.y, player.y+player.speed - ball.y];
  11.     var collision_dir = diff.indexOf(Math.min(diff[0], diff[1], diff[2], diff[3]));
  12.  
  13.     if(collision_dir === 0) { //Kollision linke Kante (Player)
  14.         ball.xVel = -Math.abs(ball.xVel);
  15.     }
  16.     else if(collision_dir === 1) { //Kollision rechte Kante (Player)
  17.         ball.xVel = Math.abs(ball.xVel);
  18.     }
  19.     else if(collision_dir === 2) { //Kollision obere Kante (Player)
  20.         ball.yVel = Math.abs(ball.yVel);  
  21.     }
  22.     else if(collision_dir === 3) { //Kollision untere Kante (Player)
  23.         ball.yVel = -Math.abs(ball.yVel);
  24.     }
  25. };
Add Comment
Please, Sign In to add comment