summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-11-08 22:26:11 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-11-08 22:26:11 +0100
commite0eddb8741b840df97380e557abd51b47a5e2eee (patch)
tree43fadcde4292437b14dcece8a0d85da560a4eb94 /src
parent5eae6f29a80cd47e268cb6eaefa96a0ab0a63e54 (diff)
downloadrpgedit-e0eddb8741b840df97380e557abd51b47a5e2eee.tar
rpgedit-e0eddb8741b840df97380e557abd51b47a5e2eee.zip
Implement circle collidables
Diffstat (limited to 'src')
-rw-r--r--src/controller/collision.ts6
-rw-r--r--src/math/circle.ts15
-rw-r--r--src/model/data/collision.ts10
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;