diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 19:59:39 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-11-05 19:59:39 +0100 |
commit | fd59aaa55c39c583af86f4dc059cbe9cedd085d2 (patch) | |
tree | 7e19b71ce1ecdf616f230ecd271400141b3e6503 /src | |
parent | 9be9f8e73940f1a38410575557fcd57cc620a8f4 (diff) | |
download | rpgedit-fd59aaa55c39c583af86f4dc059cbe9cedd085d2.tar rpgedit-fd59aaa55c39c583af86f4dc059cbe9cedd085d2.zip |
More generic collision handling
Diffstat (limited to 'src')
-rw-r--r-- | src/controller/gamecontext.ts | 19 | ||||
-rw-r--r-- | src/math/collision.ts | 7 | ||||
-rw-r--r-- | src/math/line.ts | 61 | ||||
-rw-r--r-- | src/math/point.ts | 40 |
4 files changed, 74 insertions, 53 deletions
diff --git a/src/controller/gamecontext.ts b/src/controller/gamecontext.ts index cfdcda5..4ae449a 100644 --- a/src/controller/gamecontext.ts +++ b/src/controller/gamecontext.ts @@ -6,7 +6,9 @@ import { loadMap } from '../view/map'; import { Renderer } from '../view/renderer/renderer'; import { SpriteView } from '../view/sprite'; +import { Collidable } from '../math/collision'; import { LineSegment, Movement } from '../math/line'; +import { Point } from '../math/point'; import { getJSON } from '../util'; import { vec2 } from 'gl-matrix'; @@ -24,8 +26,8 @@ export class GameContext { ); } - private static mkCollision(collision: Collision[]): LineSegment[] { - const ret: LineSegment[] = []; + private static mkCollision(collision: Collision[]): Collidable[] { + const ret: Collidable[] = []; for (const c of collision) { switch (c.type) { @@ -39,13 +41,18 @@ export class GameContext { ret.push(LineSegment.fromPoints(vec2.clone(prev), vec2.clone(v))); prev = v; } + + for (const v of c.vertices) { + ret.push(new Point(vec2.clone(v))); + prev = v; + } } } return ret; } - private static async loadMap(renderer: Renderer): Promise<[SpriteView, LineSegment[]]> { + private static async loadMap(renderer: Renderer): Promise<[SpriteView, Collidable[]]> { const map = new MapData(await getJSON('resources/map/test.json')); return [await loadMap(renderer, map), this.mkCollision(map.collision)]; } @@ -53,7 +60,7 @@ export class GameContext { private time: number|null = null; private readonly tick = 10; // ms per tick - private readonly speed = 0.05; // movement per tick + private readonly speed = 0.04; // movement per tick private readonly maxSkip = 20; // maximum ticks to process in a single render step private readonly input: DirectionHandler; @@ -67,7 +74,7 @@ export class GameContext { private readonly renderer: Renderer, private readonly mapView: SpriteView, private readonly entity: SpriteView, - private readonly collision: LineSegment[], + private readonly collision: Collidable[], ) { this.input = new DirectionHandler(); this.input.addListener((v) => { @@ -102,7 +109,7 @@ export class GameContext { const move = new Movement(this.entityPos, dest); for (const c of this.collision) { - if (!c.collidesMoveCircle(dest2, move, this.collisionRadius)) + if (!c.collide(dest2, move, this.collisionRadius)) continue; if (!vec2.exactEquals(dest, dest2)) { diff --git a/src/math/collision.ts b/src/math/collision.ts new file mode 100644 index 0000000..3df811a --- /dev/null +++ b/src/math/collision.ts @@ -0,0 +1,7 @@ +import { Movement } from './line'; + +import { vec2 } from 'gl-matrix'; + +export interface Collidable { + collide(out: vec2, move: Movement, r: number): boolean; +} diff --git a/src/math/line.ts b/src/math/line.ts index d071779..d29bc76 100644 --- a/src/math/line.ts +++ b/src/math/line.ts @@ -1,4 +1,5 @@ import { mat2, vec2 } from 'gl-matrix'; +import { Collidable } from './collision'; const rot90 = mat2.fromValues( 0, 1, @@ -80,7 +81,7 @@ export class Movement { } } -export class LineSegment extends Line { +export class LineSegment extends Line implements Collidable { public static fromPoints(p1: vec2, p2: vec2): LineSegment { const d = vec2.dist(p1, p2); const v = vec2.sub(vec2.create(), p2, p1); @@ -101,70 +102,36 @@ export class LineSegment extends Line { return vec2.scaleAndAdd(out, this.p, this.v, this.l); } - public collidesPoint(p2: vec2): boolean { + public containsPoint(p2: vec2): boolean { const d = this.projectPointDistance(p2); return (d >= 0 && d <= this.l); } - public collidesMove(out: vec2, move: Movement): boolean { - if (this.distancePoint(move.p1) < 0) - return false; - - if (this.distancePoint(move.p2) >= 0) - return false; - - const x = move.intersectLine(vec2.create(), this); - if (!this.collidesPoint(x)) - return false; - - this.projectPoint(out, move.p2); - - return true; - } - - public collidesMoveCircle(out: vec2, move: Movement, r: number): boolean { + public collide(out: vec2, move: Movement, r: number): boolean { const t = this.getNormal(vec2.create()); vec2.scale(t, t, -r); const refMove = move.translate(t); - const refOut = vec2.create(); - if (this.collidesMove(refOut, refMove)) { - vec2.sub(out, refOut, t); - return true; - } + if (!this.collideRef(out, refMove)) + return false; - return this.collidesPointMoveCircle(out, move, r); + vec2.sub(out, out, t); + return true; } - private collidesPointMoveCircle(out: vec2, move: Movement, r: number): boolean { - const moveLine = move.toLineSegment(); - - if (moveLine.projectPointDistance(this.p) < 0) + private collideRef(out: vec2, move: Movement): boolean { + if (this.distancePoint(move.p1) < 0) return false; - const d = moveLine.distancePoint(this.p) / r; - if (Math.abs(d) >= 1) + if (this.distancePoint(move.p2) >= 0) return false; - const e = Math.sqrt(1 - d * d); - - const t = moveLine.getNormal(vec2.create()); - vec2.scale(t, t, d); - vec2.scaleAndAdd(t, t, moveLine.v, e); - - const tr = vec2.scale(vec2.create(), t, r); - - const refMove = move.translate(tr); - - if (vec2.sqrDist(this.p, move.p1) > r * r && !refMove.passes(this.p)) + const x = move.intersectLine(vec2.create(), this); + if (!this.containsPoint(x)) return false; - normal(t, t); - - const tang = new Line(this.p, t); - tang.projectPoint(out, refMove.p2); - vec2.sub(out, out, tr); + this.projectPoint(out, move.p2); return true; } diff --git a/src/math/point.ts b/src/math/point.ts new file mode 100644 index 0000000..23a0f84 --- /dev/null +++ b/src/math/point.ts @@ -0,0 +1,40 @@ +import { Collidable } from './collision'; +import { Line, Movement, normal } from './line'; + +import { vec2 } from 'gl-matrix'; + +export class Point implements Collidable { + constructor(public readonly p: vec2) {} + + public collide(out: vec2, move: Movement, r: number): boolean { + const moveLine = move.toLineSegment(); + + if (moveLine.projectPointDistance(this.p) < 0) + return false; + + const d = moveLine.distancePoint(this.p) / r; + if (Math.abs(d) >= 1) + return false; + + const e = Math.sqrt(1 - d * d); + + const t = moveLine.getNormal(vec2.create()); + vec2.scale(t, t, d); + vec2.scaleAndAdd(t, t, moveLine.v, e); + + const tr = vec2.scale(vec2.create(), t, r); + + const refMove = move.translate(tr); + + if (vec2.sqrDist(this.p, move.p1) > r * r && !refMove.passes(this.p)) + return false; + + normal(t, t); + + const tang = new Line(this.p, t); + tang.projectPoint(out, refMove.p2); + vec2.sub(out, out, tr); + + return true; + } +} |