diff --git a/engine/Polygon.ts b/engine/Polygon.ts index 880dd7c..3a64d96 100644 --- a/engine/Polygon.ts +++ b/engine/Polygon.ts @@ -1,4 +1,4 @@ -import { createVector, type Vector } from "#/engine/Vector.ts"; +import { createVector, setEdgeNormal, type Vector } from "#/engine/Vector.ts"; export type Polygon = { position: Vector; @@ -24,25 +24,9 @@ export function updateAxes({ points, axes }: Polygon): void { for (let i = 0; i < cap; i++) { const pj = points[i + 1]; - - const nx = -(pj.y - pi.y); - const ny = pj.x - pi.x; - - const invLen = 1.0 / Math.sqrt(nx * nx + ny * ny); - - const axis = axes[i]; - axis.x = nx * invLen; - axis.y = ny * invLen; - + setEdgeNormal(axes[i], pi, pj); pi = pj; } - const nx = -(p0.y - pi.y); - const ny = p0.x - pi.x; - - const invLen = 1.0 / Math.sqrt(nx * nx + ny * ny); - - const axis = axes[cap]; - axis.x = nx * invLen; - axis.y = ny * invLen; + setEdgeNormal(axes[cap], pi, p0); } diff --git a/engine/Vector.ts b/engine/Vector.ts index 3235880..5fe37d6 100644 --- a/engine/Vector.ts +++ b/engine/Vector.ts @@ -58,6 +58,15 @@ export function setDistanceNormal(normal: Vector, a: Vector, b: Vector): void { } } +export function setEdgeNormal(normal: Vector, a: Vector, b: Vector): void { + const nx = -(b.y - a.y); + const ny = b.x - a.x; + const inv = 1.0 / Math.sqrt(nx * nx + ny * ny); + + normal.x = nx * inv; + normal.y = ny * inv; +} + export function getDotProduct(a: Vector, b: Vector): number { return a.x * b.x + a.y * b.y; } diff --git a/engine/system/processPhysics.ts b/engine/system/processPhysics.ts index 0afa818..2ad4565 100644 --- a/engine/system/processPhysics.ts +++ b/engine/system/processPhysics.ts @@ -1,21 +1,33 @@ +import type { Circle } from "#/engine/Circle.ts"; import $ from "#/engine/Scene.ts"; import type { Vector } from "#/engine/Vector.ts"; -export type Collider = {}; +const CIRCLE = 0; +const RECT = 1; +const POLYGON = 2; + +type Shape = 0 | 1 | 2; + +export type Collider = { + shape: Shape; + body: T; +}; export type Collision = {}; -export function moveAndCollide( - collider: Collider, +export function moveAndCollide( + collider: Collider, velocity: Vector, delta: number, -): Collision {} +): Collision { + return {}; +} export function processPhysics( ctx: CanvasRenderingContext2D, delta: number, ): void { - for (const [id, collider] of $("collider")!.entries()) { + for (const [id, collider] of $>("collider")!.entries()) { const position = $("position")!.get(id); } }