Integrate obstacles in repel force
This commit is contained in:
parent
639c56333e
commit
9ef76536ca
34
boids.mjs
34
boids.mjs
|
@ -79,11 +79,9 @@ class Boids
|
||||||
|
|
||||||
// List of active simulated boids
|
// List of active simulated boids
|
||||||
this.boids = [];
|
this.boids = [];
|
||||||
this.boidsLength = 0;
|
|
||||||
|
|
||||||
// List of obstacles
|
// List of obstacles
|
||||||
this.obstacles = obstacles;
|
this.obstacles = obstacles;
|
||||||
this.obstaclesLength = this.obstacles.length;
|
|
||||||
|
|
||||||
// Vector registers used for holding temporary values
|
// Vector registers used for holding temporary values
|
||||||
this.registers = [
|
this.registers = [
|
||||||
|
@ -122,7 +120,6 @@ class Boids
|
||||||
add(center)
|
add(center)
|
||||||
{
|
{
|
||||||
this.boids.push(new Boid(this.params, center));
|
this.boids.push(new Boid(this.params, center));
|
||||||
this.boidsLength += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Start the simulation. */
|
/** Start the simulation. */
|
||||||
|
@ -174,7 +171,8 @@ class Boids
|
||||||
*/
|
*/
|
||||||
_update(delta)
|
_update(delta)
|
||||||
{
|
{
|
||||||
const boidsLength = this.boidsLength;
|
const boidsLength = this.boids.length;
|
||||||
|
const obstaclesLength = this.obstacles.length;
|
||||||
|
|
||||||
for (let i = 0; i < boidsLength; ++i)
|
for (let i = 0; i < boidsLength; ++i)
|
||||||
{
|
{
|
||||||
|
@ -187,7 +185,7 @@ class Boids
|
||||||
let visibles = 0;
|
let visibles = 0;
|
||||||
|
|
||||||
// Compute mean flock position and velocity and compute
|
// Compute mean flock position and velocity and compute
|
||||||
// the repel force
|
// the repel force from other boids
|
||||||
for (let j = 0; j < boidsLength; ++j)
|
for (let j = 0; j < boidsLength; ++j)
|
||||||
{
|
{
|
||||||
if (i != j)
|
if (i != j)
|
||||||
|
@ -211,6 +209,17 @@ class Boids
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute the repel force from obstacles
|
||||||
|
for (let j = 0; j < obstaclesLength; ++j)
|
||||||
|
{
|
||||||
|
const obstacle = this.obstacles[j];
|
||||||
|
|
||||||
|
if (obstacle.distance(me.pos) < this.params.closeDist)
|
||||||
|
{
|
||||||
|
me.vel.add(me.pos).sub(obstacle.center);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Attract towards center of visible flock
|
// Attract towards center of visible flock
|
||||||
if (visibles >= 1)
|
if (visibles >= 1)
|
||||||
{
|
{
|
||||||
|
@ -239,17 +248,6 @@ class Boids
|
||||||
.mul(this.params.matchAccel));
|
.mul(this.params.matchAccel));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid obstacles
|
|
||||||
for (let j = 0; j < this.obstaclesLength; ++j)
|
|
||||||
{
|
|
||||||
const obstacle = this.obstacles[j];
|
|
||||||
|
|
||||||
if (obstacle.intersect(me.pos, this.params.radius))
|
|
||||||
{
|
|
||||||
me.vel.sub(obstacle.center).add(me.pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not surpass maximum speed
|
// Do not surpass maximum speed
|
||||||
const speed = me.vel.normSquared();
|
const speed = me.vel.normSquared();
|
||||||
|
|
||||||
|
@ -269,8 +267,8 @@ class Boids
|
||||||
*/
|
*/
|
||||||
_draw()
|
_draw()
|
||||||
{
|
{
|
||||||
const boidsLength = this.boidsLength;
|
const boidsLength = this.boids.length;
|
||||||
const obstaclesLength = this.obstaclesLength;
|
const obstaclesLength = this.obstacles.length;
|
||||||
|
|
||||||
this.ctx.clearRect(
|
this.ctx.clearRect(
|
||||||
-this.width / 2, -this.height / 2,
|
-this.width / 2, -this.height / 2,
|
||||||
|
|
20
geometry.mjs
20
geometry.mjs
|
@ -93,13 +93,11 @@ export class Rectangle
|
||||||
this.center = new Vector(x + w / 2, y + h / 2);
|
this.center = new Vector(x + w / 2, y + h / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
intersect(point, radius)
|
distance(point)
|
||||||
{
|
{
|
||||||
return (
|
return Math.hypot(
|
||||||
point.x >= this.x - radius
|
Math.max(point.x - this.x - this.w, 0, this.x - point.x),
|
||||||
&& point.x < this.x + this.w + radius
|
Math.max(point.y - this.y - this.h, 0, this.y - point.y),
|
||||||
&& point.y >= this.y - radius
|
|
||||||
&& point.y < this.y + this.h + radius
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,12 +115,12 @@ export class Circle
|
||||||
this.center = new Vector(x, y);
|
this.center = new Vector(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
intersect(point, radius)
|
distance(point)
|
||||||
{
|
{
|
||||||
return (
|
return Math.hypot(
|
||||||
Vector.distSquared(this.center, point)
|
this.center.x - point.x,
|
||||||
<= (this.r + radius) * (this.r + radius)
|
this.center.y - point.y,
|
||||||
);
|
) - this.r;
|
||||||
}
|
}
|
||||||
|
|
||||||
draw(ctx)
|
draw(ctx)
|
||||||
|
|
Loading…
Reference in New Issue