From 5c413f4e8b3bc30edb845abbb1edc44d32dedf80 Mon Sep 17 00:00:00 2001 From: orion Date: Fri, 27 Mar 2026 01:43:35 +0100 Subject: [PATCH] moooooore physics --- src/lib/shimeji/physics2d/primitives/aabb.ts | 7 +- src/lib/shimeji/physics2d/primitives/box2d.ts | 36 ++++++++- .../physics2d/primitives/collider2d.ts | 2 +- .../rigidbody/intersectiondetector2d.ts | 79 +++++++++++++++++++ .../physics2d/rigidbody/rigidbody2d.ts | 2 +- src/lib/shimeji/renderer/line2d.ts | 27 +++++++ src/lib/stores/shimeji.ts | 6 +- src/lib/stores/shimeji2.ts | 2 +- src/lib/{stores => }/vector.ts | 6 -- src/lib/vmath.ts | 40 ++++++++++ 10 files changed, 189 insertions(+), 18 deletions(-) create mode 100644 src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts create mode 100644 src/lib/shimeji/renderer/line2d.ts rename src/lib/{stores => }/vector.ts (97%) create mode 100644 src/lib/vmath.ts diff --git a/src/lib/shimeji/physics2d/primitives/aabb.ts b/src/lib/shimeji/physics2d/primitives/aabb.ts index 0ffe0dd..4d15e15 100644 --- a/src/lib/shimeji/physics2d/primitives/aabb.ts +++ b/src/lib/shimeji/physics2d/primitives/aabb.ts @@ -1,4 +1,4 @@ -import Vector2 from "$lib/stores/vector"; +import Vector2 from "$lib/vector"; import Rigidbody2D from "../rigidbody/rigidbody2d"; class AABB { @@ -6,7 +6,6 @@ class AABB { private halfSize: Vector2 = new Vector2(); private rigidbody: Rigidbody2D | null = null; - constructor(); constructor(min?: Vector2, max?: Vector2) { if(min !== undefined && max !== undefined) this.size = new Vector2(max).sub(min); @@ -21,4 +20,6 @@ class AABB { public get getMax(): Vector2 { return new Vector2(this.rigidbody.getPosition).add(this.halfSize); } -} \ No newline at end of file +} + +export default AABB; \ No newline at end of file diff --git a/src/lib/shimeji/physics2d/primitives/box2d.ts b/src/lib/shimeji/physics2d/primitives/box2d.ts index 6cceee5..54f2104 100644 --- a/src/lib/shimeji/physics2d/primitives/box2d.ts +++ b/src/lib/shimeji/physics2d/primitives/box2d.ts @@ -1,4 +1,4 @@ -import Vector2 from "$lib/stores/vector" +import Vector2 from "$lib/vector" import type Rigidbody2D from "../rigidbody/rigidbody2d"; class Box2D { @@ -15,6 +15,36 @@ class Box2D { } public get getMin(): Vector2 { - + return new Vector2(this.rigidbody.getPosition).sub(this.halfSize); } -} \ No newline at end of file + + public get getMax(): Vector2 { + return new Vector2(this.rigidbody.getPosition).add(this.halfSize); + } + + public get getVectices(): Vector2[] { + const min: Vector2 = this.getMin; + const max: Vector2 = this.getMax; + + const vertices: Vector2[] = [ + new Vector2(min.x, min.y), + new Vector2(min.x, max.y), + new Vector2(max.x, min.y), + new Vector2(max.x, max.y) + ]; + + if(this.rigidbody.getRotation != 0.00) { + for(const vert of vertices) { + //TODO Rotate Vert around this.rigidbody.getPosition by this.rigidbody.getRotation; + } + } + + return vertices; + } + + public get getRigidbody(): Rigidbody2D { + return this.rigidbody; + } +} + +export default Box2D; \ No newline at end of file diff --git a/src/lib/shimeji/physics2d/primitives/collider2d.ts b/src/lib/shimeji/physics2d/primitives/collider2d.ts index 4f06243..277144e 100644 --- a/src/lib/shimeji/physics2d/primitives/collider2d.ts +++ b/src/lib/shimeji/physics2d/primitives/collider2d.ts @@ -1,4 +1,4 @@ -import Vector2 from "$lib/stores/vector"; +import Vector2 from "$lib/vector"; class Collider2D { protected offset: Vector2 = new Vector2(); diff --git a/src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts b/src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts new file mode 100644 index 0000000..d2409f1 --- /dev/null +++ b/src/lib/shimeji/physics2d/rigidbody/intersectiondetector2d.ts @@ -0,0 +1,79 @@ +import Line2D from "$lib/shimeji/renderer/line2d"; +import Vector2 from "$lib/vector"; +import VMath from "$lib/vmath"; +import AABB from "../primitives/aabb"; +import type Box2D from "../primitives/box2d"; + +class IntersectionDetector2D { + + public static pointOnLine(point: Vector2, line: Line2D): boolean { + let dy: number = line.getEnd.y - line.getStart.y; + let dx: number = line.getEnd.x - line.getStart.x; + let m: number = dy / dx; + + let b = line.getEnd.y - (m * line.getEnd.x); + + //Check the line equation + return point.y == m * point.x + b; + } + + public static pointInAABB(point: Vector2, aabb: AABB): boolean { + let min: Vector2 = aabb.getMin; + let max: Vector2 = aabb.getMax; + + return point.x <= max.x && point.x >= min.x && + point.y >= min.y && point.y <= max.y; + } + + public static pointInBox2D(point: Vector2, box: Box2D): boolean { + const pointLocal = new Vector2(point); + VMath.rotate(pointLocal, box.getRigidbody.getRotation, box.getRigidbody.getPosition); + + let min: Vector2 = box.getMin; + let max: Vector2 = box.getMax; + + return pointLocal.x <= max.x && pointLocal.x >= min.x && + pointLocal.y >= min.y && pointLocal.y <= max.y; + } + + public static lineAndAABB(line: Line2D, box: AABB): boolean { + if(this.pointInAABB(line.getStart, box) || this.pointInAABB(line.getEnd, box)) + return true; + + let uVec: Vector2 = new Vector2(line.getEnd).sub(line.getStart); + uVec.normalize; + + uVec.x = (!uVec.equals(Vector2.zero)) ? 1.0 / uVec.x : 0.0; + uVec.y = (!uVec.equals(Vector2.zero)) ? 1.0 / uVec.y : 0.0; + + const min: Vector2 = box.getMin; + min.sub(line.getStart).mul(uVec); + const max: Vector2 = box.getMax; + max.sub(line.getStart).mul(uVec); + + const tmin: number = Math.max(Math.min(min.x, max.x), Math.min(min.y, max.y)); + const tmax: number = Math.min(Math.max(min.x, max.x), Math.max(min.y, max.y)); + + if(tmax < 0 || tmin > tmax) { + return false; + } + + const t = (tmin < 0) ? tmax : tmin; + return t > 0 && t * t < line.lengthSquared(); + } + + public static lineAndBox2D(line: Line2D, box: Box2D): boolean { + const rotation: number = box.getRigidbody.getRotation; + const center: Vector2 = box.getRigidbody.getPosition; + const localStart: Vector2 = new Vector2(line.getStart); + const localEnd: Vector2 = new Vector2(line.getEnd); + + VMath.rotate(localStart, rotation, center); + VMath.rotate(localEnd, rotation, center); + + const localLine: Line2D = new Line2D(localStart, localEnd); + const aabb: AABB = new AABB(box.getMin, box.getMax); + + return this.lineAndAABB(localLine, aabb); + } +} \ No newline at end of file diff --git a/src/lib/shimeji/physics2d/rigidbody/rigidbody2d.ts b/src/lib/shimeji/physics2d/rigidbody/rigidbody2d.ts index 6362b01..562c021 100644 --- a/src/lib/shimeji/physics2d/rigidbody/rigidbody2d.ts +++ b/src/lib/shimeji/physics2d/rigidbody/rigidbody2d.ts @@ -1,4 +1,4 @@ -import Vector2 from "$lib/stores/vector"; +import Vector2 from "$lib/vector"; class Rigidbody2D { private position: Vector2 = new Vector2(); diff --git a/src/lib/shimeji/renderer/line2d.ts b/src/lib/shimeji/renderer/line2d.ts new file mode 100644 index 0000000..f9524d9 --- /dev/null +++ b/src/lib/shimeji/renderer/line2d.ts @@ -0,0 +1,27 @@ +import Vector2 from "$lib/vector"; + +class Line2D { + private from: Vector2; + private to: Vector2; + + + constructor(from: Vector2, to: Vector2) { + this.from = from; + this.to = to; + } + + public get getStart(): Vector2 { + return this.from; + } + + public get getEnd(): Vector2 { + return this.to; + } + + public lengthSquared(): number { + return new Vector2(this.to).sub(this.from).sqrLength + } + +} + +export default Line2D; \ No newline at end of file diff --git a/src/lib/stores/shimeji.ts b/src/lib/stores/shimeji.ts index b1d7690..f40fa0f 100644 --- a/src/lib/stores/shimeji.ts +++ b/src/lib/stores/shimeji.ts @@ -1,4 +1,4 @@ -import Vector2 from "./vector"; +import Vector2 from "../vector"; export class Shimeji { private element: HTMLElement; @@ -19,9 +19,9 @@ export class Shimeji { private readonly TIMESCALE: number = 1; private readonly RESTITUTION: number = 0.4; - private readonly CURSOR_STRENGTH: number = 250; + private readonly CURSOR_STRENGTH: number = 500; private readonly DAMPING: number = 1; - private readonly MASS: number = 0.1; + private readonly MASS: number = 1; private readonly FRICTION: number = 0.9; private readonly GRAVITY: number = 50; diff --git a/src/lib/stores/shimeji2.ts b/src/lib/stores/shimeji2.ts index 981a96e..3c0dc08 100644 --- a/src/lib/stores/shimeji2.ts +++ b/src/lib/stores/shimeji2.ts @@ -1,4 +1,4 @@ -import Vector2 from "./vector"; +import Vector2 from "../vector"; export class Shimeji { private element: HTMLElement; diff --git a/src/lib/stores/vector.ts b/src/lib/vector.ts similarity index 97% rename from src/lib/stores/vector.ts rename to src/lib/vector.ts index a67eef0..effffd6 100644 --- a/src/lib/stores/vector.ts +++ b/src/lib/vector.ts @@ -159,12 +159,6 @@ class Vector2 { return this; } - rotate(angle: number): Vector2 { - const cos = Math.cos(angle); - const sin = Math.sin(angle); - return new Vector2(this.x * cos - this.y * sin, this.x * sin + this.y * cos); - } - get perpendicular(): Vector2 { return new Vector2(-this.y, this.x); } diff --git a/src/lib/vmath.ts b/src/lib/vmath.ts new file mode 100644 index 0000000..aa173d8 --- /dev/null +++ b/src/lib/vmath.ts @@ -0,0 +1,40 @@ +import Vector2 from "./vector"; + +class VMath { + + public static toRadians(angle: number): number { + return angle * (Math.PI / 180); + } + + public static rotate(vec: Vector2, angle: number, origin: Vector2): void { + const x: number = vec.x - origin.x; + const y: number = vec.y - origin.y; + + const cos: number = Math.cos(this.toRadians(angle)); + const sin: number = Math.sin(this.toRadians(angle)); + + let xPrim: number = (x * cos) - (y * sin); + let yPrim: number = (x * sin) + (y * cos); + xPrim += origin.x; + yPrim += origin.y; + + vec.set(xPrim, yPrim); + } + + public static compare(x: number, y: number): boolean; + public static compare(vec1: Vector2, vec2: Vector2): boolean; + public static compare(arg1: Vector2 | number, arg2: Vector2 | number): boolean { + if(typeof arg1 === 'number' && typeof arg2 === 'number') + return Math.abs(arg1 - arg2) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1), Math.abs(arg2)); + else if (typeof arg1 !== 'number' && typeof arg2 !== 'number') + return Math.abs(arg1.x - arg2.x) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1.x), Math.abs(arg2.x)) && + Math.abs(arg1.y - arg2.y) <= Number.MIN_VALUE * Math.max(1, Math.abs(arg1.y), Math.abs(arg2.y)) + else { + throw new Error('unexpected usage of compare method in vmath library'); + } + } + + +} + +export default VMath; \ No newline at end of file