Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
optimized collision
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmusmerzin committed Apr 22, 2020
1 parent 46e8742 commit 8584e8b
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/Ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import {
} from './Trig';


interface Push {
posDiff: [number, number],
distance: number
}


export default class Ant {
posX: number;
posY: number;
Expand Down Expand Up @@ -67,18 +73,18 @@ export default class Ant {
this.distancingFactor = options.distancingFactor;
}

getNeighboursInRange(neighbours: (Ant | { pos: [number, number] })[]): (Ant | { pos: [number, number] })[] {
return neighbours.filter(ant => {
getPushes(neighbours: (Ant | { pos: [number, number] })[]): Push[] {
return neighbours.map((ant): Push | null => {
if (ant !== this) {
const posDiff = getVecDiffInLoopingGrid(this.pos, ant.pos, [window.innerWidth, window.innerHeight]);
return (
posDiff[0] <= this.distancingRange &&
posDiff[1] <= this.distancingRange &&
// avoid calculating the hypotenuse(magnitude) if not necessary
getVecMagnitude(posDiff) <= this.distancingRange
);
} else return false;
});
// avoid calculating the hypotenuse(magnitude) if not necessary
if (posDiff[0] <= this.distancingRange && posDiff[1] <= this.distancingRange) {
const distance = getVecMagnitude(posDiff);
if (distance <= this.distancingRange) return { posDiff, distance };
else return null;
} else return null;
} else return null;
}).filter(push => push !== null) as Push[];
}

calcPush(distance: number) {
Expand All @@ -94,17 +100,15 @@ export default class Ant {
};

updateVelocity(neighbours: (Ant | { pos: [number, number] })[]): Ant {
neighbours = this.getNeighboursInRange(neighbours);
if (neighbours.length > 0) {
const pushes = this.getPushes(neighbours);
if (pushes.length > 0) {
const vel: [number, number] = [0, 0];
neighbours.map(ant => getVecDiffInLoopingGrid(this.pos, ant.pos, [window.innerWidth, window.innerHeight]))
.forEach(posDiff => {
const push = this.calcPush(getVecMagnitude(posDiff));
const pushAngle = vecToAng(posDiff) +180;
const pushVector = angToVec(pushAngle, push);
vel[0] += pushVector[0];
vel[1] += pushVector[1];
});
pushes.forEach(push => {
const pushAngle = vecToAng(push.posDiff) +Math.PI;
const pushVector = angToVec(pushAngle, this.calcPush(push.distance));
vel[0] += pushVector[0];
vel[1] += pushVector[1];
});
this.vel = vel;
} else {
let angle = this.angle;
Expand Down

0 comments on commit 8584e8b

Please sign in to comment.