-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
68 lines (46 loc) · 1.47 KB
/
Copy pathsketch.js
File metadata and controls
68 lines (46 loc) · 1.47 KB
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
//name spacing
const Engine = Matter.Engine;
const World = Matter.World;
const Bodies = Matter.Bodies;
const Body = Matter.Body;
const Constraint = Matter.Constraint;
var roof;
var bob1, bob2, bob3, bob4, bob5;
var bobArray = [bob1, bob2, bob3, bob4, bob5];
var bobX = 500;
var ropeArray = [];
var offsetX = -80;
function setup() {
createCanvas(1500, 700);
engine = Engine.create();
world = engine.world;
roof = new Roof(580, 285, 200, 30);
for(var i = 0; i < bobArray.length; i++) {
bobArray[i] = new Bob(bobX, 500, 20);
bobX += 40;
}
for(i = 0; i < bobArray.length; i++) {
ropeArray[i] = new Rope(bobArray[i].body, roof.body, offsetX, 0);
offsetX += 40;
}
}
function draw() {
Engine.update(engine);//updates the engine continuously
rectMode(CENTER);//tells the computer to make the rectangle relative to its center
background(230);//clears the background
roof.display();
drawSprites();
for(i = 0; i < ropeArray.length; i++){
//Instead of writing rope.display() 5 times, I created an array and put it inside a loop
ropeArray[i].display();
}
for(i = 0; i < bobArray.length; i++){
//This is an alternative to writing bob.display() 5 times
bobArray[i].display();
}
if(keyWentDown(UP_ARROW)){
//Makes the pendulum move to the left direction when up_arrow in pressed
Body.applyForce(bobArray[0].body, bobArray[0].body.position, {x: -6, y: 0});
}
displayText("Press up_arrow to apply force on the first pendulum", "black", 400, 200);
}