Skip to content

Commit

Permalink
feat: Improve Collision performance
Browse files Browse the repository at this point in the history
  • Loading branch information
drawcall committed Apr 27, 2022
1 parent ae14d0d commit b2513df
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
26 changes: 20 additions & 6 deletions build/proton.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/proton.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/proton.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proton-engine",
"version": "5.4.1",
"version": "5.4.3",
"description": "Proton is a simple and powerful javascript particle animation engine.",
"keywords": [
"particle",
Expand Down
13 changes: 9 additions & 4 deletions src/behaviour/Collision.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export default class Collision extends Behaviour {
*/
constructor(emitter, mass, callback, life, easing) {
super(life, easing);

this.reset(emitter, mass, callback);
this.newPool = [];
this.pool = [];
this.name = "Collision";
}

Expand Down Expand Up @@ -72,9 +73,13 @@ export default class Collision extends Behaviour {
* @param {Int} index the particle index
*/
applyBehaviour(particle, time, index) {
const newPool = this.emitter ? this.emitter.particles.slice(index) : this.pool.slice(index);
const length = newPool.length;
if (this.emitter) {
Util.sliceArray(this.emitter.particles, index, this.newPool);
} else {
Util.sliceArray(this.pool, index, this.newPool);
}

const length = this.newPool.length;
let otherParticle;
let lengthSq;
let overlap;
Expand All @@ -83,7 +88,7 @@ export default class Collision extends Behaviour {
let i;

for (i = 0; i < length; i++) {
otherParticle = newPool[i];
otherParticle = this.newPool[i];

if (otherParticle !== particle) {
this.delta.copy(otherParticle.p);
Expand Down
7 changes: 7 additions & 0 deletions src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export default {
return this.isArray(arr) ? arr : [arr];
},

sliceArray(arr1, index, arr2) {
this.emptyArray(arr2);
for (let i = index; i < arr1.length; i++) {
arr2.push(arr1[i]);
}
},

getRandFromArray(arr) {
if (!arr) return null;
return arr[Math.floor(arr.length * Math.random())];
Expand Down

0 comments on commit b2513df

Please sign in to comment.