From 4466df99c9720d63b883bfae7a3988fdcfa7161d Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Tue, 2 Dec 2025 12:16:39 -0500 Subject: [PATCH 1/6] feat: make Vec2 able to do math on noninstances --- src/math/Vec2.ts | 149 +++++++++++++++++++++++++++++++++-------------- src/math/math.ts | 13 ++--- 2 files changed, 111 insertions(+), 51 deletions(-) diff --git a/src/math/Vec2.ts b/src/math/Vec2.ts index 8a6680c2d..24131812a 100644 --- a/src/math/Vec2.ts +++ b/src/math/Vec2.ts @@ -19,7 +19,7 @@ export interface SerializedVec2 { * * @group Math */ -export class Vec2 { +export class Vec2 implements SerializedVec2 { /** The x coordinate */ x: number = 0; /** The y coordinate */ @@ -75,7 +75,7 @@ export class Vec2 { return new Vec2(this.x, this.y); } - static copy(v: Vec2, out: Vec2): Vec2 { + static copy(v: SerializedVec2, out: T): T { out.x = v.x; out.y = v.y; return out; @@ -87,7 +87,12 @@ export class Vec2 { return new Vec2(this.x + p2.x, this.y + p2.y); } - static addScaled(v: Vec2, other: Vec2, s: number, out: Vec2): Vec2 { + static addScaled( + v: SerializedVec2, + other: SerializedVec2, + s: number, + out: T, + ): T { out.x = v.x + other.x * s; out.y = v.y + other.y * s; return out; @@ -102,7 +107,12 @@ export class Vec2 { * * @returns The sum of the vectors */ - static addc(v: Vec2, x: number, y: number, out: Vec2): Vec2 { + static addc( + v: SerializedVec2, + x: number, + y: number, + out: T, + ): T { out.x = v.x + x; out.y = v.y + y; return out; @@ -116,7 +126,11 @@ export class Vec2 { * * @returns The sum of the vectors */ - static add(v: Vec2, other: Vec2, out: Vec2): Vec2 { + static add( + v: SerializedVec2, + other: SerializedVec2, + out: T, + ): T { out.x = v.x + other.x; out.y = v.y + other.y; return out; @@ -137,7 +151,12 @@ export class Vec2 { * * @returns The difference of the vectors */ - static subc(v: Vec2, x: number, y: number, out: Vec2): Vec2 { + static subc( + v: SerializedVec2, + x: number, + y: number, + out: T, + ): T { out.x = v.x - x; out.y = v.y - y; return out; @@ -151,7 +170,11 @@ export class Vec2 { * * @returns The difference of the vectors */ - static sub(v: Vec2, other: Vec2, out: Vec2): Vec2 { + static sub( + v: SerializedVec2, + other: SerializedVec2, + out: T, + ): T { out.x = v.x - other.x; out.y = v.y - other.y; return out; @@ -172,7 +195,11 @@ export class Vec2 { * * @returns The scale of the vector */ - static scale(v: Vec2, s: number, out: Vec2): Vec2 { + static scale( + v: SerializedVec2, + s: number, + out: T, + ): T { out.x = v.x * s; out.y = v.y * s; return out; @@ -187,7 +214,12 @@ export class Vec2 { * * @returns The scale of the vector */ - static scalec(v: Vec2, x: number, y: number, out: Vec2): Vec2 { + static scalec( + v: SerializedVec2, + x: number, + y: number, + out: T, + ): T { out.x = v.x * x; out.y = v.y * y; return out; @@ -201,7 +233,11 @@ export class Vec2 { * * @returns The scale of the vector */ - static scalev(v: Vec2, other: Vec2, out: Vec2): Vec2 { + static scalev( + v: SerializedVec2, + other: SerializedVec2, + out: T, + ): T { out.x = v.x * other.x; out.y = v.y * other.y; return out; @@ -226,10 +262,10 @@ export class Vec2 { * * @returns The between the vectors */ - static dist(v: Vec2, other: Vec2): number { + static dist(v: SerializedVec2, other: SerializedVec2): number { const x = v.x - other.x; const y = v.y - other.y; - return Math.sqrt(x * x + y * y); + return Math.hypot(x, y); } /** Get squared distance between another vector */ @@ -245,7 +281,7 @@ export class Vec2 { * * @returns The distance between the vectors */ - static sdist(v: Vec2, other: Vec2): number { + static sdist(v: SerializedVec2, other: SerializedVec2): number { const x = v.x - other.x; const y = v.y - other.y; return x * x + y * y; @@ -266,8 +302,8 @@ export class Vec2 { * * @returns The length of the vector */ - static len(v: Vec2) { - return Math.sqrt(v.x * v.x + v.y * v.y); + static len(v: SerializedVec2) { + return Math.hypot(v.x, v.y); } /** @@ -285,7 +321,7 @@ export class Vec2 { * * @returns The squared length of the vector */ - static slen(v: Vec2) { + static slen(v: SerializedVec2) { return v.x * v.x + v.y * v.y; } @@ -297,7 +333,7 @@ export class Vec2 { return len === 0 ? new Vec2(0) : this.scale(1 / len); } - static unit(v: Vec2, out: Vec2): Vec2 { + static unit(v: SerializedVec2, out: T): T { const len = Vec2.len(v); if (len === 0) { out.x = 0; @@ -316,7 +352,7 @@ export class Vec2 { return new Vec2(this.y, -this.x); } - static normal(v: Vec2, out: Vec2): Vec2 { + static normal(v: SerializedVec2, out: T): T { out.x = v.y; out.y = -v.x; return out; @@ -327,8 +363,9 @@ export class Vec2 { * * @since v3000.0 */ - reflect(normal: Vec2) { - return this.sub(normal.scale(2 * this.dot(normal))); + reflect(normal: SerializedVec2) { + const s = 2 * this.dot(normal); + return this.sub(normal.x * s, normal.y * s); } /** @@ -336,8 +373,9 @@ export class Vec2 { * * @since v3000.0 */ - project(on: Vec2) { - return on.scale(on.dot(this) / on.len()); + project(on: SerializedVec2) { + const s = this.dot(on) / Vec2.len(on); + return new Vec2(on.x * s, on.y * s); } /** @@ -345,12 +383,12 @@ export class Vec2 { * * @since v3000.0 */ - reject(on: Vec2) { + reject(on: SerializedVec2) { return this.sub(this.project(on)); } - rotate(vecOrAngle: Vec2 | number) { - if (vecOrAngle instanceof Vec2) { + rotate(vecOrAngle: SerializedVec2 | number) { + if (typeof vecOrAngle === "object") { return new Vec2( this.x * vecOrAngle.x - this.y * vecOrAngle.y, this.x * vecOrAngle.y + this.y * vecOrAngle.x, @@ -375,8 +413,12 @@ export class Vec2 { * * @returns The rotated vector */ - static rotate(v: Vec2, dir: Vec2, out: Vec2): Vec2 { - const tmp = v.x; + static rotate( + v: SerializedVec2, + dir: SerializedVec2, + out: T, + ): T { + const tmp = v.x; // save in case v and out are the same object out.x = v.x * dir.x - v.y * dir.y; out.y = tmp * dir.y + v.y * dir.x; return out; @@ -390,17 +432,21 @@ export class Vec2 { * * @returns The rotated vector */ - static rotateByAngle(v: Vec2, angle: number, out: Vec2): Vec2 { + static rotateByAngle( + v: SerializedVec2, + angle: number, + out: T, + ): T { const c = Math.cos(angle); const s = Math.sin(angle); - const tmp = v.x; + const tmp = v.x; // save in case v and out are the same object out.x = v.x * c - v.y * s; out.y = tmp * s + v.y * c; return out; } - invRotate(vecOrAngle: Vec2 | number) { - if (vecOrAngle instanceof Vec2) { + invRotate(vecOrAngle: SerializedVec2 | number) { + if (typeof vecOrAngle === "object") { return this.rotate(new Vec2(vecOrAngle.x, -vecOrAngle.y)); } else { @@ -416,8 +462,12 @@ export class Vec2 { * * @returns The rotated vector */ - static inverseRotate(v: Vec2, dir: Vec2, out: Vec2): Vec2 { - const tmp = v.x; + static inverseRotate( + v: SerializedVec2, + dir: SerializedVec2, + out: T, + ): T { + const tmp = v.x; // save in case v and out are the same object out.x = v.x * dir.x + v.y * dir.y; out.y = -tmp * dir.y + v.y * dir.x; return out; @@ -426,7 +476,7 @@ export class Vec2 { /** * Get the dot product with another vector. */ - dot(p2: Vec2): number { + dot(p2: SerializedVec2): number { return this.x * p2.x + this.y * p2.y; } @@ -435,7 +485,7 @@ export class Vec2 { * * @since v3000.0 */ - static dot(v: Vec2, other: Vec2): number { + static dot(v: SerializedVec2, other: SerializedVec2): number { return v.x * other.x + v.y * other.y; } @@ -444,7 +494,7 @@ export class Vec2 { * * @since v3000.0 */ - cross(p2: Vec2): number { + cross(p2: SerializedVec2): number { return this.x * p2.y - this.y * p2.x; } @@ -453,7 +503,7 @@ export class Vec2 { * * @since v3000.0 */ - static cross(v: Vec2, other: Vec2): number { + static cross(v: SerializedVec2, other: SerializedVec2): number { return v.x * other.y - v.y * other.x; } @@ -471,7 +521,7 @@ export class Vec2 { * * @returns Angle represented by the vector in radians */ - static toAngle(v: Vec2) { + static toAngle(v: SerializedVec2) { return Math.atan2(v.y, v.x); } @@ -492,14 +542,14 @@ export class Vec2 { * * @returns Angle between the vectors in radians */ - static angleBetween(v: Vec2, other: Vec2) { + static angleBetween(v: SerializedVec2, other: SerializedVec2) { return Math.atan2(Vec2.cross(v, other), Vec2.dot(v, other)); } /** * Linear interpolate to a destination vector (for positions). */ - lerp(dest: Vec2, t: number): Vec2 { + lerp(dest: SerializedVec2, t: number): Vec2 { return new Vec2( lerpNumber(this.x, dest.x, t), lerpNumber(this.y, dest.y, t), @@ -515,7 +565,12 @@ export class Vec2 { * * @returns The linear interpolation between src and dst by t */ - static lerp(src: Vec2, dst: Vec2, t: number, out: Vec2): Vec2 { + static lerp( + src: SerializedVec2, + dst: SerializedVec2, + t: number, + out: T, + ): T { out.x = src.x * (dst.x - src.x) * t; out.y = src.y * (dst.y - src.y) * t; return out; @@ -526,13 +581,14 @@ export class Vec2 { * * @since v3000.0 */ - slerp(dest: Vec2, t: number): Vec2 { + slerp(dest: SerializedVec2, t: number): Vec2 { const cos = this.dot(dest); const sin = this.cross(dest); const angle = Math.atan2(sin, cos); + const destScale = Math.sin(t * angle); return this .scale(Math.sin((1 - t) * angle)) - .add(dest.scale(Math.sin(t * angle))) + .add(dest.x * destScale, dest.y * destScale) .scale(1 / sin); } @@ -545,7 +601,12 @@ export class Vec2 { * * @returns The spherical interpolation between src and dst by t */ - static slerp(src: Vec2, dst: Vec2, t: number, out: Vec2): Vec2 { + static slerp( + src: SerializedVec2, + dst: SerializedVec2, + t: number, + out: T, + ): T { const cos = Vec2.dot(src, dst); const sin = Vec2.cross(src, dst); const angle = Math.atan2(sin, cos); diff --git a/src/math/math.ts b/src/math/math.ts index 3367f476c..71834a7b4 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -11,7 +11,7 @@ import { clamp } from "./clamp"; import { Color, rgb } from "./color"; import { traceRegion } from "./getImageOutline"; import { lerp, type LerpValue } from "./lerp"; -import { Vec2 } from "./Vec2"; +import { Vec2, type SerializedVec2 } from "./Vec2"; /** * Possible arguments for a Vec2. @@ -20,11 +20,10 @@ import { Vec2 } from "./Vec2"; * @subgroup Vectors */ export type Vec2Args = - | [number, number] - | [number] - | [Vec2] - | [number | Vec2] - | []; + | [number, number] // x = args[0] y = args[1] + | [number] // x = args[0] y = x + | [SerializedVec2] // x = args[0].x y = args[0].y + | []; // x = 0 y = 0 export function deg2rad(deg: number): number { return deg * Math.PI / 180; @@ -65,7 +64,7 @@ export function smoothstep(edge0: number, edge1: number, x: number) { export function vec2(...args: Vec2Args): Vec2 { if (args.length === 1) { - if (args[0] instanceof Vec2) { + if (typeof args[0] === "object") { return new Vec2(args[0].x, args[0].y); } else if (Array.isArray(args[0]) && args[0].length === 2) { From f09eda4d41b5ffe9549679d0d9cd4bcdc410184a Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Tue, 2 Dec 2025 12:18:06 -0500 Subject: [PATCH 2/6] formatter --- src/math/math.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/math/math.ts b/src/math/math.ts index 71834a7b4..664d6bab3 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -3,15 +3,13 @@ // - move Vec2 to it's own file import { resolveSprite } from "../assets/sprite"; -import { drawCircle } from "../gfx/draw/drawCircle"; -import { drawPolygon, type DrawPolygonOpt } from "../gfx/draw/drawPolygon"; import { _k } from "../shared"; import type { GameObj, RNGValue, Shape } from "../types"; import { clamp } from "./clamp"; import { Color, rgb } from "./color"; import { traceRegion } from "./getImageOutline"; import { lerp, type LerpValue } from "./lerp"; -import { Vec2, type SerializedVec2 } from "./Vec2"; +import { type SerializedVec2, Vec2 } from "./Vec2"; /** * Possible arguments for a Vec2. @@ -21,9 +19,9 @@ import { Vec2, type SerializedVec2 } from "./Vec2"; */ export type Vec2Args = | [number, number] // x = args[0] y = args[1] - | [number] // x = args[0] y = x + | [number] // x = args[0] y = x | [SerializedVec2] // x = args[0].x y = args[0].y - | []; // x = 0 y = 0 + | []; // x = 0 y = 0 export function deg2rad(deg: number): number { return deg * Math.PI / 180; From f64a4f174785a7cd7a27f447a8620d76b4141caa Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Tue, 2 Dec 2025 12:19:00 -0500 Subject: [PATCH 3/6] changeloged --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5235bed08..0085dee42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ best friend, lajbel, can put the correct version name here needs expansion) - @mflerackers - Added vertical sweep and prune - @mflerackers - Added configuration to choose broad phase algorithm - @mflerackers +- Vec2's can now operate on any object with x and y properties and don't need to + have the arguments deserialized - @dragoncoder047 ### Fixed From 4802ecfd703baad6f08a5944b668af2e743af168 Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Tue, 2 Dec 2025 12:47:59 -0500 Subject: [PATCH 4/6] okay THAT'S why that was there --- src/math/math.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/math/math.ts b/src/math/math.ts index 664d6bab3..e82fd1214 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -20,6 +20,7 @@ import { type SerializedVec2, Vec2 } from "./Vec2"; export type Vec2Args = | [number, number] // x = args[0] y = args[1] | [number] // x = args[0] y = x + | [number | SerializedVec2] | [SerializedVec2] // x = args[0].x y = args[0].y | []; // x = 0 y = 0 From a9a96c8cc54855296bfa045636a98228246e0db4 Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Wed, 3 Dec 2025 20:41:53 -0500 Subject: [PATCH 5/6] rename SerializedVec2 --> Vec2Like --- src/ecs/components/transform/anchor.ts | 5 +- src/ecs/components/transform/move.ts | 6 +- src/ecs/components/transform/scale.ts | 4 +- src/ecs/components/transform/skew.ts | 4 +- src/math/Vec2.ts | 116 ++++++++++++------------- src/math/math.ts | 6 +- 6 files changed, 70 insertions(+), 71 deletions(-) diff --git a/src/ecs/components/transform/anchor.ts b/src/ecs/components/transform/anchor.ts index 3c608ddd3..94a6d5b1e 100644 --- a/src/ecs/components/transform/anchor.ts +++ b/src/ecs/components/transform/anchor.ts @@ -1,6 +1,5 @@ import { anchorPt } from "../../../gfx/anchor"; -import { vec2 } from "../../../math/math"; -import { type SerializedVec2, Vec2 } from "../../../math/Vec2"; +import { Vec2, type Vec2Like } from "../../../math/Vec2"; import type { Anchor, Comp } from "../../../types"; /** @@ -10,7 +9,7 @@ import type { Anchor, Comp } from "../../../types"; * @subgroup Component Serialization */ export interface SerializedAnchorComp { - anchor: SerializedVec2; + anchor: Vec2Like; } /** diff --git a/src/ecs/components/transform/move.ts b/src/ecs/components/transform/move.ts index a6c7d1714..16ccdefa0 100644 --- a/src/ecs/components/transform/move.ts +++ b/src/ecs/components/transform/move.ts @@ -1,5 +1,5 @@ -import { type SerializedVec2, Vec2 } from "../../../math/Vec2"; -import type { Comp, EmptyComp, GameObj } from "../../../types"; +import { Vec2, type Vec2Like } from "../../../math/Vec2"; +import type { Comp, GameObj } from "../../../types"; import type { PosComp } from "./pos"; /** @@ -9,7 +9,7 @@ import type { PosComp } from "./pos"; * @subgroup Component Serialization */ interface SerializedMoveComp { - dir: SerializedVec2 | number; + dir: Vec2Like | number; speed: number; } diff --git a/src/ecs/components/transform/scale.ts b/src/ecs/components/transform/scale.ts index da056461c..38fae981c 100644 --- a/src/ecs/components/transform/scale.ts +++ b/src/ecs/components/transform/scale.ts @@ -1,5 +1,5 @@ import { vec2, type Vec2Args } from "../../../math/math"; -import { type SerializedVec2, Vec2 } from "../../../math/Vec2"; +import { Vec2, type Vec2Like } from "../../../math/Vec2"; import type { Comp } from "../../../types"; /** @@ -9,7 +9,7 @@ import type { Comp } from "../../../types"; * @subgroup Component Serialization */ export interface SerializedScaleComp { - scale: SerializedVec2; + scale: Vec2Like; } /** diff --git a/src/ecs/components/transform/skew.ts b/src/ecs/components/transform/skew.ts index ed8a661dd..e8153963b 100644 --- a/src/ecs/components/transform/skew.ts +++ b/src/ecs/components/transform/skew.ts @@ -1,5 +1,5 @@ import { vec2, type Vec2Args } from "../../../math/math"; -import { type SerializedVec2, Vec2 } from "../../../math/Vec2"; +import { Vec2, type Vec2Like } from "../../../math/Vec2"; import type { Comp } from "../../../types"; /** @@ -9,7 +9,7 @@ import type { Comp } from "../../../types"; * @subgroup Component Serialization */ export interface SerializedSkewComp { - skew: SerializedVec2; + skew: Vec2Like; } /** diff --git a/src/math/Vec2.ts b/src/math/Vec2.ts index 24131812a..cf73476ca 100644 --- a/src/math/Vec2.ts +++ b/src/math/Vec2.ts @@ -9,7 +9,7 @@ import { deg2rad, rad2deg, Rect, vec2, type Vec2Args } from "./math"; * @group Components * @subgroup Component Serialization */ -export interface SerializedVec2 { +export interface Vec2Like { x: number; y: number; } @@ -19,7 +19,7 @@ export interface SerializedVec2 { * * @group Math */ -export class Vec2 implements SerializedVec2 { +export class Vec2 implements Vec2Like { /** The x coordinate */ x: number = 0; /** The y coordinate */ @@ -75,7 +75,7 @@ export class Vec2 implements SerializedVec2 { return new Vec2(this.x, this.y); } - static copy(v: SerializedVec2, out: T): T { + static copy(v: Vec2Like, out: T): T { out.x = v.x; out.y = v.y; return out; @@ -87,9 +87,9 @@ export class Vec2 implements SerializedVec2 { return new Vec2(this.x + p2.x, this.y + p2.y); } - static addScaled( - v: SerializedVec2, - other: SerializedVec2, + static addScaled( + v: Vec2Like, + other: Vec2Like, s: number, out: T, ): T { @@ -107,8 +107,8 @@ export class Vec2 implements SerializedVec2 { * * @returns The sum of the vectors */ - static addc( - v: SerializedVec2, + static addc( + v: Vec2Like, x: number, y: number, out: T, @@ -126,9 +126,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The sum of the vectors */ - static add( - v: SerializedVec2, - other: SerializedVec2, + static add( + v: Vec2Like, + other: Vec2Like, out: T, ): T { out.x = v.x + other.x; @@ -151,8 +151,8 @@ export class Vec2 implements SerializedVec2 { * * @returns The difference of the vectors */ - static subc( - v: SerializedVec2, + static subc( + v: Vec2Like, x: number, y: number, out: T, @@ -170,9 +170,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The difference of the vectors */ - static sub( - v: SerializedVec2, - other: SerializedVec2, + static sub( + v: Vec2Like, + other: Vec2Like, out: T, ): T { out.x = v.x - other.x; @@ -195,8 +195,8 @@ export class Vec2 implements SerializedVec2 { * * @returns The scale of the vector */ - static scale( - v: SerializedVec2, + static scale( + v: Vec2Like, s: number, out: T, ): T { @@ -214,8 +214,8 @@ export class Vec2 implements SerializedVec2 { * * @returns The scale of the vector */ - static scalec( - v: SerializedVec2, + static scalec( + v: Vec2Like, x: number, y: number, out: T, @@ -233,9 +233,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The scale of the vector */ - static scalev( - v: SerializedVec2, - other: SerializedVec2, + static scalev( + v: Vec2Like, + other: Vec2Like, out: T, ): T { out.x = v.x * other.x; @@ -262,7 +262,7 @@ export class Vec2 implements SerializedVec2 { * * @returns The between the vectors */ - static dist(v: SerializedVec2, other: SerializedVec2): number { + static dist(v: Vec2Like, other: Vec2Like): number { const x = v.x - other.x; const y = v.y - other.y; return Math.hypot(x, y); @@ -281,7 +281,7 @@ export class Vec2 implements SerializedVec2 { * * @returns The distance between the vectors */ - static sdist(v: SerializedVec2, other: SerializedVec2): number { + static sdist(v: Vec2Like, other: Vec2Like): number { const x = v.x - other.x; const y = v.y - other.y; return x * x + y * y; @@ -302,7 +302,7 @@ export class Vec2 implements SerializedVec2 { * * @returns The length of the vector */ - static len(v: SerializedVec2) { + static len(v: Vec2Like) { return Math.hypot(v.x, v.y); } @@ -321,7 +321,7 @@ export class Vec2 implements SerializedVec2 { * * @returns The squared length of the vector */ - static slen(v: SerializedVec2) { + static slen(v: Vec2Like) { return v.x * v.x + v.y * v.y; } @@ -333,7 +333,7 @@ export class Vec2 implements SerializedVec2 { return len === 0 ? new Vec2(0) : this.scale(1 / len); } - static unit(v: SerializedVec2, out: T): T { + static unit(v: Vec2Like, out: T): T { const len = Vec2.len(v); if (len === 0) { out.x = 0; @@ -352,7 +352,7 @@ export class Vec2 implements SerializedVec2 { return new Vec2(this.y, -this.x); } - static normal(v: SerializedVec2, out: T): T { + static normal(v: Vec2Like, out: T): T { out.x = v.y; out.y = -v.x; return out; @@ -363,7 +363,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - reflect(normal: SerializedVec2) { + reflect(normal: Vec2Like) { const s = 2 * this.dot(normal); return this.sub(normal.x * s, normal.y * s); } @@ -373,7 +373,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - project(on: SerializedVec2) { + project(on: Vec2Like) { const s = this.dot(on) / Vec2.len(on); return new Vec2(on.x * s, on.y * s); } @@ -383,11 +383,11 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - reject(on: SerializedVec2) { + reject(on: Vec2Like) { return this.sub(this.project(on)); } - rotate(vecOrAngle: SerializedVec2 | number) { + rotate(vecOrAngle: Vec2Like | number) { if (typeof vecOrAngle === "object") { return new Vec2( this.x * vecOrAngle.x - this.y * vecOrAngle.y, @@ -413,9 +413,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The rotated vector */ - static rotate( - v: SerializedVec2, - dir: SerializedVec2, + static rotate( + v: Vec2Like, + dir: Vec2Like, out: T, ): T { const tmp = v.x; // save in case v and out are the same object @@ -432,8 +432,8 @@ export class Vec2 implements SerializedVec2 { * * @returns The rotated vector */ - static rotateByAngle( - v: SerializedVec2, + static rotateByAngle( + v: Vec2Like, angle: number, out: T, ): T { @@ -445,7 +445,7 @@ export class Vec2 implements SerializedVec2 { return out; } - invRotate(vecOrAngle: SerializedVec2 | number) { + invRotate(vecOrAngle: Vec2Like | number) { if (typeof vecOrAngle === "object") { return this.rotate(new Vec2(vecOrAngle.x, -vecOrAngle.y)); } @@ -462,9 +462,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The rotated vector */ - static inverseRotate( - v: SerializedVec2, - dir: SerializedVec2, + static inverseRotate( + v: Vec2Like, + dir: Vec2Like, out: T, ): T { const tmp = v.x; // save in case v and out are the same object @@ -476,7 +476,7 @@ export class Vec2 implements SerializedVec2 { /** * Get the dot product with another vector. */ - dot(p2: SerializedVec2): number { + dot(p2: Vec2Like): number { return this.x * p2.x + this.y * p2.y; } @@ -485,7 +485,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - static dot(v: SerializedVec2, other: SerializedVec2): number { + static dot(v: Vec2Like, other: Vec2Like): number { return v.x * other.x + v.y * other.y; } @@ -494,7 +494,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - cross(p2: SerializedVec2): number { + cross(p2: Vec2Like): number { return this.x * p2.y - this.y * p2.x; } @@ -503,7 +503,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - static cross(v: SerializedVec2, other: SerializedVec2): number { + static cross(v: Vec2Like, other: Vec2Like): number { return v.x * other.y - v.y * other.x; } @@ -521,7 +521,7 @@ export class Vec2 implements SerializedVec2 { * * @returns Angle represented by the vector in radians */ - static toAngle(v: SerializedVec2) { + static toAngle(v: Vec2Like) { return Math.atan2(v.y, v.x); } @@ -542,14 +542,14 @@ export class Vec2 implements SerializedVec2 { * * @returns Angle between the vectors in radians */ - static angleBetween(v: SerializedVec2, other: SerializedVec2) { + static angleBetween(v: Vec2Like, other: Vec2Like) { return Math.atan2(Vec2.cross(v, other), Vec2.dot(v, other)); } /** * Linear interpolate to a destination vector (for positions). */ - lerp(dest: SerializedVec2, t: number): Vec2 { + lerp(dest: Vec2Like, t: number): Vec2 { return new Vec2( lerpNumber(this.x, dest.x, t), lerpNumber(this.y, dest.y, t), @@ -565,9 +565,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The linear interpolation between src and dst by t */ - static lerp( - src: SerializedVec2, - dst: SerializedVec2, + static lerp( + src: Vec2Like, + dst: Vec2Like, t: number, out: T, ): T { @@ -581,7 +581,7 @@ export class Vec2 implements SerializedVec2 { * * @since v3000.0 */ - slerp(dest: SerializedVec2, t: number): Vec2 { + slerp(dest: Vec2Like, t: number): Vec2 { const cos = this.dot(dest); const sin = this.cross(dest); const angle = Math.atan2(sin, cos); @@ -601,9 +601,9 @@ export class Vec2 implements SerializedVec2 { * * @returns The spherical interpolation between src and dst by t */ - static slerp( - src: SerializedVec2, - dst: SerializedVec2, + static slerp( + src: Vec2Like, + dst: Vec2Like, t: number, out: T, ): T { @@ -671,11 +671,11 @@ export class Vec2 implements SerializedVec2 { return [this.x, this.y]; } - serialize(): SerializedVec2 { + serialize(): Vec2Like { return { x: this.x, y: this.y }; } - static deserialize(data: SerializedVec2): Vec2 { + static deserialize(data: Vec2Like): Vec2 { return vec2(data.x, data.y); } } diff --git a/src/math/math.ts b/src/math/math.ts index e82fd1214..3e0501789 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -9,7 +9,7 @@ import { clamp } from "./clamp"; import { Color, rgb } from "./color"; import { traceRegion } from "./getImageOutline"; import { lerp, type LerpValue } from "./lerp"; -import { type SerializedVec2, Vec2 } from "./Vec2"; +import { Vec2, type Vec2Like } from "./Vec2"; /** * Possible arguments for a Vec2. @@ -20,8 +20,8 @@ import { type SerializedVec2, Vec2 } from "./Vec2"; export type Vec2Args = | [number, number] // x = args[0] y = args[1] | [number] // x = args[0] y = x - | [number | SerializedVec2] - | [SerializedVec2] // x = args[0].x y = args[0].y + | [number | Vec2Like] + | [Vec2Like] // x = args[0].x y = args[0].y | []; // x = 0 y = 0 export function deg2rad(deg: number): number { From a64aed2c56eb76d2eead6821d14172bf69d5293d Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Tue, 9 Dec 2025 08:40:22 -0500 Subject: [PATCH 6/6] do the comments this way so dprint is happy --- src/math/math.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/math/math.ts b/src/math/math.ts index 3e0501789..d3979880f 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -19,10 +19,10 @@ import { Vec2, type Vec2Like } from "./Vec2"; */ export type Vec2Args = | [number, number] // x = args[0] y = args[1] - | [number] // x = args[0] y = x + | [number] // x = args[0] y = x | [number | Vec2Like] - | [Vec2Like] // x = args[0].x y = args[0].y - | []; // x = 0 y = 0 + | [Vec2Like] // x = args[0].x y = args[0].y + | []; // x = 0 y = 0 export function deg2rad(deg: number): number { return deg * Math.PI / 180;