Ok, I'm making an evolution simulator and they need to get food to survive. Blah blah blah. But this splice isn't working and I don't know how to fix it
The code:
function track(blob, ob) {
for (let i = 0; i < ob.length; i++) {
const dist = Math.hypot(
blob.x - ob[i].x,
blob.y - ob[i].y
)
if (dist - 20 - blob.size < 1) {
blob.food++
ob.splice(ob[i], 1)
} else {
const angle = Math.atan2(
ob[i].x - blob.y,
ob[i].y - blob.x
)
const velocity = {
x: Math.cos(angle) / 2,
y: Math.sin(angle) / 2
}
blob.velocity = velocity
}
}
}
The way .splice() works is you specify the starting index, the number of items to delete, and a list of items to insert at that index.
var array = [3, 4, 5, 6];
array.splice(1, 1);
console.log(array); //[3, 5, 6]
array.splice(2, 0, 7);
console.log(array); //[3, 5, 7, 6]
array.splice(0, 2, 1, 8);
console.log(array); //[1, 8, 5, 7, 6]