-
Notifications
You must be signed in to change notification settings - Fork 0
/
GravityWell.js
280 lines (254 loc) · 8.56 KB
/
GravityWell.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
if(typeof define!="function") {
var global = self;
self.define = function(nope, stuff) {
global.GravityWell = stuff();
};
}
define([], function() {
var idS = new UniqueIdGenerator();
var _2PI = 2*Math.PI
function GravityWell(x,y,mass,texture, stage) {
// Radians
this.rotation = 0;
// Radians/s
this.rotationSpeed = 0;
// Rotation acceleration
this.ar = 0;
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.ax = 0;
this.ay = 0;
this.mass = mass;
this.density = 2650;
this.calculateRadius();
this.id = idS.id;
if(texture) {
this.sprite = new PIXI.Sprite(texture);
this.updateTextureScale = this.updateTextureScale.bind(this);
stage.addChild(this.sprite);
this.updateSprite();
}
}
GravityWell.prototype.G = 6.674e-11;
GravityWell.prototype.hasGravity = true;
GravityWell.prototype.textureName = "asteroid";
GravityWell.prototype.acceleration = function(distanceSq, mass) {
if(distanceSq instanceof GravityWell) {
mass = distanceSq.mass;
radius = distanceSq.radius;
distanceSq = (this.x-distanceSq.x)*(this.x-distanceSq.x)+(this.y-distanceSq.y)*(this.y-distanceSq.y);
}
if(distanceSq > 0)
return (this.G*this.mass*mass) / distanceSq;
else
return 0;
}
/**
* Returns velocity required for this object to orbit `targetBody`.
* I'm not sure what exactly should happen when `targetBody` is lighter than
* this object.
* The return value scalar velocity. The angle needs to be calculated.
* **/
GravityWell.prototype.orbitalVelocity = function(targetBody) {
return Math.sqrt((this.G * (this.mass + targetBody.mass))/this.dist(targetBody));
}
/** Returns orbital velocity as {vx: ..., vy: ...} **/
GravityWell.prototype.orbitalVelocityCartesian = function(targetBody) {
var v = this.orbitalVelocity(targetBody);
var angle = this.angleTo(targetBody);
// turn angle by 90 degrees
angle += Math.PI/2;
return {vx: v*Math.cos(angle), vy: v*Math.sin(angle)};
}
/** Immediatelly sets the velocity as to orbit given body **/
GravityWell.prototype.orbitAround = function(targetBody) {
var v = this.orbitalVelocity(targetBody);
var angle = this.angleTo(targetBody);
// turn angle by 90 degrees
angle += Math.PI/2;
this.vx = v*Math.cos(angle)+targetBody.vx;
this.vy = v*Math.sin(angle)+targetBody.vy;
}
GravityWell.prototype.angleTo = function(object) {
return Math.atan2(-this.y+object.y, -this.x+object.x);
}
GravityWell.prototype.distSq = function(object) {
return (this.x-object.x)*(this.x-object.x)+(this.y-object.y)*(this.y-object.y)
}
GravityWell.prototype.dist = function(object) {
return Math.sqrt(this.distSq(object));
}
GravityWell.prototype.accelerate = function(direction, magnitude) {
var deltaV = magnitude/this.mass;
this.vx += deltaV*Math.cos(direction);
this.vy += deltaV*Math.sin(direction);
//this.sprite.rotation = direction+Math.PI/2;
}
GravityWell.prototype.consume = function(object) {
console.log(this.constructor.name+"#"+this.id+" consumed "+object.constructor.name+"#"+object.id);
if(object.isProjectile) {
this.mass -= 10000000;
this.vx += object.vx/(this.mass);
this.vy += object.vy/(this.mass);
if(this.isProjectile && object.mass>0) {
object.consume(this);
}
}
else {
var total = object.mass+this.mass;
var thisratio = this.mass/total;
var objectratio = object.mass/total;
this.vx = object.vx*objectratio + this.vx*thisratio;
this.vy = object.vy*objectratio + this.vy*thisratio;
this.x = object.x*objectratio + this.x*thisratio;
this.y = object.y*objectratio + this.y*thisratio;
this.mass = total;
}
this.calculateRadius();
//this.updateSprite();
}
/** Gives the given object velocity that is combination of this objects velicoty and the requested vector **/
/** This method assumes the objects toutch and that the launch direction goes away from this object **/
GravityWell.prototype.launchObject = function(object, vx, vy) {
object.vx = this.vx + vx;
object.vy = this.vy + vy;
}
GravityWell.prototype.calculateRadius = function() {
this.radius = Math.cbrt((3*this.mass)/(Math.PI*4*this.density));
if(this.sprite)
this.updateTextureScale();
}
GravityWell.prototype.move = function(dt) {
// If acceleration
this.vx += this.ax*dt;
this.vy += this.ay*dt;
this.rotationSpeed += this.ar*dt;
//this.rotationSpeed = 0;
//this.rotationSpeed -= 0.000002*Math.sign(this.rotationSpeed);
this.x += this.vx*dt;
this.y += this.vy*dt;
this.rotation += this.rotationSpeed*dt;
if(this.rotation>_2PI || this.rotation<-_2PI) {
this.rotation = this.rotation%_2PI;
}
if(this.sprite) {
this.sprite.position.x = this.x;
this.sprite.position.y = this.y;
this.sprite.rotation = this.rotation;
}
}
/*GravityWell.prototype.rotate = function(dt) {
this.rotation += this.rotationSpeed*dt;
if(this.sprite) {
this.sprite.rotation = this.rotation;
}
} */
GravityWell.SAVE_DATA_LENGTH = 9-1;
GravityWell.prototype.saveToArray = function(offset, array) {
if(typeof offset=="undefined") {
offset = 0;
}
if(typeof array=="undefined") {
array = new Float64Array(GravityWell.SAVE_DATA_LENGTH);
}
array[offset] = this.id;
array[offset+1] = this.mass;
array[offset+2] = this.radius;
array[offset+3] = this.x;
array[offset+4] = this.y;
array[offset+5] = this.vx;
array[offset+6] = this.vy;
array[offset+7] = this.rotation;
//array[offset+8] = this.rotationSpeed;
return array;
}
GravityWell.prototype.loadFromArray = function(offset, array) {
this.id = array[offset];
this.mass = array[offset+1];
this.radius = array[offset+2];
this.x = array[offset+3];
this.y = array[offset+4];
this.vx = array[offset+5];
this.vy = array[offset+6];
this.rotation = array[offset+7];
if(typeof this.rotation!="number") {
console.log("ERROR: ",offset, array);
throw new Error("ble");
}
//this.rotationSpeed = array[offset+8];
if(this.sprite)
this.updateSprite();
}
GravityWell.prototype.savePropertiesToArray = function(properties, offset, array) {
if(typeof offset=="undefined") {
offset = 0;
}
if(typeof array=="undefined") {
array = new Float64Array(properties.length);
}
for(var i=0,l=properties.length; i<l; i++) {
array[offset+i] = this[properties[i]];
}
return array;
}
GravityWell.prototype.loadPropertiesFromArray = function(properties, offset, array) {
var ar_len = array.length;
for(var i=0,l=properties.length; i<l && i+offset<ar_len; i++) {
this[properties[i]] = array[offset+i];
}
if(this.sprite)
this.updateSprite();
}
GravityWell.prototype.configurations = {
acceleration: ["id", "ax", "ay", "ar"],
acceleration_no_id: ["ax", "ay", "ar"],
}
GravityWell.prototype.updateSprite = function() {
if(this.sprite) {
this.sprite.rotation = this.rotation;
this.sprite.position.x = this.x;
this.sprite.position.y = this.y;
// center the sprite's anchor point
this.sprite.anchor.x = 0.5;
this.sprite.anchor.y = 0.5;
if(this.sprite.texture.valid)
this.updateTextureScale();
else
this.sprite.texture.on("update", this.updateTextureScale);
}
}
GravityWell.prototype.destroy = function() {
if(this.sprite) {
this.sprite.texture.removeListener("update", this.updateTextureScale);
if(this.sprite.parent)
this.sprite.parent.removeChild(this.sprite);
}
}
GravityWell.prototype.updateTextureScale = function() {
this.sprite.scale.set(2*this.radius/this.sprite.texture.width,2*(this.radius/this.sprite.texture.height));
}
GravityWell.prototype.toString = function() {
return JSON.stringify(this);
}
GravityWell.prototype.logThrottle = function() {
var t = performance.now();
if(!this.lastLogMessage || t - this.lastLogMessage>100) {
console.log.apply(console, arguments);
this.lastLogMessage = t;
}
}
function UniqueIdGenerator(prefix) {
var id = 0;
if(typeof prefix != "string")
prefix = "";
Object.defineProperty(this, "id", {
get: () => {return prefix+(++id);},
enumerable: true,
configurable: false
}
);
}
return GravityWell;
});