Skip to content

Commit b2513df

Browse files
committed
feat: Improve Collision performance
1 parent ae14d0d commit b2513df

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

build/proton.js

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/proton.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/proton.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proton-engine",
3-
"version": "5.4.1",
3+
"version": "5.4.3",
44
"description": "Proton is a simple and powerful javascript particle animation engine.",
55
"keywords": [
66
"particle",

src/behaviour/Collision.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ export default class Collision extends Behaviour {
2929
*/
3030
constructor(emitter, mass, callback, life, easing) {
3131
super(life, easing);
32-
3332
this.reset(emitter, mass, callback);
33+
this.newPool = [];
34+
this.pool = [];
3435
this.name = "Collision";
3536
}
3637

@@ -72,9 +73,13 @@ export default class Collision extends Behaviour {
7273
* @param {Int} index the particle index
7374
*/
7475
applyBehaviour(particle, time, index) {
75-
const newPool = this.emitter ? this.emitter.particles.slice(index) : this.pool.slice(index);
76-
const length = newPool.length;
76+
if (this.emitter) {
77+
Util.sliceArray(this.emitter.particles, index, this.newPool);
78+
} else {
79+
Util.sliceArray(this.pool, index, this.newPool);
80+
}
7781

82+
const length = this.newPool.length;
7883
let otherParticle;
7984
let lengthSq;
8085
let overlap;
@@ -83,7 +88,7 @@ export default class Collision extends Behaviour {
8388
let i;
8489

8590
for (i = 0; i < length; i++) {
86-
otherParticle = newPool[i];
91+
otherParticle = this.newPool[i];
8792

8893
if (otherParticle !== particle) {
8994
this.delta.copy(otherParticle.p);

src/utils/Util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export default {
4545
return this.isArray(arr) ? arr : [arr];
4646
},
4747

48+
sliceArray(arr1, index, arr2) {
49+
this.emptyArray(arr2);
50+
for (let i = index; i < arr1.length; i++) {
51+
arr2.push(arr1[i]);
52+
}
53+
},
54+
4855
getRandFromArray(arr) {
4956
if (!arr) return null;
5057
return arr[Math.floor(arr.length * Math.random())];

0 commit comments

Comments
 (0)