This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleg.js
86 lines (72 loc) · 2.37 KB
/
leg.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
function Leg(emit, color, M1ID, M2ID, centerX){
this.center = createVector(centerX, height/2-h);
this.points = [];
this.M1 = 0;
this.M2 = 0;
this.lM1 = 0;
this.lM2 = 0;
this.emit = emit;
this.color = color;
this.M1ID = M1ID;
this.M2ID = M2ID;
this.centerX = centerX;
}
Leg.prototype.draw = function(px, py){
this.point = createVector(this.centerX - px, py);
let aVec = this.point.sub(this.center).limit(max);
this.O = -aVec.heading();
this.a = aVec.mag();
this.bones = [];
if (this.a < min) this.a = min;
if(b > this.a) {
B = acos((this.a*this.a + c*c - b*b) / (2*this.a*c));
A = asin( this.a/b * sin(B) );
C = asin( c/b * sin(B) );
} else {
A = acos((b*b + c*c - this.a*this.a) / (2*b*c));
B = asin( b/this.a * sin(A) );
C = asin( c/b * sin(B) );
}
//Normalizing
this.M1 = round(180+90-B+this.O);
text("M1: " + this.M1, 10,10);
this.bones[0] = new Bone(this.color, this.centerX, height/2-h, -B + this.O, c);
this.bones[1] = new Bone(this.color, this.centerX, height/2-h, - A + 180 -B + this.O, b);
this.bones[2] = new Bone(this.color, this.centerX, height/2-h, B + this.O, c);
this.bones[3] = new Bone(this.color, this.centerX, height/2-h, A + 180 +B + this.O, b);
//Normalizing
this.M2 = round(-this.bones[3].ang+A+180+90);
text("M2: " + this.M2, 10,30);
this.bones[0].draw();
this.bones[1].fix(this.bones[0]);
this.bones[1].update();
this.bones[1].draw();
this.bones[2].draw();
this.bones[3].fix(this.bones[2]);
this.bones[3].update();
this.bones[3].draw();
this.points.push(this.bones[1].pos2);
noFill();
stroke(255);
strokeWeight(1);
for(let i=2;i<this.points.length-1;i++) {
line(this.points[i-1].x, this.points[i-1].y, this.points[i].x,this.points[i].y)
}
if(this.points.length > 150) {
this.points = this.points.slice(Math.max(this.points.length - 150, 1));
}
}
Leg.prototype.drawLimits = function() {
ellipse(this.centerX, height/2-h, min*2, min*2);
ellipse(this.centerX, height/2-h, max*2, max*2);
}
Leg.prototype.emitSocket = function() {
if(this.M1 != this.lM1){
this.emit(this.M1ID, this.M1);
this.lM1 = this.M1;
}
if(this.M2 != this.lM2){
this.emit(this.M2ID, this.M2);
this.lM2 = this.M2;
}
}