I made a function called CheckForTarget()
to check the closest target to an object. I used the p5.js
function dist()
to calculate the distance but the answer to this calculation results as NaN
.
checkForTarget() {
let closest;
let index = 0;
for (let i = 0; i < food_amount; i++) {
let d = dist(this.x, this.y, food[i].pos.x, food[i].pos.y);
console.log(d);
if (d < this.sense && d < closest) {
closest = d;
index = i;
}
}
}
Expected result: a certain distance between 0 and the radius
Actual result: NaN
Added:
class Dot {
constructor() {
this.pos = createVector(width / 2, height / 2);
this.vel = createVector();
this.acc = createVector();