Skip to content

Commit

Permalink
fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
gjmcn committed May 10, 2022
1 parent 530aed8 commit 9d2d1d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 0.1.5 — May 10, 2022

* Fix bounce bug [#6](https://github.com/gjmcn/atomic-agents/issues/6).

#### 0.1.4 — May 2, 2022

* Add `zIndex` property to agents.
Expand Down
1 change: 0 additions & 1 deletion __tests__/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
partitionRect
} from '../src/helpers';

import { XSet } from '../src/x-set';
import { random } from '../src/random';
import { Simulation } from '../src/simulation';
import { Actor } from '../src/actor';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gjmcn/atomic-agents",
"version": "0.1.4",
"version": "0.1.5",
"description": "Spatial Agent-based Modeling in JavaScript.",
"type": "module",
"main": "src/index.js",
Expand Down
45 changes: 25 additions & 20 deletions src/simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,28 +624,33 @@ export class Simulation {
const { speed } = obj;
const inward = region === this ? 'true' : obj.inward;

// region is grid, square or zone:
// - no new force if actor entering and current velocity inward, or exiting
// and velocity outward.
// - amplify bounce if computed is insufficient to prevent overlap next tick
// region is grid, square or zone
if (region._shape === 'rect') {
let dim, lim, overlap;
if ((dim = 'x', lim = -1, (overlap = region.xMin - actor.x + actor.radius) >= 0) ||
( lim = 1, (overlap = actor.x + actor.radius - region.xMax) >= 0) ||
(dim = 'y', lim = -1, (overlap = region.yMin - actor.y + actor.radius) >= 0) ||
( lim = 1, (overlap = actor.y + actor.radius - region.yMax) >= 0)) {
if (actor.vel[dim] * lim * (inward ? 1 : -1) <= 0) {
return;
}
const u = actor.vel.copy();
overlap = Math.abs(overlap);
const undoOverlap = Math.abs(u[dim]);
if (overlap > undoOverlap) {
u.mult(overlap / undoOverlap * 1.000001);
}
u[dim] *= -1;
return u.sub(actor.vel);

const xOverlap = actor.x < region.x
? actor.x + actor.radius - region.xMin
: region.xMax - (actor.x - actor.radius);
const yOverlap = actor.y < region.y
? actor.y + actor.radius - region.yMin
: region.yMax - (actor.y - actor.radius);
if (xOverlap >= 2 * actor.radius && yOverlap >= 2 * actor.radius) {
return;
}

// if outward, want to flip velocity on dim with least overlap; for
// inward, want dim with greatest 'outerlap' (which is least overlap!)
const dim = xOverlap >= yOverlap ? 'y' : 'x';

// no new force if inward and bounce would decrease alignment of velocity
// vector with actor->region vector; opposite for outward
if (actor.vel[dim] * (region[dim] - actor[dim]) * (inward ? 1 : -1) > 0) {
return;
}

const u = actor.vel.copy();
u[dim] *= -1;
return u.sub(actor.vel);

}

// region is actor; inward force
Expand Down

0 comments on commit 9d2d1d7

Please sign in to comment.