2020-05-04 15:17:55 +00:00
|
|
|
|
import Vector from './vector.mjs';
|
|
|
|
|
|
|
|
|
|
const boidShape = [
|
|
|
|
|
new Vector(2, 0),
|
|
|
|
|
new Vector(-1, 1),
|
|
|
|
|
new Vector(-1, -1),
|
|
|
|
|
new Vector(2, 0),
|
|
|
|
|
];
|
|
|
|
|
|
2020-05-04 16:17:25 +00:00
|
|
|
|
const boidShapeSize = boidShape.length;
|
|
|
|
|
const transformed = new Vector(0, 0);
|
|
|
|
|
|
2020-05-04 15:17:55 +00:00
|
|
|
|
export const boids = (activeBoids, params, ctx, width, height, center) =>
|
|
|
|
|
{
|
2020-05-04 16:17:25 +00:00
|
|
|
|
const boidsSize = activeBoids.length;
|
|
|
|
|
|
2020-05-04 15:17:55 +00:00
|
|
|
|
ctx.clearRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
// // Draw each boid’s visibility and proximity zones
|
|
|
|
|
// ctx.beginPath();
|
|
|
|
|
// ctx.fillStyle = '#BBEE94';
|
|
|
|
|
|
|
|
|
|
// for (let boid of activeBoids)
|
|
|
|
|
// {
|
|
|
|
|
// const trans = boid.pos.add(center);
|
|
|
|
|
|
|
|
|
|
// ctx.moveTo(
|
|
|
|
|
// trans.x + params.visibleDist,
|
|
|
|
|
// trans.y
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// ctx.arc(
|
|
|
|
|
// trans.x, trans.y,
|
|
|
|
|
// params.visibleDist,
|
|
|
|
|
// 0, 2 * Math.PI
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ctx.fill();
|
|
|
|
|
// ctx.stroke();
|
|
|
|
|
|
|
|
|
|
// ctx.beginPath();
|
|
|
|
|
// ctx.fillStyle = '#EE9495';
|
|
|
|
|
|
|
|
|
|
// for (let boid of activeBoids)
|
|
|
|
|
// {
|
|
|
|
|
// const trans = boid.pos.add(center);
|
|
|
|
|
|
|
|
|
|
// ctx.moveTo(
|
|
|
|
|
// trans.x + params.closeDist,
|
|
|
|
|
// trans.y
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// ctx.arc(
|
|
|
|
|
// trans.x, trans.y,
|
|
|
|
|
// params.closeDist,
|
|
|
|
|
// 0, 2 * Math.PI
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ctx.fill();
|
|
|
|
|
// ctx.stroke();
|
|
|
|
|
|
|
|
|
|
// // Draw each boid’s velocity vector
|
|
|
|
|
// ctx.beginPath();
|
|
|
|
|
// ctx.strokeStyle = 'rgba(0, 0, 0, 0.5)';
|
|
|
|
|
|
|
|
|
|
// for (let boid of activeBoids)
|
|
|
|
|
// {
|
|
|
|
|
// const start = boid.pos.add(center);
|
|
|
|
|
// const end = start.add(boid.vel.mul(0.25));
|
|
|
|
|
|
|
|
|
|
// ctx.moveTo(start.x, start.y);
|
|
|
|
|
// ctx.lineTo(end.x, end.y);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ctx.stroke();
|
|
|
|
|
|
|
|
|
|
// Draw each boid’s head following the angle of its course
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.fillStyle = 'black';
|
|
|
|
|
|
2020-05-04 16:17:25 +00:00
|
|
|
|
for (let i = 0; i < boidsSize; ++i)
|
2020-05-04 15:17:55 +00:00
|
|
|
|
{
|
2020-05-04 16:17:25 +00:00
|
|
|
|
const boid = activeBoids[i];
|
2020-05-04 15:17:55 +00:00
|
|
|
|
const angle = boid.vel.angle();
|
|
|
|
|
let isFirst = true;
|
|
|
|
|
|
2020-05-04 16:17:25 +00:00
|
|
|
|
for (let j = 0; j < boidShapeSize; ++j)
|
2020-05-04 15:17:55 +00:00
|
|
|
|
{
|
2020-05-04 16:17:25 +00:00
|
|
|
|
transformed.x = boidShape[j].x;
|
|
|
|
|
transformed.y = boidShape[j].y;
|
|
|
|
|
|
|
|
|
|
transformed
|
|
|
|
|
.rotate(angle)
|
|
|
|
|
.mul(params.radius)
|
|
|
|
|
.add(center)
|
|
|
|
|
.add(boid.pos);
|
2020-05-04 15:17:55 +00:00
|
|
|
|
|
|
|
|
|
if (isFirst)
|
|
|
|
|
{
|
2020-05-04 16:17:25 +00:00
|
|
|
|
ctx.moveTo(transformed.x, transformed.y);
|
2020-05-04 15:17:55 +00:00
|
|
|
|
isFirst = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-04 16:17:25 +00:00
|
|
|
|
ctx.lineTo(transformed.x, transformed.y);
|
2020-05-04 15:17:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.fill();
|
|
|
|
|
};
|