WIP
This commit is contained in:
+3
-19
@@ -1,4 +1,4 @@
|
|||||||
import { createVector, type Vector } from "#/engine/Vector.ts";
|
import { createVector, setEdgeNormal, type Vector } from "#/engine/Vector.ts";
|
||||||
|
|
||||||
export type Polygon = {
|
export type Polygon = {
|
||||||
position: Vector;
|
position: Vector;
|
||||||
@@ -24,25 +24,9 @@ export function updateAxes({ points, axes }: Polygon): void {
|
|||||||
|
|
||||||
for (let i = 0; i < cap; i++) {
|
for (let i = 0; i < cap; i++) {
|
||||||
const pj = points[i + 1];
|
const pj = points[i + 1];
|
||||||
|
setEdgeNormal(axes[i], pi, pj);
|
||||||
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;
|
|
||||||
|
|
||||||
pi = pj;
|
pi = pj;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nx = -(p0.y - pi.y);
|
setEdgeNormal(axes[cap], pi, p0);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
export function getDotProduct(a: Vector, b: Vector): number {
|
||||||
return a.x * b.x + a.y * b.y;
|
return a.x * b.x + a.y * b.y;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,33 @@
|
|||||||
|
import type { Circle } from "#/engine/Circle.ts";
|
||||||
import $ from "#/engine/Scene.ts";
|
import $ from "#/engine/Scene.ts";
|
||||||
import type { Vector } from "#/engine/Vector.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<T> = {
|
||||||
|
shape: Shape;
|
||||||
|
body: T;
|
||||||
|
};
|
||||||
|
|
||||||
export type Collision = {};
|
export type Collision = {};
|
||||||
|
|
||||||
export function moveAndCollide(
|
export function moveAndCollide<T>(
|
||||||
collider: Collider,
|
collider: Collider<T>,
|
||||||
velocity: Vector,
|
velocity: Vector,
|
||||||
delta: number,
|
delta: number,
|
||||||
): Collision {}
|
): Collision {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
export function processPhysics(
|
export function processPhysics(
|
||||||
ctx: CanvasRenderingContext2D,
|
ctx: CanvasRenderingContext2D,
|
||||||
delta: number,
|
delta: number,
|
||||||
): void {
|
): void {
|
||||||
for (const [id, collider] of $<Collider>("collider")!.entries()) {
|
for (const [id, collider] of $<Collider<unknown>>("collider")!.entries()) {
|
||||||
const position = $<Vector>("position")!.get(id);
|
const position = $<Vector>("position")!.get(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user