Skip to content

Commit

Permalink
Updates to ant
Browse files Browse the repository at this point in the history
  • Loading branch information
hasnainroopawalla committed Sep 2, 2023
1 parent 769e516 commit 5f6c898
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 36 deletions.
30 changes: 14 additions & 16 deletions src/aco/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ export class Ant {
}

private approachTarget(target: Vector): void {
// TODO: modify
const bearing = new Vector(
target.x - this.position.x,
target.y - this.position.y
);
this.desiredVelocity = bearing.normalize();
this.desiredVelocity = target.sub(this.position).normalize();
}

private getAntennas(): IAntennas {
// TODO: remove magic numbers
const leftAntenna = this.position.add(
this.desiredVelocity.rotate(-2.35 / 2).mult(90)
this.desiredVelocity
.rotate(-config.antAntennaRotation)
.mult(config.antAntennaRange)
);
const frontAntenna = this.position.add(
this.desiredVelocity.mult(config.antAntennaRange)
);
const frontAntenna = this.position.add(this.desiredVelocity.mult(90));
const rightAntenna = this.position.add(
this.desiredVelocity.rotate(2.35 / 2).mult(90)
this.desiredVelocity
.rotate(config.antAntennaRotation)
.mult(config.antAntennaRange)
);

config.showAntAntennas &&
Expand Down Expand Up @@ -130,20 +130,19 @@ export class Ant {
);
}

if (this.targetFoodItem) {
if (!this.targetFoodItem) {
// follow food pheromones if no food item is found within perception range
this.handleAntennaSteering(IPheromoneType.Food);
} else {
// check if reserved food item is picked up
if (this.targetFoodItem.collide(this.position)) {
// rotate 180 degrees
this.desiredVelocity.rotate(Math.PI, true);
this.targetFoodItem.pickedUp();
this.returningHome();
} else {
// TODO: improve if-else condition
this.approachTarget(this.targetFoodItem.position);
}
} else {
// follow food pheromones if no food item is found within perception range
this.handleAntennaSteering(IPheromoneType.Food);
}
}

Expand Down Expand Up @@ -208,7 +207,6 @@ export class Ant {
this.position.add(this.velocity.mult(config.antMaxSpeed), true);
}

// TODO: Create a wrapper for render methods to handle push/pop logic
private renderAnt() {
this.p.push();
this.p.strokeWeight(config.antStrokeWeight);
Expand Down
4 changes: 4 additions & 0 deletions src/aco/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const config = {
frameRate: 60,
// world
worldBackground: "#78624f",
foodClusterSize: 7,
// food item
foodItemSize: 4, // diameter
foodItemColor: "#39FF14",
Expand All @@ -14,6 +15,7 @@ export const config = {
colonyColor: "#ffffff",
colonyStrokeWeight: 1,
colonyTextSize: 20,
// ant
antWanderStrength: 0.2,
antMaxSpeed: 2.5,
antSteeringLimit: 0.1,
Expand All @@ -27,6 +29,8 @@ export const config = {
antPerceptionStrokeWeight: 1,
showAntAntennas: false,
antAntennaRadius: 70,
antAntennaRange: 90,
antAntennaRotation: 1.1,
antObstacleAngleRange: 0.7,
// pheromone
pheromoneSize: 4,
Expand Down
13 changes: 0 additions & 13 deletions src/aco/obstacle.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/aco/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { IConfig } from "./sketch.interface";
let world: World;
let canvasInteractionEnabled = true;
const numAnts: number = 200;
const foodClusterSize: number = 7;

export const updateAcoConfig = <T extends keyof IConfig>(
param: T,
Expand All @@ -23,9 +22,9 @@ export const sketch = (p: p5) => {
for (let i = 0; i < numAnts; i++) {
world.createAnt();
}
world.createFoodCluster(250, 250, foodClusterSize);
world.createFoodCluster(1000, 150, foodClusterSize);
world.createFoodCluster(1020, 550, foodClusterSize);
world.createFoodCluster(250, 250, config.foodClusterSize);
world.createFoodCluster(1000, 150, config.foodClusterSize);
world.createFoodCluster(1020, 550, config.foodClusterSize);
};

p.draw = () => {
Expand All @@ -36,6 +35,6 @@ export const sketch = (p: p5) => {
if (!canvasInteractionEnabled) {
return;
}
world.createFoodCluster(p.mouseX, p.mouseY, foodClusterSize);
world.createFoodCluster(p.mouseX, p.mouseY, config.foodClusterSize);
};
};
1 change: 0 additions & 1 deletion src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const App = () => {
updateAcoConfig={updateAcoConfig}
/>
<Sketch />
{/* Add footer for GitHub source */}
</>
);
};
1 change: 0 additions & 1 deletion src/components/control-panel/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const Checkbox = (props: ICheckboxProps) => {
setValue(newValue);
};

// TODO: Change checked color to blue
return (
<input
type="checkbox"
Expand Down

0 comments on commit 5f6c898

Please sign in to comment.