Moar code that i didnt commit and push like a fucking moron - doing this remotely from orion

This commit is contained in:
2026-05-02 19:50:47 +02:00
parent 1f23f54ee4
commit f50fc96416
9 changed files with 321 additions and 154 deletions
+3
View File
@@ -0,0 +1,3 @@
export namespace Physis {
export class Engine {}
}
+6 -7
View File
@@ -1,24 +1,23 @@
import Vector2 from "$lib/vector";
import Rigidbody2D from "../rigidbody/rigidbody2d";
import Vector2 from '$lib/vector';
import Rigidbody2D from '../rigidbody/rigidbody2d';
class AABB {
export class AABB {
private size: Vector2 = new Vector2();
private halfSize: Vector2 = new Vector2();
private rigidbody: Rigidbody2D | null = null;
constructor(min?: Vector2, max?: Vector2) {
if(min !== undefined && max !== undefined)
this.size = new Vector2(max).sub(min);
if (min !== undefined && max !== undefined) this.size = new Vector2(max).sub(min);
this.halfSize = new Vector2(this.size.mul(0.5));
}
public get getMin(): Vector2 {
return new Vector2(this.rigidbody.getPosition).sub(this.halfSize);
return new Vector2(this.rigidbody.Position).sub(this.halfSize);
}
public get getMax(): Vector2 {
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
return new Vector2(this.rigidbody.Position).add(this.halfSize);
}
}
@@ -1,25 +1,24 @@
import Vector2 from "$lib/vector"
import type Rigidbody2D from "../rigidbody/rigidbody2d";
import Vector2 from '$lib/vector';
import type Rigidbody2D from '../rigidbody/rigidbody2d';
class Box2D {
export class Box2D {
private size: Vector2 = new Vector2();
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);
if (min !== undefined && max !== undefined) this.size = new Vector2(max).sub(min);
this.halfSize = new Vector2(this.size.mul(0.5));
}
public get getMin(): Vector2 {
return new Vector2(this.rigidbody.getPosition).sub(this.halfSize);
return new Vector2(this.rigidbody.Position).sub(this.halfSize);
}
public get getMax(): Vector2 {
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
return new Vector2(this.rigidbody.Position).add(this.halfSize);
}
public get getVectices(): Vector2[] {
@@ -33,8 +32,8 @@ class Box2D {
new Vector2(max.x, max.y)
];
if(this.rigidbody.getRotation != 0.00) {
for(const vert of vertices) {
if (this.rigidbody.Rotation != 0.0) {
for (const vert of vertices) {
//TODO Rotate Vert around this.rigidbody.getPosition by this.rigidbody.getRotation;
}
}
@@ -0,0 +1,17 @@
import type Vector2 from '$lib/vector';
import type Rigidbody2D from '../rigidbody/rigidbody2d';
class Circle {
private radius: number = 1.0;
private rigidbody: Rigidbody2D;
public get Radius(): number {
return this.radius;
}
public get Center(): Vector2 {
return this.rigidbody.Position;
}
}
export default Circle;
@@ -0,0 +1,22 @@
import type Vector2 from '$lib/vector';
class Ray2D {
private origin: Vector2;
private direction: Vector2;
constructor(origin: Vector2, direction: Vector2) {
this.origin = origin;
this.direction = direction;
this.direction.normalize();
}
public get Origin(): Vector2 {
return this.origin;
}
public get Direction(): Vector2 {
return this.direction;
}
}
export default Ray2D;
@@ -0,0 +1,26 @@
import Vector2 from '$lib/vector';
class RaycastResult {
private point: Vector2 = new Vector2();
private normal: Vector2 = new Vector2();
private t: number = -1;
private hit: boolean = false;
public init(point: Vector2, normal: Vector2, t: number, hit: boolean) {
this.point.set(point.x, point.y);
this.normal.set(normal.x, normal.y);
this.t = t;
this.hit = hit;
}
public static reset(result: RaycastResult) {
if (result != null) {
result.point.set(Vector2.zero.x, Vector2.zero.y);
result.normal.set(Vector2.zero.x, Vector2.zero.y);
result.t = -1;
result.hit = false;
}
}
}
export default RaycastResult;
@@ -1,72 +1,100 @@
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";
import Line2D from '$lib/shimeji/renderer/line2d';
import Vector2 from '$lib/vector';
import VMath from '$lib/vmath';
import AABB from '../primitives/aabb';
import Box2D from '../primitives/box2d';
import Circle from '../primitives/circle';
import type Ray2D from '../primitives/ray2d';
import RaycastResult from '../primitives/raycast_result';
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 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);
let b = line.getEnd().y - m * line.getEnd().x;
//Check the line equation
return point.y == m * point.x + b;
}
public static pointInCircle(point: Vector2, circle: Circle) {
const circleCenter: Vector2 = circle.Center;
const centerToPoint: Vector2 = new Vector2(point).sub(circleCenter);
return centerToPoint.sqrLength <= circle.Radius * circle.Radius;
}
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;
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);
VMath.rotate(pointLocal, box.getRigidbody.Rotation, box.getRigidbody.Position);
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;
return (
pointLocal.x <= max.x &&
pointLocal.x >= min.x &&
pointLocal.y >= min.y &&
pointLocal.y <= max.y
);
}
public static lineAndCircle(line: Line2D, circle: Circle): boolean {
if (this.pointInCircle(line.getStart(), circle) || this.pointInCircle(line.getEnd(), circle))
return true;
const ab: Vector2 = new Vector2(line.getEnd()).sub(line.getStart());
const circleCenter: Vector2 = circle.Center;
const centerToLineStart: Vector2 = new Vector2(circleCenter).sub(line.getStart());
const t: number = centerToLineStart.dot(ab) / ab.dot(ab);
if (t < 0.0 || t > 1.0) return false;
const closestPoint: Vector2 = new Vector2(line.getStart()).add(ab.mul(t));
return this.pointInCircle(closestPoint, circle);
}
public static lineAndAABB(line: Line2D, box: AABB): boolean {
if(this.pointInAABB(line.getStart, box) || this.pointInAABB(line.getEnd, box))
return true;
if (this.pointInAABB(line.getStart(), box) || this.pointInAABB(line.getEnd(), box)) return true;
let uVec: Vector2 = new Vector2(line.getEnd).sub(line.getStart);
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;
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);
min.sub(line.getStart()).mul(uVec);
const max: Vector2 = box.getMax;
max.sub(line.getStart).mul(uVec);
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) {
if (tmax < 0 || tmin > tmax) {
return false;
}
const t = (tmin < 0) ? tmax : tmin;
return t > 0 && t * t < line.lengthSquared();
const t = tmin < 0 ? tmax : tmin;
return t > 0 && t * t < line.lengthSqr();
}
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);
const rotation: number = box.getRigidbody.Rotation;
const center: Vector2 = box.getRigidbody.Position;
const localStart: Vector2 = new Vector2(line.getStart());
const localEnd: Vector2 = new Vector2(line.getEnd());
VMath.rotate(localStart, rotation, center);
VMath.rotate(localEnd, rotation, center);
@@ -76,4 +104,80 @@ class IntersectionDetector2D {
return this.lineAndAABB(localLine, aabb);
}
public static raycast(circle: Circle, ray: Ray2D, result: RaycastResult): boolean;
public static raycast(aabb: AABB, ray: Ray2D, result: RaycastResult): boolean;
public static raycast(box: Box2D, ray: Ray2D, result: RaycastResult): boolean;
public static raycast(
primitive: Circle | AABB | Box2D,
ray: Ray2D,
result: RaycastResult
): boolean {
if (primitive instanceof Circle) {
RaycastResult.reset(result);
const originToCircle: Vector2 = new Vector2(primitive.Center).sub(ray.Origin);
const radiusSqr: number = primitive.Radius * primitive.Radius;
const originToCircleSqr: number = originToCircle.sqrLength;
const a: number = originToCircle.dot(ray.Direction);
const bSqr: number = originToCircleSqr - a * a;
if (radiusSqr - bSqr < 0.0) return false;
const f: number = Math.sqrt(radiusSqr - bSqr);
let t: number = 0;
if (originToCircleSqr < radiusSqr) {
t = a + f;
} else {
t = a - f;
}
if (result != null) {
const point: Vector2 = new Vector2(ray.Origin).add(new Vector2(ray.Direction)).mul(t);
const normal: Vector2 = new Vector2(point).sub(primitive.Center);
normal.normalize();
result.init(point, normal, t, true);
}
return true;
} else if (primitive instanceof AABB) {
RaycastResult.reset(result);
let uVec: Vector2 = ray.Direction;
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 = primitive.getMin;
min.sub(ray.Origin).mul(uVec);
const max: Vector2 = primitive.getMax;
max.sub(ray.Origin).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;
const hit: boolean = t > 0; //&& t * t < ray.Distance;
if (!hit) return false;
if (result != null) {
const point: Vector2 = new Vector2(ray.Origin).add(new Vector2(ray.Direction).mul(t));
const normal: Vector2 = new Vector2(ray.Origin).sub(point);
normal.normalize();
result.init(point, normal, t, true);
}
return true;
} else {
//TODO: Box2D implementation
return true;
}
}
}
@@ -1,10 +1,10 @@
import Vector2 from "$lib/vector";
import Vector2 from '$lib/vector';
class Rigidbody2D {
private position: Vector2 = new Vector2();
private rotation: number = 0.0;
public get getPosition(): Vector2 {
public get Position(): Vector2 {
return this.position;
}
@@ -12,14 +12,13 @@ class Rigidbody2D {
this.position = position;
}
public get getRotation(): number {
public get Rotation(): number {
return this.rotation;
}
public set setRotation(rotation: number) {
this.rotation = rotation
this.rotation = rotation;
}
}
export default Rigidbody2D;
+5 -7
View File
@@ -1,27 +1,25 @@
import Vector2 from "$lib/vector";
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 {
public getStart(): Vector2 {
return this.from;
}
public get getEnd(): Vector2 {
public getEnd(): Vector2 {
return this.to;
}
public lengthSquared(): number {
return new Vector2(this.to).sub(this.from).sqrLength
public lengthSqr(): number {
return new Vector2(this.to).sub(this.from).sqrLength;
}
}
export default Line2D;