diff options
-rw-r--r-- | dist/resources/entity/red_circle.json | 10 | ||||
-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 |
4 files changed, 32 insertions, 9 deletions
diff --git a/dist/resources/entity/red_circle.json b/dist/resources/entity/red_circle.json index c9479ae..65af16a 100644 --- a/dist/resources/entity/red_circle.json +++ b/dist/resources/entity/red_circle.json @@ -2,13 +2,9 @@ "sprite": "red_circle", "collision": [ { - "type": "polygon", - "vertices": [ - [-0.5, -0.5], - [-0.5, 0.5], - [0.5, 0.5], - [0.5, -0.5] - ] + "type": "circle", + "center": [0, 0], + "radius": 0.46875 } ] } 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; |