@esengine/ecs-framework-math 1.0.3 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/bin/Animation/Easing.d.ts +150 -150
- package/bin/Animation/Easing.d.ts.map +1 -1
- package/bin/Animation/Easing.js +151 -151
- package/bin/Animation/Easing.js.map +1 -1
- package/bin/Animation/Interpolation.d.ts +102 -102
- package/bin/Animation/Interpolation.d.ts.map +1 -1
- package/bin/Animation/Interpolation.js +104 -104
- package/bin/Animation/Interpolation.js.map +1 -1
- package/bin/Animation/index.d.ts.map +1 -1
- package/bin/Animation/index.js.map +1 -1
- package/bin/Circle.d.ts +159 -159
- package/bin/Circle.d.ts.map +1 -1
- package/bin/Circle.js +159 -159
- package/bin/Circle.js.map +1 -1
- package/bin/Collision/CollisionDetector.d.ts +64 -64
- package/bin/Collision/CollisionDetector.d.ts.map +1 -1
- package/bin/Collision/CollisionDetector.js +66 -66
- package/bin/Collision/CollisionDetector.js.map +1 -1
- package/bin/Color.d.ts +277 -0
- package/bin/Color.d.ts.map +1 -0
- package/bin/Color.js +470 -0
- package/bin/Color.js.map +1 -0
- package/bin/Fixed32.d.ts +266 -0
- package/bin/Fixed32.d.ts.map +1 -0
- package/bin/Fixed32.js +381 -0
- package/bin/Fixed32.js.map +1 -0
- package/bin/FixedMath.d.ts +109 -0
- package/bin/FixedMath.d.ts.map +1 -0
- package/bin/FixedMath.js +264 -0
- package/bin/FixedMath.js.map +1 -0
- package/bin/FixedVector2.d.ts +293 -0
- package/bin/FixedVector2.d.ts.map +1 -0
- package/bin/FixedVector2.js +413 -0
- package/bin/FixedVector2.js.map +1 -0
- package/bin/MathUtils.d.ts +205 -205
- package/bin/MathUtils.d.ts.map +1 -1
- package/bin/MathUtils.js +206 -206
- package/bin/MathUtils.js.map +1 -1
- package/bin/Matrix3.d.ts +158 -139
- package/bin/Matrix3.d.ts.map +1 -1
- package/bin/Matrix3.js +179 -151
- package/bin/Matrix3.js.map +1 -1
- package/bin/Rectangle.d.ts +144 -144
- package/bin/Rectangle.d.ts.map +1 -1
- package/bin/Rectangle.js +144 -144
- package/bin/Rectangle.js.map +1 -1
- package/bin/Vector2.d.ts +202 -186
- package/bin/Vector2.d.ts.map +1 -1
- package/bin/Vector2.js +198 -188
- package/bin/Vector2.js.map +1 -1
- package/bin/Vector3.d.ts +257 -0
- package/bin/Vector3.d.ts.map +1 -0
- package/bin/Vector3.js +372 -0
- package/bin/Vector3.js.map +1 -0
- package/bin/index.d.ts +8 -1
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +9 -0
- package/bin/index.js.map +1 -1
- package/package.json +66 -67
package/bin/Vector2.js
CHANGED
|
@@ -9,110 +9,110 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export class Vector2 {
|
|
11
11
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
* 创建2D向量
|
|
13
|
+
* @param x X分量,默认为0
|
|
14
|
+
* @param y Y分量,默认为0
|
|
15
|
+
*/
|
|
16
16
|
constructor(x = 0, y = 0) {
|
|
17
17
|
this.x = x;
|
|
18
18
|
this.y = y;
|
|
19
19
|
}
|
|
20
20
|
// 基础属性
|
|
21
21
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
* 获取向量长度(模)
|
|
23
|
+
*/
|
|
24
24
|
get length() {
|
|
25
25
|
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
* 获取向量长度的平方
|
|
29
|
+
*/
|
|
30
30
|
get lengthSquared() {
|
|
31
31
|
return this.x * this.x + this.y * this.y;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
* 获取向量角度(弧度)
|
|
35
|
+
*/
|
|
36
36
|
get angle() {
|
|
37
37
|
return Math.atan2(this.y, this.x);
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
* 检查是否为零向量
|
|
41
|
+
*/
|
|
42
42
|
get isZero() {
|
|
43
43
|
return this.x === 0 && this.y === 0;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
* 检查是否为单位向量
|
|
47
|
+
*/
|
|
48
48
|
get isUnit() {
|
|
49
49
|
const lenSq = this.lengthSquared;
|
|
50
50
|
return Math.abs(lenSq - 1) < Number.EPSILON;
|
|
51
51
|
}
|
|
52
52
|
// 基础运算
|
|
53
53
|
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
* 设置向量分量
|
|
55
|
+
* @param x X分量
|
|
56
|
+
* @param y Y分量
|
|
57
|
+
* @returns 当前向量实例(链式调用)
|
|
58
|
+
*/
|
|
59
59
|
set(x, y) {
|
|
60
60
|
this.x = x;
|
|
61
61
|
this.y = y;
|
|
62
62
|
return this;
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
* 复制另一个向量的值
|
|
66
|
+
* @param other 源向量
|
|
67
|
+
* @returns 当前向量实例(链式调用)
|
|
68
|
+
*/
|
|
69
69
|
copy(other) {
|
|
70
70
|
this.x = other.x;
|
|
71
71
|
this.y = other.y;
|
|
72
72
|
return this;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
* 克隆当前向量
|
|
76
|
+
* @returns 新的向量实例
|
|
77
|
+
*/
|
|
78
78
|
clone() {
|
|
79
79
|
return new Vector2(this.x, this.y);
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
* 向量加法
|
|
83
|
+
* @param other 另一个向量
|
|
84
|
+
* @returns 当前向量实例(链式调用)
|
|
85
|
+
*/
|
|
86
86
|
add(other) {
|
|
87
87
|
this.x += other.x;
|
|
88
88
|
this.y += other.y;
|
|
89
89
|
return this;
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
* 向量减法
|
|
93
|
+
* @param other 另一个向量
|
|
94
|
+
* @returns 当前向量实例(链式调用)
|
|
95
|
+
*/
|
|
96
96
|
subtract(other) {
|
|
97
97
|
this.x -= other.x;
|
|
98
98
|
this.y -= other.y;
|
|
99
99
|
return this;
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
* 向量数乘
|
|
103
|
+
* @param scalar 标量
|
|
104
|
+
* @returns 当前向量实例(链式调用)
|
|
105
|
+
*/
|
|
106
106
|
multiply(scalar) {
|
|
107
107
|
this.x *= scalar;
|
|
108
108
|
this.y *= scalar;
|
|
109
109
|
return this;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
* 向量数除
|
|
113
|
+
* @param scalar 标量
|
|
114
|
+
* @returns 当前向量实例(链式调用)
|
|
115
|
+
*/
|
|
116
116
|
divide(scalar) {
|
|
117
117
|
if (scalar === 0) {
|
|
118
118
|
throw new Error('不能除以零');
|
|
@@ -122,9 +122,9 @@ export class Vector2 {
|
|
|
122
122
|
return this;
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
* 向量取反
|
|
126
|
+
* @returns 当前向量实例(链式调用)
|
|
127
|
+
*/
|
|
128
128
|
negate() {
|
|
129
129
|
this.x = -this.x;
|
|
130
130
|
this.y = -this.y;
|
|
@@ -132,25 +132,25 @@ export class Vector2 {
|
|
|
132
132
|
}
|
|
133
133
|
// 向量运算
|
|
134
134
|
/**
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
* 计算与另一个向量的点积
|
|
136
|
+
* @param other 另一个向量
|
|
137
|
+
* @returns 点积值
|
|
138
|
+
*/
|
|
139
139
|
dot(other) {
|
|
140
140
|
return this.x * other.x + this.y * other.y;
|
|
141
141
|
}
|
|
142
142
|
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
143
|
+
* 计算与另一个向量的叉积(2D中返回标量)
|
|
144
|
+
* @param other 另一个向量
|
|
145
|
+
* @returns 叉积值
|
|
146
|
+
*/
|
|
147
147
|
cross(other) {
|
|
148
148
|
return this.x * other.y - this.y * other.x;
|
|
149
149
|
}
|
|
150
150
|
/**
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
* 向量归一化(转换为单位向量)
|
|
152
|
+
* @returns 当前向量实例(链式调用)
|
|
153
|
+
*/
|
|
154
154
|
normalize() {
|
|
155
155
|
const len = this.length;
|
|
156
156
|
if (len === 0) {
|
|
@@ -159,38 +159,38 @@ export class Vector2 {
|
|
|
159
159
|
return this.divide(len);
|
|
160
160
|
}
|
|
161
161
|
/**
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
* 获取归一化后的向量(不修改原向量)
|
|
163
|
+
* @returns 新的单位向量
|
|
164
|
+
*/
|
|
165
165
|
normalized() {
|
|
166
166
|
return this.clone().normalize();
|
|
167
167
|
}
|
|
168
168
|
// 几何运算
|
|
169
169
|
/**
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
170
|
+
* 计算到另一个向量的距离
|
|
171
|
+
* @param other 另一个向量
|
|
172
|
+
* @returns 距离值
|
|
173
|
+
*/
|
|
174
174
|
distanceTo(other) {
|
|
175
175
|
const dx = this.x - other.x;
|
|
176
176
|
const dy = this.y - other.y;
|
|
177
177
|
return Math.sqrt(dx * dx + dy * dy);
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
180
|
+
* 计算到另一个向量的距离平方
|
|
181
|
+
* @param other 另一个向量
|
|
182
|
+
* @returns 距离平方值
|
|
183
|
+
*/
|
|
184
184
|
distanceToSquared(other) {
|
|
185
185
|
const dx = this.x - other.x;
|
|
186
186
|
const dy = this.y - other.y;
|
|
187
187
|
return dx * dx + dy * dy;
|
|
188
188
|
}
|
|
189
189
|
/**
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
190
|
+
* 计算与另一个向量的夹角(弧度)
|
|
191
|
+
* @param other 另一个向量
|
|
192
|
+
* @returns 夹角(0到π)
|
|
193
|
+
*/
|
|
194
194
|
angleTo(other) {
|
|
195
195
|
const dot = this.dot(other);
|
|
196
196
|
const lenProduct = this.length * other.length;
|
|
@@ -199,10 +199,10 @@ export class Vector2 {
|
|
|
199
199
|
return Math.acos(Math.max(-1, Math.min(1, dot / lenProduct)));
|
|
200
200
|
}
|
|
201
201
|
/**
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
202
|
+
* 计算向量在另一个向量上的投影
|
|
203
|
+
* @param onto 投影目标向量
|
|
204
|
+
* @returns 新的投影向量
|
|
205
|
+
*/
|
|
206
206
|
projectOnto(onto) {
|
|
207
207
|
const dot = this.dot(onto);
|
|
208
208
|
const lenSq = onto.lengthSquared;
|
|
@@ -211,10 +211,10 @@ export class Vector2 {
|
|
|
211
211
|
return onto.clone().multiply(dot / lenSq);
|
|
212
212
|
}
|
|
213
213
|
/**
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
214
|
+
* 计算向量在另一个向量上的投影长度
|
|
215
|
+
* @param onto 投影目标向量
|
|
216
|
+
* @returns 投影长度(带符号)
|
|
217
|
+
*/
|
|
218
218
|
projectOntoLength(onto) {
|
|
219
219
|
const len = onto.length;
|
|
220
220
|
if (len === 0)
|
|
@@ -222,49 +222,59 @@ export class Vector2 {
|
|
|
222
222
|
return this.dot(onto) / len;
|
|
223
223
|
}
|
|
224
224
|
/**
|
|
225
|
-
*
|
|
225
|
+
* 获取垂直向量(顺时针旋转90度)
|
|
226
|
+
* Get perpendicular vector (clockwise 90 degrees)
|
|
226
227
|
* @returns 新的垂直向量
|
|
227
228
|
*/
|
|
228
229
|
perpendicular() {
|
|
229
|
-
|
|
230
|
+
// Clockwise 90° rotation: (x, y) -> (y, -x)
|
|
231
|
+
// 顺时针旋转 90°
|
|
232
|
+
return new Vector2(this.y, -this.x);
|
|
230
233
|
}
|
|
231
234
|
// 变换操作
|
|
232
235
|
/**
|
|
233
|
-
*
|
|
236
|
+
* 向量旋转(顺时针为正)
|
|
237
|
+
* Rotate vector (clockwise positive)
|
|
238
|
+
*
|
|
239
|
+
* 使用左手坐标系约定:正角度 = 顺时针旋转
|
|
240
|
+
* Uses left-hand coordinate system: positive angle = clockwise
|
|
241
|
+
*
|
|
234
242
|
* @param angle 旋转角度(弧度)
|
|
235
243
|
* @returns 当前向量实例(链式调用)
|
|
236
244
|
*/
|
|
237
245
|
rotate(angle) {
|
|
238
246
|
const cos = Math.cos(angle);
|
|
239
247
|
const sin = Math.sin(angle);
|
|
240
|
-
|
|
241
|
-
|
|
248
|
+
// Clockwise rotation: x' = x*cos + y*sin, y' = -x*sin + y*cos
|
|
249
|
+
// 顺时针旋转公式
|
|
250
|
+
const x = this.x * cos + this.y * sin;
|
|
251
|
+
const y = -this.x * sin + this.y * cos;
|
|
242
252
|
this.x = x;
|
|
243
253
|
this.y = y;
|
|
244
254
|
return this;
|
|
245
255
|
}
|
|
246
256
|
/**
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
257
|
+
* 获取旋转后的向量(不修改原向量)
|
|
258
|
+
* @param angle 旋转角度(弧度)
|
|
259
|
+
* @returns 新的旋转后向量
|
|
260
|
+
*/
|
|
251
261
|
rotated(angle) {
|
|
252
262
|
return this.clone().rotate(angle);
|
|
253
263
|
}
|
|
254
264
|
/**
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
265
|
+
* 围绕一个点旋转
|
|
266
|
+
* @param center 旋转中心点
|
|
267
|
+
* @param angle 旋转角度(弧度)
|
|
268
|
+
* @returns 当前向量实例(链式调用)
|
|
269
|
+
*/
|
|
260
270
|
rotateAround(center, angle) {
|
|
261
271
|
return this.subtract(center).rotate(angle).add(center);
|
|
262
272
|
}
|
|
263
273
|
/**
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
274
|
+
* 反射向量(关于法线)
|
|
275
|
+
* @param normal 法线向量(应为单位向量)
|
|
276
|
+
* @returns 当前向量实例(链式调用)
|
|
277
|
+
*/
|
|
268
278
|
reflect(normal) {
|
|
269
279
|
const dot = this.dot(normal);
|
|
270
280
|
this.x -= 2 * dot * normal.x;
|
|
@@ -272,30 +282,30 @@ export class Vector2 {
|
|
|
272
282
|
return this;
|
|
273
283
|
}
|
|
274
284
|
/**
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
285
|
+
* 获取反射后的向量(不修改原向量)
|
|
286
|
+
* @param normal 法线向量(应为单位向量)
|
|
287
|
+
* @returns 新的反射向量
|
|
288
|
+
*/
|
|
279
289
|
reflected(normal) {
|
|
280
290
|
return this.clone().reflect(normal);
|
|
281
291
|
}
|
|
282
292
|
// 插值和限制
|
|
283
293
|
/**
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
294
|
+
* 线性插值
|
|
295
|
+
* @param target 目标向量
|
|
296
|
+
* @param t 插值参数(0到1)
|
|
297
|
+
* @returns 当前向量实例(链式调用)
|
|
298
|
+
*/
|
|
289
299
|
lerp(target, t) {
|
|
290
300
|
this.x += (target.x - this.x) * t;
|
|
291
301
|
this.y += (target.y - this.y) * t;
|
|
292
302
|
return this;
|
|
293
303
|
}
|
|
294
304
|
/**
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
305
|
+
* 限制向量长度
|
|
306
|
+
* @param maxLength 最大长度
|
|
307
|
+
* @returns 当前向量实例(链式调用)
|
|
308
|
+
*/
|
|
299
309
|
clampLength(maxLength) {
|
|
300
310
|
const lenSq = this.lengthSquared;
|
|
301
311
|
if (lenSq > maxLength * maxLength) {
|
|
@@ -304,11 +314,11 @@ export class Vector2 {
|
|
|
304
314
|
return this;
|
|
305
315
|
}
|
|
306
316
|
/**
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
317
|
+
* 限制向量分量
|
|
318
|
+
* @param min 最小值向量
|
|
319
|
+
* @param max 最大值向量
|
|
320
|
+
* @returns 当前向量实例(链式调用)
|
|
321
|
+
*/
|
|
312
322
|
clamp(min, max) {
|
|
313
323
|
this.x = Math.max(min.x, Math.min(max.x, this.x));
|
|
314
324
|
this.y = Math.max(min.y, Math.min(max.y, this.y));
|
|
@@ -316,144 +326,144 @@ export class Vector2 {
|
|
|
316
326
|
}
|
|
317
327
|
// 比较操作
|
|
318
328
|
/**
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
329
|
+
* 检查两个向量是否相等
|
|
330
|
+
* @param other 另一个向量
|
|
331
|
+
* @param epsilon 容差,默认为Number.EPSILON
|
|
332
|
+
* @returns 是否相等
|
|
333
|
+
*/
|
|
324
334
|
equals(other, epsilon = Number.EPSILON) {
|
|
325
335
|
return Math.abs(this.x - other.x) < epsilon &&
|
|
326
336
|
Math.abs(this.y - other.y) < epsilon;
|
|
327
337
|
}
|
|
328
338
|
/**
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
339
|
+
* 检查两个向量是否完全相等
|
|
340
|
+
* @param other 另一个向量
|
|
341
|
+
* @returns 是否完全相等
|
|
342
|
+
*/
|
|
333
343
|
exactEquals(other) {
|
|
334
344
|
return this.x === other.x && this.y === other.y;
|
|
335
345
|
}
|
|
336
346
|
// 静态方法
|
|
337
347
|
/**
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
348
|
+
* 向量加法(静态方法)
|
|
349
|
+
* @param a 向量a
|
|
350
|
+
* @param b 向量b
|
|
351
|
+
* @returns 新的结果向量
|
|
352
|
+
*/
|
|
343
353
|
static add(a, b) {
|
|
344
354
|
return new Vector2(a.x + b.x, a.y + b.y);
|
|
345
355
|
}
|
|
346
356
|
/**
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
357
|
+
* 向量减法(静态方法)
|
|
358
|
+
* @param a 向量a
|
|
359
|
+
* @param b 向量b
|
|
360
|
+
* @returns 新的结果向量
|
|
361
|
+
*/
|
|
352
362
|
static subtract(a, b) {
|
|
353
363
|
return new Vector2(a.x - b.x, a.y - b.y);
|
|
354
364
|
}
|
|
355
365
|
/**
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
366
|
+
* 向量数乘(静态方法)
|
|
367
|
+
* @param vector 向量
|
|
368
|
+
* @param scalar 标量
|
|
369
|
+
* @returns 新的结果向量
|
|
370
|
+
*/
|
|
361
371
|
static multiply(vector, scalar) {
|
|
362
372
|
return new Vector2(vector.x * scalar, vector.y * scalar);
|
|
363
373
|
}
|
|
364
374
|
/**
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
375
|
+
* 向量点积(静态方法)
|
|
376
|
+
* @param a 向量a
|
|
377
|
+
* @param b 向量b
|
|
378
|
+
* @returns 点积值
|
|
379
|
+
*/
|
|
370
380
|
static dot(a, b) {
|
|
371
381
|
return a.x * b.x + a.y * b.y;
|
|
372
382
|
}
|
|
373
383
|
/**
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
384
|
+
* 向量叉积(静态方法)
|
|
385
|
+
* @param a 向量a
|
|
386
|
+
* @param b 向量b
|
|
387
|
+
* @returns 叉积值
|
|
388
|
+
*/
|
|
379
389
|
static cross(a, b) {
|
|
380
390
|
return a.x * b.y - a.y * b.x;
|
|
381
391
|
}
|
|
382
392
|
/**
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
393
|
+
* 计算两点间距离(静态方法)
|
|
394
|
+
* @param a 点a
|
|
395
|
+
* @param b 点b
|
|
396
|
+
* @returns 距离值
|
|
397
|
+
*/
|
|
388
398
|
static distance(a, b) {
|
|
389
399
|
const dx = a.x - b.x;
|
|
390
400
|
const dy = a.y - b.y;
|
|
391
401
|
return Math.sqrt(dx * dx + dy * dy);
|
|
392
402
|
}
|
|
393
403
|
/**
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
404
|
+
* 线性插值(静态方法)
|
|
405
|
+
* @param a 起始向量
|
|
406
|
+
* @param b 目标向量
|
|
407
|
+
* @param t 插值参数(0到1)
|
|
408
|
+
* @returns 新的插值结果向量
|
|
409
|
+
*/
|
|
400
410
|
static lerp(a, b, t) {
|
|
401
411
|
return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
|
|
402
412
|
}
|
|
403
413
|
/**
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
414
|
+
* 从角度创建单位向量(静态方法)
|
|
415
|
+
* @param angle 角度(弧度)
|
|
416
|
+
* @returns 新的单位向量
|
|
417
|
+
*/
|
|
408
418
|
static fromAngle(angle) {
|
|
409
419
|
return new Vector2(Math.cos(angle), Math.sin(angle));
|
|
410
420
|
}
|
|
411
421
|
/**
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
422
|
+
* 从极坐标创建向量(静态方法)
|
|
423
|
+
* @param length 长度
|
|
424
|
+
* @param angle 角度(弧度)
|
|
425
|
+
* @returns 新的向量
|
|
426
|
+
*/
|
|
417
427
|
static fromPolar(length, angle) {
|
|
418
428
|
return new Vector2(length * Math.cos(angle), length * Math.sin(angle));
|
|
419
429
|
}
|
|
420
430
|
/**
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
431
|
+
* 获取两个向量中的最小分量向量(静态方法)
|
|
432
|
+
* @param a 向量a
|
|
433
|
+
* @param b 向量b
|
|
434
|
+
* @returns 新的最小分量向量
|
|
435
|
+
*/
|
|
426
436
|
static min(a, b) {
|
|
427
437
|
return new Vector2(Math.min(a.x, b.x), Math.min(a.y, b.y));
|
|
428
438
|
}
|
|
429
439
|
/**
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
440
|
+
* 获取两个向量中的最大分量向量(静态方法)
|
|
441
|
+
* @param a 向量a
|
|
442
|
+
* @param b 向量b
|
|
443
|
+
* @returns 新的最大分量向量
|
|
444
|
+
*/
|
|
435
445
|
static max(a, b) {
|
|
436
446
|
return new Vector2(Math.max(a.x, b.x), Math.max(a.y, b.y));
|
|
437
447
|
}
|
|
438
448
|
// 字符串转换
|
|
439
449
|
/**
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
450
|
+
* 转换为字符串
|
|
451
|
+
* @returns 字符串表示
|
|
452
|
+
*/
|
|
443
453
|
toString() {
|
|
444
454
|
return `Vector2(${this.x.toFixed(3)}, ${this.y.toFixed(3)})`;
|
|
445
455
|
}
|
|
446
456
|
/**
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
457
|
+
* 转换为数组
|
|
458
|
+
* @returns [x, y] 数组
|
|
459
|
+
*/
|
|
450
460
|
toArray() {
|
|
451
461
|
return [this.x, this.y];
|
|
452
462
|
}
|
|
453
463
|
/**
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
464
|
+
* 转换为普通对象
|
|
465
|
+
* @returns {x, y} 对象
|
|
466
|
+
*/
|
|
457
467
|
toObject() {
|
|
458
468
|
return { x: this.x, y: this.y };
|
|
459
469
|
}
|