public
anonymous / CD.java
Created

Collision detection

  • Download Gist
CD.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
public void checkCollisions(){
//p1-puck
if(puck.xPos < 450){
if(puck.yPos >= p1.yPos - 20 && puck.yPos <= p1.yPos + 60){
if(puck.xPos <= p1.xPos + 10 && !(puck.xPos <= p1.xPos)){
puck.xPos = p1.xPos + 11;
puck.xSpeed = -puck.xSpeed;
}else if(puck.xPos >= p1.xPos - 20 && !(puck.xPos + 20 >= p1.xPos + 10)){
puck.xPos = p1.xPos - 21;
puck.xSpeed = -puck.xSpeed;
}
}
//p2-puck
}else if(puck.yPos >= p2.yPos - 20 && puck.yPos <= p2.yPos + 60){
if(puck.xPos >= p2.xPos - 20 && !(puck.xPos + 20 >= p2.xPos + 10)){
puck.xPos = p2.xPos - 21;
puck.xSpeed = -puck.xSpeed;
}else if(puck.xPos <= p2.xPos + 10 && !(puck.xPos <= p2.xPos)){
puck.xPos = p2.xPos + 11;
puck.xSpeed = -puck.xSpeed;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.