- 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
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
import {clamp} from "./math.js";
import pool from "./../system/pooling.js";
/**
* @classdesc
* a generic 2D Vector Object
*/
export default class Vector2d {
/**
* @param {number} [x=0] - x value of the vector
* @param {number} [y=0] - y value of the vector
*/
constructor(x = 0, y = 0) {
this.onResetEvent(x, y);
}
/**
* @param {number} [x=0] - x value of the vector
* @param {number} [y=0] - y value of the vector
* @ignore
*/
onResetEvent(x = 0, y = 0) {
/**
* x value of the vector
* @type {number}
*/
this.x = x;
/**
* y value of the vector
* @type {number}
*/
this.y = y;
}
/**
* @param {number} [x=0] - x value of the vector
* @param {number} [y=0] - y value of the vector
* @ignore
*/
_set(x, y) {
this.x = x;
this.y = y;
return this;
}
/**
* set the Vector x and y properties to the given values
* @param {number} x
* @param {number} y
* @returns {Vector2d} Reference to this object for method chaining
*/
set(x, y) {
if (x !== +x || y !== +y) {
throw new Error(
"invalid x,y parameters (not a number)"
);
}
return this._set(x, y);
}
/**
* set the Vector x and y properties to 0
* @returns {Vector2d} Reference to this object for method chaining
*/
setZero() {
return this.set(0, 0);
}
/**
* set the Vector x and y properties using the passed vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
setV(v) {
return this._set(v.x, v.y);
}
/**
* Add the passed vector to this vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
add(v) {
return this._set(this.x + v.x, this.y + v.y);
}
/**
* Substract the passed vector to this vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
sub(v) {
return this._set(this.x - v.x, this.y - v.y);
}
/**
* Multiply this vector values by the given scalar
* @param {number} x
* @param {number} [y=x]
* @returns {Vector2d} Reference to this object for method chaining
*/
scale(x, y = x) {
return this._set(this.x * x, this.y * y);
}
/**
* Convert this vector into isometric coordinate space
* @returns {Vector2d} Reference to this object for method chaining
*/
toIso() {
return this._set(this.x - this.y, (this.x + this.y) * 0.5);
}
/**
* Convert this vector into 2d coordinate space
* @returns {Vector2d} Reference to this object for method chaining
*/
to2d() {
return this._set(this.y + this.x / 2, this.y - this.x / 2);
}
/**
* Multiply this vector values by the passed vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
scaleV(v) {
return this._set(this.x * v.x, this.y * v.y);
}
/**
* Divide this vector values by the passed value
* @param {number} n - the value to divide the vector by
* @returns {Vector2d} Reference to this object for method chaining
*/
div(n) {
return this._set(this.x / n, this.y / n);
}
/**
* Update this vector values to absolute values
* @returns {Vector2d} Reference to this object for method chaining
*/
abs() {
return this._set((this.x < 0) ? -this.x : this.x, (this.y < 0) ? -this.y : this.y);
}
/**
* Clamp the vector value within the specified value range
* @param {number} low
* @param {number} high
* @returns {Vector2d} new me.Vector2d
*/
clamp(low, high) {
return new Vector2d(clamp(this.x, low, high), clamp(this.y, low, high));
}
/**
* Clamp this vector value within the specified value range
* @param {number} low
* @param {number} high
* @returns {Vector2d} Reference to this object for method chaining
*/
clampSelf(low, high) {
return this._set(clamp(this.x, low, high), clamp(this.y, low, high));
}
/**
* Update this vector with the minimum value between this and the passed vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
minV(v) {
return this._set((this.x < v.x) ? this.x : v.x, (this.y < v.y) ? this.y : v.y);
}
/**
* Update this vector with the maximum value between this and the passed vector
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
maxV(v) {
return this._set((this.x > v.x) ? this.x : v.x, (this.y > v.y) ? this.y : v.y);
}
/**
* Floor the vector values
* @returns {Vector2d} new me.Vector2d
*/
floor() {
return new Vector2d(Math.floor(this.x), Math.floor(this.y));
}
/**
* Floor this vector values
* @returns {Vector2d} Reference to this object for method chaining
*/
floorSelf() {
return this._set(Math.floor(this.x), Math.floor(this.y));
}
/**
* Ceil the vector values
* @returns {Vector2d} new me.Vector2d
*/
ceil() {
return new Vector2d(Math.ceil(this.x), Math.ceil(this.y));
}
/**
* Ceil this vector values
* @returns {Vector2d} Reference to this object for method chaining
*/
ceilSelf() {
return this._set(Math.ceil(this.x), Math.ceil(this.y));
}
/**
* Negate the vector values
* @returns {Vector2d} new me.Vector2d
*/
negate() {
return new Vector2d(-this.x, -this.y);
}
/**
* Negate this vector values
* @returns {Vector2d} Reference to this object for method chaining
*/
negateSelf() {
return this._set(-this.x, -this.y);
}
/**
* Copy the x,y values of the passed vector to this one
* @param {Vector2d} v
* @returns {Vector2d} Reference to this object for method chaining
*/
copy(v) {
return this._set(v.x, v.y);
}
/**
* return true if this vector is equal to the given values or vector
* @param {number|Vector2d|ObservableVector2d} x
* @param {number} [y]
* @returns {boolean}
*/
equals(...args) {
let _x, _y;
if (args.length === 2) {
// x, y
[_x, _y] = args;
} else {
// vector
[_x, _y] = [args[0].x, args[0].y];
}
return this.x === _x && this.y === _y;
}
/**
* normalize this vector (scale the vector so that its magnitude is 1)
* @returns {Vector2d} Reference to this object for method chaining
*/
normalize() {
return this.div(this.length() || 1);
}
/**
* change this vector to be perpendicular to what it was before.<br>
* (Effectively rotates it 90 degrees in a clockwise direction)
* @returns {Vector2d} Reference to this object for method chaining
*/
perp() {
return this._set(this.y, -this.x);
}
/**
* Rotate this vector (counter-clockwise) by the specified angle (in radians).
* @param {number} angle - The angle to rotate (in radians)
* @param {Vector2d|ObservableVector2d} [v] - an optional point to rotate around
* @returns {Vector2d} Reference to this object for method chaining
*/
rotate(angle, v) {
let cx = 0;
let cy = 0;
if (typeof v === "object") {
cx = v.x;
cy = v.y;
}
let x = this.x - cx;
let y = this.y - cy;
let c = Math.cos(angle);
let s = Math.sin(angle);
return this._set(x * c - y * s + cx, x * s + y * c + cy);
}
/**
* return the dot product of this vector and the passed one
* @param {Vector2d} v
* @returns {number} The dot product.
*/
dot(v) {
return this.x * v.x + this.y * v.y;
}
/**
* return the cross product of this vector and the passed one
* @param {Vector2d} v
* @returns {number} The cross product.
*/
cross(v) {
return this.x * v.y - this.y * v.x;
}
/**
* return the square length of this vector
* @returns {number} The length^2 of this vector.
*/
length2() {
return this.dot(this);
}
/**
* return the length (magnitude) of this vector
* @returns {number} the length of this vector
*/
length() {
return Math.sqrt(this.length2());
}
/**
* Linearly interpolate between this vector and the given one.
* @param {Vector2d} v
* @param {number} alpha - distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).
* @returns {Vector2d} Reference to this object for method chaining
*/
lerp(v, alpha) {
this.x += (v.x - this.x) * alpha;
this.y += (v.y - this.y) * alpha;
return this;
}
/**
* interpolate the position of this vector towards the given one by the given maximum step.
* @param {Vector2d} target
* @param {number} step - the maximum step per iteration (Negative values will push the vector away from the target)
* @returns {Vector2d} Reference to this object for method chaining
*/
moveTowards(target, step) {
let angle = Math.atan2(target.y - this.y, target.x - this.x);
let distance = this.distance(target);
if (distance === 0 || (step >= 0 && distance <= step * step)) {
return target;
}
this.x += Math.cos(angle) * step;
this.y += Math.sin(angle) * step;
return this;
}
/**
* return the distance between this vector and the passed one
* @param {Vector2d} v
* @returns {number}
*/
distance(v) {
let dx = this.x - v.x, dy = this.y - v.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* return the angle between this vector and the passed one
* @param {Vector2d} v
* @returns {number} angle in radians
*/
angle(v) {
return Math.acos(clamp(this.dot(v) / (this.length() * v.length()), -1, 1));
}
/**
* project this vector on to another vector.
* @param {Vector2d} v - The vector to project onto.
* @returns {Vector2d} Reference to this object for method chaining
*/
project(v) {
return this.scale(this.dot(v) / v.length2());
}
/**
* Project this vector onto a vector of unit length.<br>
* This is slightly more efficient than `project` when dealing with unit vectors.
* @param {Vector2d} v - The unit vector to project onto.
* @returns {Vector2d} Reference to this object for method chaining
*/
projectN(v) {
return this.scale(this.dot(v));
}
/**
* return a clone copy of this vector
* @returns {Vector2d} new me.Vector2d
*/
clone() {
return pool.pull("Vector2d", this.x, this.y);
}
/**
* convert the object to a string representation
* @returns {string}
*/
toString() {
return "x:" + this.x + ",y:" + this.y;
}
}