-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpendulum.js
184 lines (158 loc) · 4.34 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
class Pendulum {
constructor() {
// BOB
this.r = h / 20;
this.colliding = false;
this.stamina = 50;
// HARPOON
this.haccX = 0;
this.haccY = g;
this.hvelX = 0;
this.hvelY = 0;
this.hposX = this.posX;
this.hposY = this.posY;
this.len;
this.start = true;
}
setup(x,y){
this.accX = 0;
this.accY = g;
this.velX = 0;
this.velY = 0;
this.posX = x;
this.posY = y ;
this.posXP = 0;
this.posYP = 0;
this.velA = 0;
this.trig = false;
this.hook = false;
this.alive = true;
this.life = 1;
}
update() {
if(this.life < 1 && this.alive){
this.life += .0005;
}
this.colliding = false;
// UNHOOKED PHYSICS
if(!this.hook){
this.velY = this.velY * friction + this.accY;
this.velX *= friction;
// BOB COLLISIONS
for(var i = 0; i < lineCollide.length; i++){
let l = lineCollide[i];
let vx = this.velX;
let vy = this.velY;
if(this.collide(l.x0, l.y0, l.x1, l.y1, this.posX+vx, this.posY+vy, this.r) && !this.colliding){
this.colliding = true;
//let velMag = sqrt(vx**2 + vy**2);
//let bounce = 10**(-velMag)/10+0.9;
vx = (vy * sin(l.ang*2) + vx * cos(l.ang*2)) * .8;
vy = -(vy * cos(l.ang*2) + vx * sin(l.ang*2)) * .8;
// vx = vx*(cos(l.ang) - sin(l.ang)) * .9;
// vy = vy*(sin(l.ang) - cos(l.ang)) * .9;
this.velX = vx;
this.velY = vy;
}
}
this.posXP = this.posX;
this.posYP = this.posY;
this.posX += this.velX;
this.posY += this.velY;
// HARPOON SHOT:
if (!this.trig) {
this.hposX = this.posX;
this.hposY = this.posY;
}
else {
this.hvelY += this.haccY*0.5;
this.hposX += this.hvelX;
this.hposY += this.hvelY;
this.len = sqrt(pow(this.posX-this.hposX,2)+pow(this.posY-this.hposY,2));
// HARPOON PLATFORM COLLISION
for(var i = 0; i < platformH.length; i++){
if (
this.hposX > platformH[i].x &&
this.hposX < platformH[i].x + platformH[i].w &&
this.hposY > platformH[i].y &&
this.hposY < platformH[i].y + platformH[i].h &&
this.len > 50
) {
this.hook = true;
this.ang = atan2(this.posX - this.hposX, this.posY - this.hposY);
this.angPre = atan2(this.posX - this.posXP, this.posY - this.posYP);
var velMag = sqrt(this.velY**2 + this.velX**2);
let plus = 1.2;
this.velA = velMag * sin(this.angPre - this.ang)/this.len * plus;
break;
}
}
}
}
// HOOKED PHYSICS
// BOB COLLISIONS
else{
for(var i = 0; i < lineCollide.length; i++){
let l = lineCollide[i];
if(!this.colliding){
if(this.collide(l.x0, l.y0, l.x1, l.y1, this.posX+this.velX, this.posY+this.velY, this.r)){
this.colliding = true;
this.velA *= -1;
}
}
}
// PENDULUM MOVEMENT
this.accA = -g * sin(this.ang) / this.len;
// console.log("ang " + this.ang);
// console.log("velA " + this.velA);
if(!this.colliding){
this.velA += this.accA;
}
this.ang += this.velA;
this.posX = this.len * sin(this.ang) + this.hposX;
this.posY = this.len * cos(this.ang) + this.hposY;
}
}
draw() {
noStroke();
fill(255);
circle(this.posX, this.posY, this.r * 2);
if(this.trig) circle(this.hposX, this.hposY, this.r/2);
}
collide(x1 , y1 , x2 , y2 , cx , cy , radius){
var dx31
var dx21
var dy31
var dy21
var d
var dx
var dy
var result // Int Or Bool
// Calculate Closest Point To Circle Center
dx31 = cx - x1
dx21 = x2 - x1
dy31 = cy - y1
dy21 = y2 - y1
d = ((dx21 * dx21) + (dy21 * dy21))
if (d != 0){
d = ((dx31 * dx21) + (dy31 * dy21)) / d
}
// Clip To The Line Segments Legal Bounds
if (d < 0.0){
d = 0
}
if (d > 1.0){
d = 1
}
dx = cx - (x1 + (dx21 * d))
dy = cy - (y1 + (dy21 * d))
let magnitude = sqrt((dx * dx) + (dy * dy)) // Square Root
if (radius >= magnitude){
result = true // True = Collision Occurred
let circle_to_line_intersection_x_pos = -dx
let circle_to_line_intersection_y_pos = -dy
}
else result = false // False = No Collision
return result
};
}