-
Notifications
You must be signed in to change notification settings - Fork 0
/
slingshot.js
40 lines (35 loc) · 861 Bytes
/
slingshot.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
// Angry Birds with Matter.js
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/challenges/content/videos/challenges/138-angry-birds-with-matterjs
// https://youtu.be/TDQzoe9nslY
// Code from Challenge: https://editor.p5js.org/codingtrain/sketches/UOR4nIcNS
class SlingShot {
constructor(x, y, rock) {
const options = {
pointA: {
x: x,
y: y
},
bodyB: rock,
stiffness: 0.02,
length: 40
};
this.sling = Constraint.create(options);
World.add(world, this.sling);
}
fly() {
this.sling.bodyB = null;
}
show() {
if (this.sling.bodyB) {
stroke(0);
strokeWeight(4);
const posA = this.sling.pointA;
const posB = this.sling.bodyB.position;
line(posA.x, posA.y, posB.x, posB.y);
}
}
attach(body) {
this.sling.bodyB = rock;
}
}