From 72ee5520a5bbfdab6975122c45ab17bb4f5eb0e5 Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 12:02:09 +0100 Subject: [PATCH 1/6] add rotate for Vec2 --- src/Vec2.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Vec2.ts b/src/Vec2.ts index 52bd055..514e2a7 100644 --- a/src/Vec2.ts +++ b/src/Vec2.ts @@ -401,6 +401,36 @@ export default class Vec2 implements Vector2 { const proj = this.projectOnto(normal); return this.subtract(proj.multiply(2)); } + /** + * Rotates the current normalized vector by a given angle around a given axis. + * + * @param axis - The axis of rotation. + * @param angle - The angle of rotation in degrees. + * @returns The rotated vector. + */ + rotate(axis: Vector2, angle: number): Vec2 { + // Convert angle from degrees to radians + const radians = angle * (Math.PI / 180); + + // Translate the vector to the origin relative to the pivot (axis) + let translatedX = this.x - axis.x; + let translatedY = this.y - axis.y; + + // Use complex number rotation (Euler's formula) + // New x = x * cos(radians) - y * sin(radians) + // New y = x * sin(radians) + y * cos(radians) + const cos = Math.cos(radians); + const sin = Math.sin(radians); + + const rotatedX = translatedX * cos - translatedY * sin; + const rotatedY = translatedX * sin + translatedY * cos; + + // Translate the rotated vector back relative to the pivot (axis) and return it + return new Vec2( + rotatedX + axis.x, + rotatedY + axis.y + ); + } /** * Sets the X component of the vector. * From 9e4dd834387f8850726f2cb57e88828463104aa3 Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 12:12:05 +0100 Subject: [PATCH 2/6] add toVec3 method in Vec2 --- src/Vec2.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Vec2.ts b/src/Vec2.ts index 514e2a7..692323e 100644 --- a/src/Vec2.ts +++ b/src/Vec2.ts @@ -1,5 +1,6 @@ import { Vector2, Direction, VectorXZ } from "@minecraft/server"; import { Logger } from "./Logging"; +import Vec3 from "./Vec3"; type VectorLike = VectorXZ | Vector2 | Vec2 | Direction | number[] | number @@ -431,6 +432,15 @@ export default class Vec2 implements Vector2 { rotatedY + axis.y ); } + /** + * Converts the current vector to a 3d vetor with the given y-value. + * + * @param z - The optional z value for the 3d vetor. + * @returns The converted vector. + */ + toVec3(z = 0): Vec3 { + return new Vec3(this.x, this.y, z); + } /** * Sets the X component of the vector. * From 7a83db2646522e491af7840e06b6b5c2de72b39f Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 12:28:03 +0100 Subject: [PATCH 3/6] impl from Vec2 for Vec3 --- src/Vec3.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Vec3.ts b/src/Vec3.ts index a2b36e6..6380908 100644 --- a/src/Vec3.ts +++ b/src/Vec3.ts @@ -1,5 +1,6 @@ import { Vector3, Direction, Vector2 } from "@minecraft/server"; import { Logger } from "./Logging"; +import Vec2 from "./Vec2"; type VectorLike = Vector3 | Vec3 | Direction | number[] | number @@ -74,11 +75,13 @@ export default class Vec3 implements Vector3 { */ static from(x: number, y: number, z: number): Vec3; static from(x: Vec3): Vec3; + static from(x: Vec2, z?: number | undefined): Vec3; static from(x: Vector3): Vec3; static from(x: Direction): Vec3; static from(x: number[]): Vec3; static from(x: VectorLike, y?: number, z?: number): Vec3 { if (x instanceof Vec3) return x; + if (x instanceof Vec2) return x.toVec3(z); if (typeof x === 'number' && y !== undefined && z !== undefined) { return new Vec3(x, y, z); } From 176463ca46f3bcafd7ae95e77ced076b50cc3462 Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 12:30:31 +0100 Subject: [PATCH 4/6] use undefined for optional Z in Vec2#toVec3 instead --- src/Vec2.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Vec2.ts b/src/Vec2.ts index 692323e..52d97cb 100644 --- a/src/Vec2.ts +++ b/src/Vec2.ts @@ -438,8 +438,8 @@ export default class Vec2 implements Vector2 { * @param z - The optional z value for the 3d vetor. * @returns The converted vector. */ - toVec3(z = 0): Vec3 { - return new Vec3(this.x, this.y, z); + toVec3(z?: number | undefined): Vec3 { + return new Vec3(this.x, this.y, z ? z : 0); } /** * Sets the X component of the vector. From 2d1ec6156b8a1df8612f20ba39c74f347c622a4e Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 13:00:42 +0100 Subject: [PATCH 5/6] add ctor for Vec3 from Vec2 --- src/Vec2.ts | 4 ++-- src/Vec3.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Vec2.ts b/src/Vec2.ts index 52d97cb..cd845ea 100644 --- a/src/Vec2.ts +++ b/src/Vec2.ts @@ -438,8 +438,8 @@ export default class Vec2 implements Vector2 { * @param z - The optional z value for the 3d vetor. * @returns The converted vector. */ - toVec3(z?: number | undefined): Vec3 { - return new Vec3(this.x, this.y, z ? z : 0); + toVec3(z?: number): Vec3 { + return new Vec3(this.x, this.y, z || 0); } /** * Sets the X component of the vector. diff --git a/src/Vec3.ts b/src/Vec3.ts index 6380908..faad18f 100644 --- a/src/Vec3.ts +++ b/src/Vec3.ts @@ -2,7 +2,7 @@ import { Vector3, Direction, Vector2 } from "@minecraft/server"; import { Logger } from "./Logging"; import Vec2 from "./Vec2"; -type VectorLike = Vector3 | Vec3 | Direction | number[] | number +type VectorLike = Vector3 | Vec3 | Vec2 | Direction | number[] | number export default class Vec3 implements Vector3 { private static readonly log = Logger.getLogger("vec3", "vec3", "bedrock-boost"); @@ -19,6 +19,7 @@ export default class Vec3 implements Vector3 { readonly z: number; constructor(x: number, y: number, z: number); constructor(x: Vec3); + constructor(x: Vec2, z?: number); constructor(x: Vector3); constructor(x: Direction); constructor(x: number[]); @@ -59,7 +60,11 @@ export default class Vec3 implements Vector3 { this.x = x.x; this.y = x.y; this.z = x.z; - } else { + } else if (x instanceof Vec2) { + this.x = x.x; + this.y = x.y; + this.z = z || 0; + }else { if (!x || (!x.x && x.x !== 0) || (!x.y && x.y !== 0) || (!x.z && x.z !== 0)) { Vec3.log.error(new Error("Invalid vector"), x); throw new Error("Invalid vector"); @@ -75,13 +80,13 @@ export default class Vec3 implements Vector3 { */ static from(x: number, y: number, z: number): Vec3; static from(x: Vec3): Vec3; - static from(x: Vec2, z?: number | undefined): Vec3; + static from(x: Vec2, z?: number): Vec3; static from(x: Vector3): Vec3; static from(x: Direction): Vec3; static from(x: number[]): Vec3; static from(x: VectorLike, y?: number, z?: number): Vec3 { if (x instanceof Vec3) return x; - if (x instanceof Vec2) return x.toVec3(z); + if (x instanceof Vec2) return x.toVec3(z || 0); if (typeof x === 'number' && y !== undefined && z !== undefined) { return new Vec3(x, y, z); } From 7637e2f1563818fc101c72be9cf42bec5c8238d1 Mon Sep 17 00:00:00 2001 From: theaddon Date: Sat, 2 Nov 2024 13:09:44 +0100 Subject: [PATCH 6/6] enable conversion from Vec3 to Vec2 --- src/Vec2.ts | 8 +++++++- src/Vec3.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Vec2.ts b/src/Vec2.ts index cd845ea..1c0c33a 100644 --- a/src/Vec2.ts +++ b/src/Vec2.ts @@ -2,7 +2,7 @@ import { Vector2, Direction, VectorXZ } from "@minecraft/server"; import { Logger } from "./Logging"; import Vec3 from "./Vec3"; -type VectorLike = VectorXZ | Vector2 | Vec2 | Direction | number[] | number +type VectorLike = VectorXZ | Vector2 | Vec2 | Vec3 | Direction | number[] | number export default class Vec2 implements Vector2 { private static readonly log = Logger.getLogger("vec2", "vec2", "bedrock-boost"); @@ -16,6 +16,7 @@ export default class Vec2 implements Vector2 { readonly y: number; constructor(x: number, y: number); constructor(x: Vec2); + constructor(x: Vec3); constructor(x: Vector2); constructor(x: Direction); constructor(x: number[]); @@ -44,6 +45,9 @@ export default class Vec2 implements Vector2 { } else if (x instanceof Vec2) { this.x = x.x; this.y = x.y; + } else if (x instanceof Vec3) { + this.x = x.x; + this.y = x.y; } else { const anyX = x as any; if (!anyX || (!anyX.x && anyX.x !== 0) || ((!anyX.y && anyX.y !== 0) && (!anyX.z && anyX.z !== 0))) { @@ -67,12 +71,14 @@ export default class Vec2 implements Vector2 { */ static from(x: number, y: number): Vec2; static from(x: Vec2): Vec2; + static from(x: Vec3): Vec2; static from(x: Vector2): Vec2; static from(x: VectorXZ): Vec2; static from(x: Direction): Vec2; static from(x: number[]): Vec2; static from(x: VectorLike, y?: number): Vec2 { if (x instanceof Vec2) return x; + if (x instanceof Vec3) return if (typeof x === 'number' && y !== undefined) { return new Vec2(x, y); } diff --git a/src/Vec3.ts b/src/Vec3.ts index faad18f..5092464 100644 --- a/src/Vec3.ts +++ b/src/Vec3.ts @@ -133,6 +133,14 @@ export default class Vec3 implements Vector3 { copy(): Vec3 { return new Vec3(this.x, this.y, this.z); } + /** + * Converts the current vector to a 2d vetor. + * + * @returns The converted vector. + */ + toVec2(): Vec2 { + return new Vec2(this.x, this.z); + } /** * Creates a new direction vector from yaw and pitch values. *