-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrope.js
93 lines (80 loc) · 1.91 KB
/
rope.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
class Rope
{
constructor(nlink, pointA)
{
this.nlink = nlink
const group = Body.nextGroup(true);
const rects = Composites.stack(100, 100, this.nlink, 1, 5, 5, function(x, y) {
return Bodies.rectangle(x, y, 30, 5, { collisionFilter: { group: group } });
});
this.pointA = pointA;
this.body = Composites.chain(rects, 0.1, 0, -0.6, 0, {stiffness: 0.1, length: 0.1, render: {type: 'line'}});
World.add(engine.world, this.body);
Composite.add(rects, Constraint.create({
pointA: this.pointA,
bodyB: rects.bodies[0],
pointB: {x: -25, y: 0},
length:10,
stiffness: 0.1
}));
}
break()
{ //Matter.Composite.clear(this.rope,true);
this.body = null;
}
show()
{
if(this.body!=null)
{
for (let i = 0; i < this.body.bodies.length-1; i++)
{
this.drawVertices(this.body.bodies[i].vertices);
}
}
}
drawVertices(vertices)
{
beginShape();
fill('#FFF717')
noStroke();
for (let i = 0; i < vertices.length; i++)
{
vertex(vertices[i].x, vertices[i].y);
}
endShape(CLOSE);
}
showConstraints(constraints)
{
if(constraints!=null)
{
for (let i = 0; i < constraints.length; i++) {
this.drawConstraint(constraints[i]);
}
}
}
drawConstraint(constraint) {
if(constraint!=null)
{
const offsetA = constraint.pointA;
let posA = {x:0, y:0};
if (constraint.bodyA) {
posA = constraint.bodyA.position;
}
const offsetB = constraint.pointB;
let posB = {x:0, y:0};
if (constraint.bodyB) {
posB = constraint.bodyB.position;
}
push()
strokeWeight(4);
stroke(255);
line(
posA.x + offsetA.x,
posA.y + offsetA.y,
posB.x + offsetB.x,
posB.y + offsetB.y
);
pop();
}
}
}