From be884b644d92a8f881b14aa07f6a9bce2f7e6a35 Mon Sep 17 00:00:00 2001 From: Sam Marquart Date: Sun, 22 Nov 2020 23:56:58 -0700 Subject: [PATCH 1/5] Add math folder; Create Angle type --- math/index.ts | 1 + math/types.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 math/index.ts create mode 100644 math/types.ts diff --git a/math/index.ts b/math/index.ts new file mode 100644 index 0000000..eea524d --- /dev/null +++ b/math/index.ts @@ -0,0 +1 @@ +export * from "./types"; diff --git a/math/types.ts b/math/types.ts new file mode 100644 index 0000000..ffdc5a7 --- /dev/null +++ b/math/types.ts @@ -0,0 +1,44 @@ +/** Converts Degrees to Radians */ +const DEGTORAD = 0.017453293; +/** Converts Radians to Degrees */ +const RADTODEG = 57.295779513; + +export class Angle { + protected constructor(private readonly rads: number) {} + + static fromDegrees(degrees: number) { + return new Angle(degrees * DEGTORAD); + } + + static fromRadians(radians: number) { + return new Angle(radians); + } + + static random() { + return new Angle(GetRandomReal(0, math.pi * 2)); + } + + get degrees() { + return this.rads * RADTODEG; + } + + get radians() { + return this.rads; + } + + get cos() { + return Cos(this.rads); + } + + get sin() { + return Sin(this.rads); + } + + add(other: Angle) { + return new Angle(this.radians + other.radians); + } +} + +export const degress = Angle.fromDegrees; +export const radians = Angle.fromRadians; +export const randomAngle = Angle.random; From 1d8c909943864a489f2091233f97636d6728d5dd Mon Sep 17 00:00:00 2001 From: Sam Marquart Date: Mon, 23 Nov 2020 00:08:51 -0700 Subject: [PATCH 2/5] Add Vec2 --- math/types.ts | 114 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/math/types.ts b/math/types.ts index ffdc5a7..93fe7dc 100644 --- a/math/types.ts +++ b/math/types.ts @@ -1,8 +1,13 @@ +import { Point } from "../handles/point"; + /** Converts Degrees to Radians */ const DEGTORAD = 0.017453293; /** Converts Radians to Degrees */ const RADTODEG = 57.295779513; +/** + * Angle encapsulates an angle, abstracting the usage of degrees and radians. + * */ export class Angle { protected constructor(private readonly rads: number) {} @@ -37,8 +42,115 @@ export class Angle { add(other: Angle) { return new Angle(this.radians + other.radians); } + + // asDirection returns a unit length Vec2 in the direction of this angle, + // parallel to the ground plane. + asDirection() { + return new Vec2(this.cos, this.sin); + } } -export const degress = Angle.fromDegrees; +export const degrees = Angle.fromDegrees; export const radians = Angle.fromRadians; export const randomAngle = Angle.random; + +export class Vec2 { + constructor(readonly x: number, readonly y: number) {} + + public get terrainZ() { + const temp = new Point(this.x, this.y); + const z = temp.z; + temp.destroy(); + return z; + } + + public add(other: Vec2) { + return new Vec2(this.x + other.x, this.y + other.y); + } + + public sub(other: Vec2) { + return new Vec2(this.x - other.x, this.y - other.y); + } + + public scale(factor: number) { + return new Vec2(this.x * factor, this.y * factor); + } + + public mul(other: Vec2) { + return new Vec2(this.x * other.x, this.y * other.y); + } + + public dot(other: Vec2) { + return this.x * other.x + this.y * other.y; + } + + public get length() { + return SquareRoot(this.lengthSq); + } + + public get lengthSq() { + return this.x * this.x + this.y * this.y; + } + + public get norm() { + const len = this.length; + if (len > 0) { + return new Vec2(this.x / len, this.y / len); + } + return new Vec2(this.x, this.y); + } + + // rotate rotates this vector around the Z axis (up from the ground). + public rotate(angle: Angle): Vec2 { + const cos = angle.cos; + const sin = angle.sin; + + const px = this.x * cos - this.y * sin; + const py = this.x * sin + this.y * cos; + return new Vec2(px, py); + } + + public angleTo(other: Vec2): Angle { + // Weird implementation note: this method causes map start failures when + // not in the same file in as the Angle class. + const dir = this.normalizedPointerTo(other); + return Angle.fromRadians(Atan2(dir.y, dir.x)); + } + + // normalizedPointerTo returns a normalized vector in the direction of the + // target. When the target and this vector are equal, return a vector + // pointing right. + public normalizedPointerTo(other: Vec2): Vec2 { + let v = other.sub(this).norm; + if (v.length == 0) { + return new Vec2(1, 0); + } + return v; + } + + public moveTowards(other: Vec2, dist: number) { + return this.add(this.normalizedPointerTo(other).scale(dist)); + } + + public polarOffset(angle: Angle, dist: number) { + return this.add(angle.asDirection().scale(dist)); + } + + public distanceTo(other: Vec2) { + return other.sub(this).length; + } + + public distanceToSq(other: Vec2) { + return other.sub(this).lengthSq; + } + + public inRange(other: Vec2, radius: number): boolean { + return this.distanceToSq(other) < radius * radius; + } + + public toString() { + return "(" + this.x.toString() + ", " + this.y.toString() + ")"; + } +} + +export const vec2 = (x: number, y: number) => new Vec2(x, y); From 0d5b9eb5398b4a6be4226eaf369b41c8419b09e3 Mon Sep 17 00:00:00 2001 From: Sam Marquart Date: Mon, 23 Nov 2020 00:37:47 -0700 Subject: [PATCH 3/5] Add Vec3; Update Vec2 --- math/types.ts | 126 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 11 deletions(-) diff --git a/math/types.ts b/math/types.ts index 93fe7dc..8016638 100644 --- a/math/types.ts +++ b/math/types.ts @@ -6,8 +6,8 @@ const DEGTORAD = 0.017453293; const RADTODEG = 57.295779513; /** - * Angle encapsulates an angle, abstracting the usage of degrees and radians. - * */ + * Encapsulates an angle, abstracting the usage of degrees and radians. + */ export class Angle { protected constructor(private readonly rads: number) {} @@ -43,7 +43,7 @@ export class Angle { return new Angle(this.radians + other.radians); } - // asDirection returns a unit length Vec2 in the direction of this angle, + // returns a unit length Vec2 in the direction of this angle, // parallel to the ground plane. asDirection() { return new Vec2(this.cos, this.sin); @@ -57,11 +57,12 @@ export const randomAngle = Angle.random; export class Vec2 { constructor(readonly x: number, readonly y: number) {} - public get terrainZ() { - const temp = new Point(this.x, this.y); - const z = temp.z; - temp.destroy(); - return z; + public withZ(z: number) { + return new Vec3(this.x, this.y, z); + } + + public withTerrainZ() { + return new Vec3(this.x, this.y, this.terrainZ); } public add(other: Vec2) { @@ -97,7 +98,7 @@ export class Vec2 { if (len > 0) { return new Vec2(this.x / len, this.y / len); } - return new Vec2(this.x, this.y); + return this; } // rotate rotates this vector around the Z axis (up from the ground). @@ -117,11 +118,11 @@ export class Vec2 { return Angle.fromRadians(Atan2(dir.y, dir.x)); } - // normalizedPointerTo returns a normalized vector in the direction of the + // returns a normalized vector in the direction of the // target. When the target and this vector are equal, return a vector // pointing right. public normalizedPointerTo(other: Vec2): Vec2 { - let v = other.sub(this).norm; + const v = other.sub(this).norm; if (v.length == 0) { return new Vec2(1, 0); } @@ -148,9 +149,112 @@ export class Vec2 { return this.distanceToSq(other) < radius * radius; } + public get terrainZ() { + const temp = new Point(this.x, this.y); + const z = temp.z; + temp.destroy(); + return z; + } + public toString() { return "(" + this.x.toString() + ", " + this.y.toString() + ")"; } } +export class Vec3 { + constructor(readonly x: number, readonly y: number, readonly z: number) {} + + toVec2() { + return new Vec2(this.x, this.y); + } + + add(other: Vec3): Vec3 { + return new Vec3(this.x + other.x, this.y + other.y, this.z + other.z); + } + + sub(other: Vec3): Vec3 { + return new Vec3(this.x - other.x, this.y - other.y, this.z - other.z); + } + + scale(factor: number): Vec3 { + return new Vec3(this.x * factor, this.y * factor, this.z * factor); + } + + mul(other: Vec3): Vec3 { + return new Vec3(this.x * other.x, this.y * other.y, this.z * other.z); + } + + dot(other: Vec3): number { + return this.x * other.x + this.y * other.y + this.z * other.z; + } + + cross(other: Vec3) { + return new Vec3( + this.y * other.z - this.z * other.y, + this.z * other.x - this.x * other.z, + this.x * other.y - this.y * other.x + ); + } + + get length(): number { + return SquareRoot(this.lengthSq); + } + + get lengthSq(): number { + return this.x * this.x + this.y * this.y + this.z * this.z; + } + + get norm(): Vec3 { + const len = this.length; + if (len > 0) { + return new Vec3(this.x / len, this.y / len, this.z / len); + } + return this; + } + + // returns a normalized vector in the direction of the + // target. When the target and origin vector are equal, returns the x-axis + // unit vector. + normalizedPointerTo(other: Vec3) { + const v = other.sub(this).norm; + if (v.length == 0) { + return new Vec3(1, 0, 0); + } + return v; + } + + distanceTo(other: Vec3) { + return other.sub(this).length; + } + + distanceToSq(other: Vec3) { + return other.sub(this).lengthSq; + } + + public polarProject(dist: number, angleGround: Angle, angleAir: Angle) { + return new Vec3( + this.x + dist * angleGround.cos * angleAir.sin, + this.y + dist * angleGround.sin * angleAir.sin, + this.z + dist * angleAir.cos + ); + } + + public moveTowards(other: Vec3, dist: number) { + return this.add(this.normalizedPointerTo(other).scale(dist)); + } + + public toString() { + return ( + "(" + + this.x.toString() + + ", " + + this.y.toString() + + ", " + + this.z.toString() + + ")" + ); + } +} + export const vec2 = (x: number, y: number) => new Vec2(x, y); +export const vec3 = (x: number, y: number, z: number) => new Vec3(x, y, z); From 1c6369f07d1d480469bdf0ba3b0c34bd58679f26 Mon Sep 17 00:00:00 2001 From: Sam Marquart Date: Mon, 23 Nov 2020 00:42:07 -0700 Subject: [PATCH 4/5] add missing public modifiers --- math/types.ts | 56 +++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/math/types.ts b/math/types.ts index 8016638..f913c81 100644 --- a/math/types.ts +++ b/math/types.ts @@ -1,8 +1,8 @@ import { Point } from "../handles/point"; -/** Converts Degrees to Radians */ +// Converts Degrees to Radians const DEGTORAD = 0.017453293; -/** Converts Radians to Degrees */ +// Converts Radians to Degrees const RADTODEG = 57.295779513; /** @@ -11,41 +11,41 @@ const RADTODEG = 57.295779513; export class Angle { protected constructor(private readonly rads: number) {} - static fromDegrees(degrees: number) { + public static fromDegrees(degrees: number) { return new Angle(degrees * DEGTORAD); } - static fromRadians(radians: number) { + public static fromRadians(radians: number) { return new Angle(radians); } - static random() { + public static random() { return new Angle(GetRandomReal(0, math.pi * 2)); } - get degrees() { + public get degrees() { return this.rads * RADTODEG; } - get radians() { + public get radians() { return this.rads; } - get cos() { + public get cos() { return Cos(this.rads); } - get sin() { + public get sin() { return Sin(this.rads); } - add(other: Angle) { + public add(other: Angle) { return new Angle(this.radians + other.radians); } // returns a unit length Vec2 in the direction of this angle, // parallel to the ground plane. - asDirection() { + public asDirection() { return new Vec2(this.cos, this.sin); } } @@ -55,7 +55,7 @@ export const radians = Angle.fromRadians; export const randomAngle = Angle.random; export class Vec2 { - constructor(readonly x: number, readonly y: number) {} + public constructor(readonly x: number, readonly y: number) {} public withZ(z: number) { return new Vec3(this.x, this.y, z); @@ -162,33 +162,37 @@ export class Vec2 { } export class Vec3 { - constructor(readonly x: number, readonly y: number, readonly z: number) {} + public constructor( + readonly x: number, + readonly y: number, + readonly z: number + ) {} - toVec2() { + public toVec2() { return new Vec2(this.x, this.y); } - add(other: Vec3): Vec3 { + public add(other: Vec3): Vec3 { return new Vec3(this.x + other.x, this.y + other.y, this.z + other.z); } - sub(other: Vec3): Vec3 { + public sub(other: Vec3): Vec3 { return new Vec3(this.x - other.x, this.y - other.y, this.z - other.z); } - scale(factor: number): Vec3 { + public scale(factor: number): Vec3 { return new Vec3(this.x * factor, this.y * factor, this.z * factor); } - mul(other: Vec3): Vec3 { + public mul(other: Vec3): Vec3 { return new Vec3(this.x * other.x, this.y * other.y, this.z * other.z); } - dot(other: Vec3): number { + public dot(other: Vec3): number { return this.x * other.x + this.y * other.y + this.z * other.z; } - cross(other: Vec3) { + public cross(other: Vec3) { return new Vec3( this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, @@ -196,15 +200,15 @@ export class Vec3 { ); } - get length(): number { + public get length(): number { return SquareRoot(this.lengthSq); } - get lengthSq(): number { + public get lengthSq(): number { return this.x * this.x + this.y * this.y + this.z * this.z; } - get norm(): Vec3 { + public get norm(): Vec3 { const len = this.length; if (len > 0) { return new Vec3(this.x / len, this.y / len, this.z / len); @@ -215,7 +219,7 @@ export class Vec3 { // returns a normalized vector in the direction of the // target. When the target and origin vector are equal, returns the x-axis // unit vector. - normalizedPointerTo(other: Vec3) { + public normalizedPointerTo(other: Vec3) { const v = other.sub(this).norm; if (v.length == 0) { return new Vec3(1, 0, 0); @@ -223,11 +227,11 @@ export class Vec3 { return v; } - distanceTo(other: Vec3) { + public distanceTo(other: Vec3) { return other.sub(this).length; } - distanceToSq(other: Vec3) { + public distanceToSq(other: Vec3) { return other.sub(this).lengthSq; } From c493c6950df79ba1b7b246a711e8a0717ff26b44 Mon Sep 17 00:00:00 2001 From: Sam Marquart Date: Mon, 23 Nov 2020 10:58:19 -0700 Subject: [PATCH 5/5] Fix terrainZ; Add missed root index export --- index.ts | 1 + math/types.ts | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 9f8d972..a321f0b 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,6 @@ export * from "./handles/index"; export * from "./hooks/index"; export * from "./system/index"; +export * from "./math/index"; export { tsGlobals }; import * as tsGlobals from "./globals/index"; diff --git a/math/types.ts b/math/types.ts index f913c81..d76ac83 100644 --- a/math/types.ts +++ b/math/types.ts @@ -149,11 +149,10 @@ export class Vec2 { return this.distanceToSq(other) < radius * radius; } + private static terrainPoint: Point = new Point(0, 0); public get terrainZ() { - const temp = new Point(this.x, this.y); - const z = temp.z; - temp.destroy(); - return z; + Vec2.terrainPoint.setPosition(this.x, this.y); + return Vec2.terrainPoint.z; } public toString() {