-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticleCalculator.js
More file actions
435 lines (395 loc) · 18.6 KB
/
Copy pathparticleCalculator.js
File metadata and controls
435 lines (395 loc) · 18.6 KB
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
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (c) 2024 OneNok_HK
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
// =============== 常量定義 ===============
const BETA = 0.3;
// =============== 實用小函數區域 ===============
const sharedMemoryAPI = {
getParticleGroups() {
return sharedMemory.views.particleGroups;
},
getParticleView(type) {
return sharedMemory.views.particleGroups[type];
},
getParticle(type, index) {
const view = this.getParticleView(type);
return {
x: loadAtomicFloat(view.x, index),
y: loadAtomicFloat(view.y, index),
vx: loadAtomicFloat(view.vx, index),
vy: loadAtomicFloat(view.vy, index),
id: Atomics.load(view.id, index),
type: type,
index: index,
};
},
getParticleById(id) {
let particleCumulativeCount = 0;
let t = 0;
let i = 0;
try {
for (let type = 0; type < particleTypes; type++) {
const particleCount = particleCounts[type];
if (particleCumulativeCount + particleCount > id) {
t = type;
i = id - particleCumulativeCount;
return this.getParticle(type, id - particleCumulativeCount);
}
particleCumulativeCount += particleCount;
}
} catch (error) {
console.error(`Worker ${workerId} particlesCollision failed to get particle by id ${id} type ${t} index ${i}:`, error);
}
console.warn(`Particle with id ${id} not found`);
return null;
},
updateParticle(type, index, updates) {
const view = this.getParticleView(type);
if (updates.x !== undefined) storeAtomicFloat(view.x, index, updates.x);
if (updates.y !== undefined) storeAtomicFloat(view.y, index, updates.y);
if (updates.vx !== undefined) storeAtomicFloat(view.vx, index, updates.vx);
if (updates.vy !== undefined) storeAtomicFloat(view.vy, index, updates.vy);
}
};
/**
* 一次性控制臺輸出函數,只會輸出一次指定訊息
* @param {string} key 唯一鍵值
* @param {string} message 要輸出的訊息
*/
const onceConsole = (() => {
const printed = new Set();
return (key, ...message) => {
if (!printed.has(key)) {
console.log(key, ...message);
printed.add(key);
}
};
})();
// =============== 輔助函數 ===============
// 計算粒子間作用力
function calculateForce(r, a) {
if (r < BETA) {
return r / BETA - 1;
}
else if (BETA < r && r < 1) {
return a * (1 - Math.abs(2 * r - 1 - BETA) / (1 - BETA));
}
return 0;
}
// 原子操作輔助函數
function storeAtomicFloat(array, index, value) {
return Atomics.store(array, index, Math.round(value * 1000));
}
function loadAtomicFloat(array, index) {
return Atomics.load(array, index) / 1000;
}
// =============== 全局變量 ===============
let sharedMemory = null;
let particleViews = null;
let syncView = null;
let canvas = { width: 0, height: 0 };
let workerId = -1;
let particleTypes = 0;
let particleCounts = [];
let ballRadius = 0;
let isThrough = false;
let forceMatrix = [];
let distanceMatrix = [];
let currentDt = 1/144; // 預設時間步長為 1/144 秒
let frictionFactor = 1;
let test = 0;
// =============== 消息處理 ===============
self.onmessage = function(e) {
switch (e.data.type) {
case 'initSharedMemory':
try {
if (!e.data.sharedMemory || e.data.workerId === undefined || e.data.particleTypes === undefined || e.data.particleCounts === undefined) {
throw new Error('Missing required initialization data\nmissing: ' + (!e.data.sharedMemory ? 'sharedMemory: ' + e.data.sharedMemory: '') + (!e.data.workerId ? 'workerId: ' + e.data.workerId : '') + (!e.data.particleTypes ? 'particleTypes: ' + e.data.particleTypes : '') + (!e.data.particleCounts ? 'particleCounts: ' + e.data.particleCounts : ''));
}
// 初始化基本參數
workerId = e.data.workerId;
console.log('Worker ' + workerId + ': received Initializing...', performance.now());
particleTypes = e.data.particleTypes;
particleCounts = e.data.particleCounts;
ballRadius = e.data.ballRadius;
canvas.width = e.data.canvas.width;
canvas.height = e.data.canvas.height;
// 設置共享內存
sharedMemory = e.data.sharedMemory;
particleViews = sharedMemory.views.particleGroups;
syncView = sharedMemory.views.sync;
console.log('Worker ' + workerId + ': initialized', performance.now());
self.postMessage({ type: 'initComplete', status: 'success' });
} catch (error) {
console.error('Worker ' + workerId + ' initialization failed:', error);
self.postMessage({ type: 'initComplete', status: 'failed', error: error.message });
}
break;
case 'calculate_Force':
onceConsole('calculate_ForceOnMessage', `Worker ${workerId} received calculate_Force message ${performance.now()}`);
try {
const { startId, endId, forceMatrix: newForceMatrix, distanceMatrix: newDistanceMatrix, isThrough: newIsThrough, currentDt: newCurrentDt, frictionFactor: newFrictionFactor } = e.data;
canvas.width = e.data.canvasWidth;
canvas.height = e.data.canvasHeight;
// 更新計算參數
forceMatrix = newForceMatrix;
distanceMatrix = newDistanceMatrix;
isThrough = newIsThrough;
currentDt = newCurrentDt;
frictionFactor = newFrictionFactor;
// 執行直接計算
onceConsole('calculate_Force', `Worker ${workerId} calculating forces for particle type from id ${startId} to ${endId} at ${performance.now()}`);
const { calcCount, skippedCount } = calculate_ForceForce(startId, endId);
//console.debug("calculate_ForceComplete");
self.postMessage({
type: 'calculate_ForceComplete',
calcCount,
skippedCount
});
} catch (error) {
console.error(`Worker ${workerId} calculate_Force failed:`, error);
self.postMessage({ type: 'error', message: error.message });
}
break;
case 'positionUpdate':
onceConsole('positionUpdateOnMessage', `Worker ${workerId} received positionUpdate message ${performance.now()}`);
canvas.width = e.data.canvasWidth;
canvas.height = e.data.canvasHeight;
try {
const { startId, endId, currentDt, frictionFactor, isThrough } = e.data;
updateParticlePositions(startId, endId, currentDt, frictionFactor, isThrough);
onceConsole('positionUpdated', `Worker ${workerId} updated positions for particle type from id ${startId} to ${endId} at ${performance.now()}`);
self.postMessage({ type: 'positionUpdateComplete' });
} catch (error) {
console.error(`Worker ${workerId} positionUpdate failed:`, error);
self.postMessage({ type: 'error', message: error.message });
}
break;
case 'particlesCollision':
onceConsole('particlesCollisionOnMessage', `Worker ${workerId} received particlesCollision message ${performance.now()}`);
try {
canvas.width = e.data.canvasWidth;
canvas.height = e.data.canvasHeight;
const { startId, endId, isThrough, frictionFactor, restitution } = e.data;
const { particlesList, particleCollisionCountsTimes } = particlesCollision(startId, endId, frictionFactor, isThrough, restitution);
onceConsole('particlesCollisionComplete', `Worker ${workerId} completed particlesCollision for particle type from id ${startId} to ${endId} at ${performance.now()}`);
self.postMessage({ type: 'particlesCollisionComplete', particlesList, particleCollisionCountsTimes });
} catch (error) {
console.error(`Worker ${workerId} particlesCollision failed:`, error);
self.postMessage({ type: 'error', message: error.message });
}
break;
}
};
// =============== 計算函數 ===============
function calculate_ForceForce(startId, endId) {
onceConsole('calculate_ForceForce', `Worker ${workerId} calculating forces at ${performance.now()}`);
let calcCount = 0;
let skippedCount = 0;
onceConsole('calculate_ForceForce_check', `Worker ${workerId} calculating forces for particle from id ${startId} to ${endId}\n particleViews is ${particleViews}, forceMatrix is ${forceMatrix}, distanceMatrix is ${distanceMatrix}`);
// 檢查參數有效性
for (let id = startId; id < endId; id++) {
let totalForceX = 0;
let totalForceY = 0;
let p = sharedMemoryAPI.getParticleById(id);
for (let type2 = 0; type2 < particleTypes; type2++) {
calcCount+=particleCounts[type2];
const view2 = particleViews[type2];
const force = forceMatrix[p.type][type2];
const maxDistance = distanceMatrix[p.type][type2];
if (maxDistance === 0) {
skippedCount++;
continue;
}
const maxDistSquared = maxDistance * maxDistance;
let fx = 0, fy = 0;
for (let j = 0; j < particleCounts[type2]; j++) {
if (p.type === type2 && p.index === j) continue;
let dx = loadAtomicFloat(view2.x, j) - p.x;
let dy = loadAtomicFloat(view2.y, j) - p.y;
if (isThrough) {
if (Math.abs(dx) > canvas.width / 2) {
dx -= Math.sign(dx) * canvas.width;
}
if (Math.abs(dy) > canvas.height / 2) {
dy -= Math.sign(dy) * canvas.height;
}
}
const distSquared = dx * dx + dy * dy;
if (distSquared >= maxDistSquared) {
skippedCount++;
continue;
}
else if (distSquared === 0) {
fx += Math.random() * 0.01 - 0.005;
fy += Math.random() * 0.01 - 0.005; // 隨機小擾動
}
else {
const distance = Math.sqrt(distSquared);
const normalizedDist = distance / maxDistance;
const forceMagnitude = calculateForce(normalizedDist, force);
if (forceMagnitude === 0) {
skippedCount++;
continue;
}
fx += forceMagnitude * dx / distance;
fy += forceMagnitude * dy / distance;
}
}
totalForceX += fx * 10 * maxDistance;
totalForceY += fy * 10 * maxDistance;
}
// 更新速度
storeAtomicFloat(particleViews[p.type].vx, p.index, p.vx * frictionFactor + totalForceX * currentDt);
storeAtomicFloat(particleViews[p.type].vy, p.index, p.vy * frictionFactor + totalForceY * currentDt);
onceConsole('calculate_Force_totalForce', `Worker ${workerId} updated particle ${id} of type ${p.type} with forces (${p.vx + totalForceX * currentDt}, ${p.vy + totalForceY * currentDt}) at ${performance.now()}`);
onceConsole('calculate_Force_totalForce_check', `Worker ${workerId} particle ${id} of type ${p.type} has total forces (${loadAtomicFloat(particleViews[p.type].vx, p.index)}, ${loadAtomicFloat(particleViews[p.type].vy, p.index)}) at ${performance.now()}`);
}
return { calcCount, skippedCount };
}
function updateParticlePositions(startId, endId, dt, frictionFactor, isThrough) {
onceConsole('updateParticlePositions_start', `UPDATEING positions Worker ${workerId} from id ${startId} to ${endId} at ${performance.now()}`);
for (let id = startId; id < endId; id++) {
// 讀取當前位置和速度
let p = sharedMemoryAPI.getParticleById(id);
p.vx *= frictionFactor;
p.vy *= frictionFactor;
onceConsole('updateParticlePositions_read', `Worker ${workerId} read particle ${id} of type ${p.type} with position (${p.x}, ${p.y}) and velocity (${p.vx}, ${p.vy}) at ${performance.now()}`);
// 更新位置
p.x += p.vx * dt;
p.y += p.vy * dt;
// 邊界處理
if (isThrough) {
p.x = ((p.x % canvas.width) + canvas.width) % canvas.width;
p.y = ((p.y % canvas.height) + canvas.height) % canvas.height;
} else {
if (p.x < 0) {
p.x = 0;
p.vx = Math.abs(p.vx);
} else if (p.x > canvas.width) {
p.x = canvas.width;
p.vx = -Math.abs(p.vx);
}
if (p.y < 0) {
p.y = 0;
p.vy = Math.abs(p.vy);
} else if (p.y > canvas.height) {
p.y = canvas.height;
p.vy = -Math.abs(p.vy);
}
}
// 存儲更新後的位置和速度
sharedMemoryAPI.updateParticle(p.type, p.index, p);
}
}
function particlesCollision(startId, endId, frictionFactor, isThrough, restitution) {
let particleCollisionCountsTimes = 0;
let particlesList = [];
onceConsole('particlesCollision_start', `particlesCollision Worker ${workerId} from id ${startId} to ${endId} at ${performance.now()}`);
for (let id = startId; id < endId; id++) {
// 讀取當前位置和速度
try{
let p = sharedMemoryAPI.getParticleById(id);
}catch (error) {
console.error(`Worker ${workerId} particlesCollision failed to get particle by id ${id}:`, error);
continue; // 如果讀取失敗,則跳過此粒子
}
// >>> 粒子類型循環 <<<
for (let type2 = 0; type2 < particleTypes; type2++) {
// --避免自我碰撞--
// --第二組粒子循環--
let totalx = 0;
let totaly = 0;
particleCollisionCountsTimes += particleCounts[type2];
for (let j = 0; j < particleCounts[type2]; j++) {
if (p.type === type2 && p.index === j) continue; // 跳過自身
const p2 = sharedMemoryAPI.getParticle(type2, j);
// >>> 距離計算 <<<
// --基本距離--
let dx = p2.x - p.x;
let dy = p2.y - p.y;
// --邊界穿越處理--
if (isThrough) {
if (Math.abs(dx) > canvas.width / 2) {
dx = dx - Math.sign(dx) * canvas.width;
}
if (Math.abs(dy) > canvas.height / 2) {
dy = dy - Math.sign(dy) * canvas.height;
}
}
if (Math.abs(dx) >= ballRadius || Math.abs(dy) >= ballRadius) {
continue; // 如果距離大於半徑,則跳過
}
// --碰撞檢測--
const distSquared = dx * dx + dy * dy;
const minDist = 2 * ballRadius;
// >>> 碰撞處理 <<<
if (distSquared < minDist * minDist) {
const dist = Math.sqrt(distSquared);
// --碰撞軸計算--
const nx = dx / dist;
const ny = dy / dist;
// --切向向量計算--
const tx = -ny;
const ty = nx;
// --相對速度計算--
const dvx = p2.vx - p.vx;
const dvy = p2.vy - p.vy;
// --速度投影--
const normalVelocity = dvx * nx + dvy * ny;
const tangentVelocity = dvx * tx + dvy * ty;
// >>> 碰撞響應 <<<
if (normalVelocity >= 0) {
continue; // 如果法向速度為正,則不處理碰撞
}
particleCollisionCountsTimes++; // 粒子碰撞次數
// --衝量計算--
const jn = -(1 + restitution) * normalVelocity / 2;
const jt = -tangentVelocity * frictionFactor / 2;
// --速度更新--
p.vx -= (jn * nx + jt * tx);
p.vy -= (jn * ny + jt * ty);
// --重疊修正--
const overlap = minDist - dist;
if (overlap > 0) {
const correction = (overlap / 2) * 1.05;
totalx -= nx * correction;
totaly -= ny * correction;
}
}
}
if (totalx === 0 && totaly === 0) {
continue; // 如果沒有碰撞,則跳過
}
// >>> 更新粒子位置 <<<
p.x += totalx;
p.y += totaly;
// >>> 邊界檢查和修正 <<<
// --第一個粒子--
if (isThrough) {
// --環繞處理--
p.x = ((p.x % canvas.width) + canvas.width) % canvas.width;
p.y = ((p.y % canvas.height) + canvas.height) % canvas.height;
} else {
// --邊界彈回--
if (p.x < ballRadius) {
p.x = ballRadius;
p.vx = Math.abs(p.vx);
} else if (p.x > canvas.width - ballRadius) {
p.x = canvas.width - ballRadius;
p.vx = -Math.abs(p.vx);
}
if (p.y < ballRadius) {
p.y = ballRadius;
p.vy = Math.abs(p.vy);
} else if (p.y > canvas.height - ballRadius) {
p.y = canvas.height - ballRadius;
p.vy = -Math.abs(p.vy);
}
}
particlesList.push(p)
}
}
return {particlesList, particleCollisionCountsTimes}
}