-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspring.js
58 lines (41 loc) · 1.26 KB
/
spring.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
class Spring {
constructor(x, y) {
this.mouseJoint = null;
}
update(x, y) {
if (this.mouseJoint !== null) {
let mouseWorld = scaleToWorld(x, y);
this.mouseJoint.SetTarget(mouseWorld);
}
}
display() {
if (this.mouseJoint !== null) {
let posA = this.mouseJoint.GetAnchorA();
let posB = this.mouseJoint.GetAnchorB();
let v1 = scaleToPixels(posA.x, posA.y);
let v2 = scaleToPixels(posB.x, posB.y);
stroke(200);
strokeWeight(2);
line(v1.x, v1.y, v2.x, v2.y);
}
}
bind(x, y, ele) {
let md = new box2d.b2MouseJointDef();
md.bodyA = world.CreateBody(new box2d.b2BodyDef());
md.bodyB = ele.body;
ele.body.SetFixedRotation(true);
let mp = scaleToWorld(x, y);
md.target = mp;
md.maxForce = 1000.0 * ele.body.m_mass;
md.frequencyHz = 5.0;
md.dampingRatio = 0.9;
this.mouseJoint = world.CreateJoint(md);
}
destroy() {
if (this.mouseJoint !== null) {
this.mouseJoint.GetBodyB().SetFixedRotation(false);
world.DestroyJoint(this.mouseJoint);
this.mouseJoint = null;
}
}
}