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 = {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<T> = {
|
||||
shape: Shape;
|
||||
body: T;
|
||||
};
|
||||
|
||||
export type Collision = {};
|
||||
|
||||
export function moveAndCollide(
|
||||
collider: Collider,
|
||||
export function moveAndCollide<T>(
|
||||
collider: Collider<T>,
|
||||
velocity: Vector,
|
||||
delta: number,
|
||||
): Collision {}
|
||||
): Collision {
|
||||
return {};
|
||||
}
|
||||
|
||||
export function processPhysics(
|
||||
ctx: CanvasRenderingContext2D,
|
||||
delta: number,
|
||||
): void {
|
||||
for (const [id, collider] of $<Collider>("collider")!.entries()) {
|
||||
for (const [id, collider] of $<Collider<unknown>>("collider")!.entries()) {
|
||||
const position = $<Vector>("position")!.get(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user