-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpendulum.js
80 lines (60 loc) · 1.91 KB
/
pendulum.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
class Pendulum {
constructor(x, y) {
this.len = 96.0;
this.cart = new Box(x, y, 30, 16);
this.wheel = new Circle(x, y, 24);
this.stick = new Box(x, y, 8, this.len, true);
this.handle = new Box(x, y, 32, 16);
let wjd = new box2d.b2WeldJointDef();
wjd.bodyA = this.cart.body;
wjd.bodyB = this.stick.body;
wjd.localAnchorB = scaleToWorld(0, this.len/2);
world.CreateJoint(wjd);
wjd = new box2d.b2WeldJointDef();
wjd.bodyA = this.handle.body;
wjd.bodyB = this.stick.body;
wjd.localAnchorB = scaleToWorld(0, -this.len/2);
world.CreateJoint(wjd);
wjd = new box2d.b2WheelJointDef();
wjd.bodyA = this.wheel.body;
wjd.bodyB = this.cart.body;
wjd.frequencyHz = 30;
wjd.dampingRatio = 1.0;
wjd.maxMotorTorque = 1000;
this.motor = world.CreateJoint(wjd);
this.motor.EnableMotor(true);
this.motor.SetMotorSpeed(0);
this.elements = [this.wheel, this.stick, this.handle];
}
deletion() {
world.DestroyBody(this.cart.body);
world.DestroyBody(this.wheel.body);
world.DestroyBody(this.stick.body);
world.DestroyBody(this.handle.body);
this.elements = null;
}
getPosition() {
return scaleToPixels(this.wheel.body.GetPosition());
}
getVelocity() {
return this.cart.body.GetLinearVelocity();
}
getAngleRadians() {
let radians = this.stick.body.GetAngleRadians();
while(radians > PI) radians -= 2*PI;
while(radians < -PI) radians += 2*PI;
return radians;
}
setMotorSpeed(speed) {
this.motor.SetMotorSpeed(speed);
}
display() {
this.wheel.display();
this.cart.display();
this.handle.display();
this.stick.display();
}
getElements() {
return this.elements;
}
}