forked from whitehatjr/bounceOffAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
30 lines (26 loc) · 945 Bytes
/
sketch.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
var*fixedRect, movingRect;
function setup() {
createCanvas(1200,800);
fixedRect = createSprite(400, 100, 50, 80);
fixedRect.shapeColor = "green";
fixedRect.debug = true;
movingRect = createSprite(400, 800,80,30);
movingRect.shapeColor = "green";
movingRect.debug = true;
movingRect.velocityY = -5;
fixedRect.velocityY = +5;
}
function draw() {
background(0,0,0);
if (movingRect.x - fixedRect.x < fixedRect.width/2 + movingRect.width/2
&& fixedRect.x - movingRect.x < fixedRect.width/2 + movingRect.width/2) {
movingRect.velocityX = movingRect.velocityX * (-1);
fixedRect.velocityX = fixedRect.velocityX * (-1);
}
if (movingRect.y - fixedRect.y < fixedRect.height/2 + movingRect.height/2
&& fixedRect.y - movingRect.y < fixedRect.height/2 + movingRect.height/2){
movingRect.velocityY = movingRect.velocityY * (-1);
fixedRect.velocityY = fixedRect.velocityY * (-1);
}
drawSprites();
}