update = (ctx, particles, fieldPerGridCell) => {
let points = [];
let noiseZ = 0;
const doReset = () => {
reset;
poseNet.removeAllListeners("pose");
};
doReset();
poseNet.on("pose", function (poses) {
if (poses === undefined) return;
points = [];
if (isSkelton) {
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, width, height);
ctx.fill();
}
poses[0].pose.keypoints
.filter((kp) => kp.score >= 0.2)
.map((kp) => {
if (isSkelton) {
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.fillStyle = "white";
ctx.arc(kp.position.x, kp.position.y, 3, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
}
if (kp.part === "leftElbow" || kp.part === "leftWrist") {
kp.positive = true;
points.push(kp);
} else if (kp.part === "rightElbow" || kp.part === "rightWrist") {
kp.positive = false;
points.push(kp);
}
});
// Draw skelton
if (isSkelton) {
const skeleton = poses[0].skeleton;
if (skeleton.length > 0) {
skeleton.map((sk) => {
ctx.beginPath();
ctx.moveTo(sk[0].position.x, sk[0].position.y);
ctx.lineTo(sk[1].position.x, sk[1].position.y);
ctx.stroke();
});
}
}
// Give force to grid
fieldPerGridCell = gridCellFactory({
cellValue: (col, row) => {
let x = col * resolution;
let y = row * resolution;
return fieldDirectionSampler(x, y, points, noiseZ);
}
});
if (isSkelton) {
for (let x = 0; x < fieldPerGridCell.length; x++) {
for (let y = 0; y < fieldPerGridCell[0].length; y++) {
const xPos = fieldPerGridCell[x][y].x;
const yPos = fieldPerGridCell[x][y].y;
ctx.beginPath();
ctx.moveTo(xPos, yPos);
ctx.lineTo(
xPos + fieldPerGridCell[x][y].vector.x * resolution * 2,
yPos + fieldPerGridCell[x][y].vector.y * resolution * 2
);
ctx.strokeStyle = "white";
ctx.stroke();
}
}
}
// Update the particles position
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
const nearestFieldPoint = getNearestFieldPoint(
particle.position.x,
particle.position.y,
fieldPerGridCell
);
particle.acceleration.add(nearestFieldPoint.vector);
particle.velocity.add(particle.acceleration);
particle.acceleration.mult(0);
// gridLayer(fieldG, flattenedArr);
// // Give force to the particles that are within a certain range from the body points
// points.map((p) => {
// const d = particle.position.dist(p.position);
// const angle =
// n.simplex3(p.position.x / 20, p.position.y / 20, noiseZ) *
// Math.PI *
// 2 +
// Math.PI / 2;
// const length = Math.abs(
// n.simplex3(
// p.position.x / 40 + 40000,
// p.position.y / 40 + 40000,
// noiseZ
// )
// );
// const acc = new Vector(0, length);
// acc.setAngle(angle);
// if (d <= range) {
// particle.acceleration.add(acc);
// particle.velocity.add(particle.acceleration);
// particle.acceleration.mult(0);
// }
// });
}
noiseZ += 0.002;
});
}