-
Notifications
You must be signed in to change notification settings - Fork 0
/
collisionDetection.js
69 lines (58 loc) · 2.51 KB
/
collisionDetection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {ball,paddle,board} from './main.js'
class CollisionDetection{
withWalls(){
if(ball.getPositionY() <= board.getTopWall()){
ball.setTopCollide(true);
ball.setBottomCollide(false);
paddle.setSideCollide(false)
ball.setCollideWithEdge(false)
return true;
} else if(ball.getPositionX()+ball.getWidth() >= board.getRightWall()) {
ball.setRightCollide(true);
ball.setLeftCollide(false);
paddle.setSideCollide(false)
ball.setCollideWithEdge(false)
return true;
} else if(ball.getPositionX() <= board.getLeftWall()){
ball.setLeftCollide(true);
ball.setRightCollide(false);
paddle.setSideCollide(false)
ball.setCollideWithEdge(false)
return true;
}
}
withPaddle(){
if(ball.getPositionX()+ball.getWidth() >= paddle.getPositionX() && ball.getPositionX() <= paddle.getPositionX()+paddle.getWidth()
&& ball.getPositionY()+ball.getHeight() >= paddle.getPositionY() && ball.getPositionY()+ball.getHeight() <= paddle.getPositionY()+paddle.getHeight()){
// check if ball is collding with the side of paddles
if(ball.getPositionX()+ball.getWidth() <= paddle.getPositionX()+ball.getRadius() || ball.getPositionX() >= paddle.getPositionX()+paddle.getWidth()-ball.getRadius() ){
ball.setCollideWithEdge(true)
}else {
return true;
}
}
}
withBricks(brick){
if(ball.getPositionX()+ball.getWidth() >= brick.getPositionX() && ball.getPositionX() <= brick.getPositionX()+brick.getWidth()
&& ball.getPositionY()+ball.getHeight() >= brick.getPositionY() && ball.getPositionY() <= brick.getPositionY()+brick.getHeight()){
if(ball.getPositionY() >= brick.getPositionY()+brick.getHeight()/3){
ball.moveDown()
}else{
ball.moveUp();
}
if(ball.getPositionX()+ball.getWidth()/2 <= brick.getPositionX()){
ball.moveLeft();
}else if(ball.getPositionX()+ball.getWidth()/2 >= brick.getPositionX()+brick.getWidth()){
ball.moveRight();
}
return true;
}
}
withFloor(){
if(ball.getPositionY() >= board.getBottomWall()+board.getBorder()){
return true
}
}
}
let collision = new CollisionDetection();
export {collision}