Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cfc = function(player, ball) {
- //Rechtecke auf Überlappung überprüfen
- if((ball.x+ball.size/2 < player.x || ball.x-ball.size/2 > player.x+player.size ||
- ball.y-ball.speed/2 > player.y+player.speed || ball.y+ball.speed/2 < player.y)) {
- return;
- }
- //Kollisionsabstände berechnen um die Richtung zu bestimmen
- var diff = [ball.x+ball.size/2 - player.x, player.x+player.size - ball.x,
- ball.y+ball.speed/2 - player.y, player.y+player.speed - ball.y];
- var collision_dir = diff.indexOf(Math.min(diff[0], diff[1], diff[2], diff[3]));
- if(collision_dir === 0) { //Kollision linke Kante (Player)
- ball.xVel = -Math.abs(ball.xVel);
- }
- else if(collision_dir === 1) { //Kollision rechte Kante (Player)
- ball.xVel = Math.abs(ball.xVel);
- }
- else if(collision_dir === 2) { //Kollision obere Kante (Player)
- ball.yVel = Math.abs(ball.yVel);
- }
- else if(collision_dir === 3) { //Kollision untere Kante (Player)
- ball.yVel = -Math.abs(ball.yVel);
- }
- };
Add Comment
Please, Sign In to add comment