-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.js
137 lines (114 loc) · 4.6 KB
/
circle.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
class Circle {
constructor(game, x, y, velocity, mass, maxSpeed) {
Object.assign(this, { game, x, y, velocity, mass, maxSpeed });
this.points = [{ x: this.x, y: this.y }];
this.radius = Math.ceil(Math.sqrt(this.mass / Math.PI));
this.testSpeed();
};
testSpeed() {
var speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
if (this.maxSpeed !== false && speed > this.maxSpeed) {
var ratio = this.maxSpeed / speed;
this.velocity.x *= ratio;
this.velocity.y *= ratio;
}
};
collide(other) {
return distance(this, other) < this.radius + other.radius;
};
move() {
// move
this.x += this.velocity.x * this.game.clockTick;
this.y += this.velocity.y * this.game.clockTick;
};
update() {
this.points.push({ x: this.x, y: this.y });
// collision with other circles
for (var i = 0; i < this.game.entities.length && this.game.entities[i] !== this; i++) {
var ent = this.game.entities[i];
if (this.collide(ent)) {
// push away from each other
//var dist = distance(this, ent);
//var delta = this.radius + ent.radius - dist;
//var difX = (this.x - ent.x) / dist;
//var difY = (this.y - ent.y) / dist;
//this.x += difX * delta / 2;
//this.y += difY * delta / 2;
//ent.x -= difX * delta / 2;
//ent.y -= difY * delta / 2;
// swap velocities
var totalMass = this.mass + ent.mass;
var temp = { x: this.velocity.x, y: this.velocity.y };
this.velocity.x = ((this.mass - ent.mass) * this.velocity.x + 2 * ent.mass * ent.velocity.x) / totalMass * PARAMS.FRICTION;
this.velocity.y = ((this.mass - ent.mass) * this.velocity.y + 2 * ent.mass * ent.velocity.y) / totalMass * PARAMS.FRICTION;
ent.velocity.x = (2 * this.mass * temp.x + (ent.mass - this.mass) * ent.velocity.x) / totalMass * PARAMS.FRICTION;
ent.velocity.y = (2 * this.mass * temp.y + (ent.mass - this.mass) * ent.velocity.y) / totalMass * PARAMS.FRICTION;
}
}
for (var i = 0; i < this.game.entities.length; i++) {
var ent = this.game.entities[i];
if (ent !== this && !(ent instanceof Center)) {
var dist = distance(this, ent);
var difX = (ent.x - this.x) / dist;
var difY = (ent.y - this.y) / dist;
this.velocity.x += difX * PARAMS.ACCELERATION / (dist * dist) * ent.mass;
this.velocity.y += difY * PARAMS.ACCELERATION / (dist * dist) * ent.mass;
}
}
this.testSpeed();
this.velocity.x -= (1 - PARAMS.FRICTION) * this.game.clockTick * this.velocity.x;
this.velocity.y -= (1 - PARAMS.FRICTION) * this.game.clockTick * this.velocity.y;
};
draw(ctx) {
ctx.beginPath();
ctx.setLineDash([]);
ctx.fillStyle = "White";
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
if (document.getElementById("visual").checked) {
var max = 1000;
if (this.points.length > max) this.points.splice(0,1);
for (var i = 0; i < this.points.length; i++) {
ctx.beginPath();
ctx.fillStyle = "Red";
var point = this.points[this.points.length - 1 - i];
ctx.arc(point.x, point.y, 1, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
}
}
};
};
class Center {
constructor(game) {
Object.assign(this, { game });
this.x = 0;
this.y = 0;
this.size = 25;
};
move() { };
update() {
var point = { x: 0, y: 0 };
var total = 0;
for (var i = 0; i < this.game.entities.length; i++) {
var ent = this.game.entities[i];
if (this !== ent) {
total += ent.mass;
point.x += ent.x * ent.mass;
point.y += ent.y * ent.mass;
}
}
this.x = point.x / total;
this.y = point.y / total;
};
draw(ctx) {
ctx.beginPath()
ctx.strokeStyle = "LightBlue";
ctx.moveTo(this.x, this.y - this.size);
ctx.lineTo(this.x, this.y + this.size);
ctx.moveTo(this.x - this.size, this.y);
ctx.lineTo(this.x + this.size, this.y);
ctx.stroke();
};
};