diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/controller/collision.ts | 6 | ||||
-rw-r--r-- | src/math/circle.ts | 15 | ||||
-rw-r--r-- | src/model/data/collision.ts | 10 |
3 files changed, 29 insertions, 2 deletions
diff --git a/src/controller/collision.ts b/src/controller/collision.ts index e1bd9f9..a44ffa4 100644 --- a/src/controller/collision.ts +++ b/src/controller/collision.ts @@ -1,5 +1,6 @@ import { Collision } from '../model/data/collision'; +import { Circle } from '../math/circle'; import { Collidable } from '../math/collision'; import { LineSegment, Movement } from '../math/line'; import { Point } from '../math/point'; @@ -26,6 +27,11 @@ export function mkCollision(collision: Collision[]): Collidable[] { ret.push(new Point(vec2.clone(v))); prev = v; } + break; + + case 'circle': + ret.push(new Circle(vec2.clone(c.center), c.radius)); + break; } } diff --git a/src/math/circle.ts b/src/math/circle.ts new file mode 100644 index 0000000..f4e855a --- /dev/null +++ b/src/math/circle.ts @@ -0,0 +1,15 @@ +import { Collidable } from './collision'; +import { Movement } from './line'; +import { Point } from './point'; + +import { vec2 } from 'gl-matrix'; + +export class Circle implements Collidable, Point { + private pointCollide = Point.prototype.collide; + + constructor(public readonly p: vec2, public readonly r: number) {} + + public collide(out: vec2, move: Movement, r: number): boolean { + return this.pointCollide(out, move, r + this.r); + } +} diff --git a/src/model/data/collision.ts b/src/model/data/collision.ts index 5008fa3..29d0879 100644 --- a/src/model/data/collision.ts +++ b/src/model/data/collision.ts @@ -1,6 +1,12 @@ -export interface CollisionPolygon { +export interface PolygonCollision { readonly type: 'polygon'; readonly vertices: Array<[number, number]>; } -export type Collision = CollisionPolygon; +export interface CircleCollision { + readonly type: 'circle'; + readonly center: [number, number]; + readonly radius: number; +} + +export type Collision = PolygonCollision | CircleCollision; |