-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
119 lines (85 loc) · 2.28 KB
/
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var Engine = Matter.Engine,
// Render = Matter.Render,
World = Matter.World,
Events = Matter.Events,
Constraint = Matter.Constraint,
Bodies = Matter.Bodies;
var engine;
var world;
var box1;
var boxes = [];
var particles = [];
var boundaries = [];
var constraints = [];
function preload() {
// ding = loadSound("beep.mp3");
}
function mousePressed(){
var tempParticle = new Particle(mouseX, mouseY, random(5, 30));
particles.push(tempParticle);
}
var setup = function(){
createCanvas(800, 600)
background(360,100,100)
colorMode(HSB);
// create matter engine
engine = Engine.create();
world = engine.world;
tempBoundary = new Boundary(width / 2 , height -20 , width, 5, Math.PI );
boundaries.push(tempBoundary);
leftBoundary = new Boundary(0,height / 2,10,height, Math.PI);
boundaries.push(leftBoundary);
rightBoundary = new Boundary(width - 20,height / 2, 10, height, Math.PI);
boundaries.push(rightBoundary);
// create a few particles
prevParticle = null;
for (x = 20; x < 400; x += 30){
p = new Particle(x, 30, 10);
particles.push(p);
// insert a constraint to the previous particle, if it exists
if(prevParticle){
console.log(prevParticle)
var options = {
bodyA: prevParticle.body,
bodyB: p.body,
length: 60,
stiffness: 1
}
var constraint = Constraint.create(options);
World.add(world, constraint);
}
// set the previous particle to be this one
prevParticle = p;
}
Engine.run(engine);
// create collision function
function collision(event){
var pairs = event.pairs;
for (var i = 0; i < pairs.length; i++){
var bodyA = pairs[i].bodyA;
var bodyB = pairs[i].bodyB;
// console.log(bodyA.label, bodyB.label)
}
}
// register callback to collision function
Events.on(engine, 'collisionStart', collision);
}
var draw = function() {
background(100,20,100);
for (b of boxes){
b.show();
}
prevParticle = null;
for (p of particles){
p.show();
if(prevParticle){
line(p.body.position.x, p.body.position.y,
prevParticle.body.position.x, prevParticle.body.position.y
)
prevParticle = p;
}
}
for (b of boundaries){
b.show();
}
}