Moar code that i didnt commit and push like a fucking moron - doing this remotely from orion
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export namespace Physis {
|
||||
export class Engine {}
|
||||
}
|
||||
@@ -1,25 +1,24 @@
|
||||
import Vector2 from "$lib/vector";
|
||||
import Rigidbody2D from "../rigidbody/rigidbody2d";
|
||||
import Vector2 from '$lib/vector';
|
||||
import Rigidbody2D from '../rigidbody/rigidbody2d';
|
||||
|
||||
class AABB {
|
||||
private size: Vector2 = new Vector2();
|
||||
private halfSize: Vector2 = new Vector2();
|
||||
private rigidbody: Rigidbody2D | null = null;
|
||||
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);
|
||||
constructor(min?: Vector2, max?: Vector2) {
|
||||
if (min !== undefined && max !== undefined) this.size = new Vector2(max).sub(min);
|
||||
|
||||
this.halfSize = new Vector2(this.size.mul(0.5));
|
||||
}
|
||||
this.halfSize = new Vector2(this.size.mul(0.5));
|
||||
}
|
||||
|
||||
public get getMin(): Vector2 {
|
||||
return new Vector2(this.rigidbody.getPosition).sub(this.halfSize);
|
||||
}
|
||||
public get getMin(): Vector2 {
|
||||
return new Vector2(this.rigidbody.Position).sub(this.halfSize);
|
||||
}
|
||||
|
||||
public get getMax(): Vector2 {
|
||||
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
|
||||
}
|
||||
public get getMax(): Vector2 {
|
||||
return new Vector2(this.rigidbody.Position).add(this.halfSize);
|
||||
}
|
||||
}
|
||||
|
||||
export default AABB;
|
||||
@@ -1,50 +1,49 @@
|
||||
import Vector2 from "$lib/vector"
|
||||
import type Rigidbody2D from "../rigidbody/rigidbody2d";
|
||||
import Vector2 from '$lib/vector';
|
||||
import type Rigidbody2D from '../rigidbody/rigidbody2d';
|
||||
|
||||
class Box2D {
|
||||
private size: Vector2 = new Vector2();
|
||||
private halfSize: Vector2 = new Vector2();
|
||||
private rigidbody: Rigidbody2D | null = null;
|
||||
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);
|
||||
constructor();
|
||||
constructor(min?: Vector2, max?: Vector2) {
|
||||
if (min !== undefined && max !== undefined) this.size = new Vector2(max).sub(min);
|
||||
|
||||
this.halfSize = new Vector2(this.size.mul(0.5));
|
||||
}
|
||||
this.halfSize = new Vector2(this.size.mul(0.5));
|
||||
}
|
||||
|
||||
public get getMin(): Vector2 {
|
||||
return new Vector2(this.rigidbody.getPosition).sub(this.halfSize);
|
||||
}
|
||||
public get getMin(): Vector2 {
|
||||
return new Vector2(this.rigidbody.Position).sub(this.halfSize);
|
||||
}
|
||||
|
||||
public get getMax(): Vector2 {
|
||||
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
|
||||
}
|
||||
public get getMax(): Vector2 {
|
||||
return new Vector2(this.rigidbody.Position).add(this.halfSize);
|
||||
}
|
||||
|
||||
public get getVectices(): Vector2[] {
|
||||
const min: Vector2 = this.getMin;
|
||||
const max: Vector2 = this.getMax;
|
||||
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)
|
||||
];
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (this.rigidbody.Rotation != 0.0) {
|
||||
for (const vert of vertices) {
|
||||
//TODO Rotate Vert around this.rigidbody.getPosition by this.rigidbody.getRotation;
|
||||
}
|
||||
}
|
||||
|
||||
return vertices;
|
||||
}
|
||||
return vertices;
|
||||
}
|
||||
|
||||
public get getRigidbody(): Rigidbody2D {
|
||||
return this.rigidbody;
|
||||
}
|
||||
public get getRigidbody(): Rigidbody2D {
|
||||
return this.rigidbody;
|
||||
}
|
||||
}
|
||||
|
||||
export default Box2D;
|
||||
@@ -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,79 +1,183 @@
|
||||
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 m: number = dy / dx;
|
||||
|
||||
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;
|
||||
|
||||
let b = line.getEnd.y - (m * line.getEnd.x);
|
||||
//Check the line equation
|
||||
return point.y == m * point.x + b;
|
||||
}
|
||||
|
||||
//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);
|
||||
|
||||
public static pointInAABB(point: Vector2, aabb: AABB): boolean {
|
||||
let min: Vector2 = aabb.getMin;
|
||||
let max: Vector2 = aabb.getMax;
|
||||
return centerToPoint.sqrLength <= circle.Radius * circle.Radius;
|
||||
}
|
||||
|
||||
return point.x <= max.x && point.x >= min.x &&
|
||||
point.y >= min.y && point.y <= max.y;
|
||||
}
|
||||
public static pointInAABB(point: Vector2, aabb: AABB): boolean {
|
||||
let min: Vector2 = aabb.getMin;
|
||||
let max: Vector2 = aabb.getMax;
|
||||
|
||||
public static pointInBox2D(point: Vector2, box: Box2D): boolean {
|
||||
const pointLocal = new Vector2(point);
|
||||
VMath.rotate(pointLocal, box.getRigidbody.getRotation, box.getRigidbody.getPosition);
|
||||
return point.x <= max.x && point.x >= min.x && point.y >= min.y && point.y <= max.y;
|
||||
}
|
||||
|
||||
let min: Vector2 = box.getMin;
|
||||
let max: Vector2 = box.getMax;
|
||||
public static pointInBox2D(point: Vector2, box: Box2D): boolean {
|
||||
const pointLocal = new Vector2(point);
|
||||
VMath.rotate(pointLocal, box.getRigidbody.Rotation, box.getRigidbody.Position);
|
||||
|
||||
return pointLocal.x <= max.x && pointLocal.x >= min.x &&
|
||||
pointLocal.y >= min.y && pointLocal.y <= max.y;
|
||||
}
|
||||
let min: Vector2 = box.getMin;
|
||||
let max: Vector2 = box.getMax;
|
||||
|
||||
public static lineAndAABB(line: Line2D, box: AABB): boolean {
|
||||
if(this.pointInAABB(line.getStart, box) || this.pointInAABB(line.getEnd, box))
|
||||
return true;
|
||||
return (
|
||||
pointLocal.x <= max.x &&
|
||||
pointLocal.x >= min.x &&
|
||||
pointLocal.y >= min.y &&
|
||||
pointLocal.y <= max.y
|
||||
);
|
||||
}
|
||||
|
||||
let uVec: Vector2 = new Vector2(line.getEnd).sub(line.getStart);
|
||||
uVec.normalize;
|
||||
public static lineAndCircle(line: Line2D, circle: Circle): boolean {
|
||||
if (this.pointInCircle(line.getStart(), circle) || this.pointInCircle(line.getEnd(), circle))
|
||||
return true;
|
||||
|
||||
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 ab: Vector2 = new Vector2(line.getEnd()).sub(line.getStart());
|
||||
|
||||
const min: Vector2 = box.getMin;
|
||||
min.sub(line.getStart).mul(uVec);
|
||||
const max: Vector2 = box.getMax;
|
||||
max.sub(line.getStart).mul(uVec);
|
||||
const circleCenter: Vector2 = circle.Center;
|
||||
const centerToLineStart: Vector2 = new Vector2(circleCenter).sub(line.getStart());
|
||||
const t: number = centerToLineStart.dot(ab) / ab.dot(ab);
|
||||
|
||||
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 (t < 0.0 || t > 1.0) return false;
|
||||
|
||||
if(tmax < 0 || tmin > tmax) {
|
||||
return false;
|
||||
}
|
||||
const closestPoint: Vector2 = new Vector2(line.getStart()).add(ab.mul(t));
|
||||
|
||||
const t = (tmin < 0) ? tmax : tmin;
|
||||
return t > 0 && t * t < line.lengthSquared();
|
||||
}
|
||||
return this.pointInCircle(closestPoint, circle);
|
||||
}
|
||||
|
||||
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);
|
||||
public static lineAndAABB(line: Line2D, box: AABB): boolean {
|
||||
if (this.pointInAABB(line.getStart(), box) || this.pointInAABB(line.getEnd(), box)) return true;
|
||||
|
||||
VMath.rotate(localStart, rotation, center);
|
||||
VMath.rotate(localEnd, rotation, center);
|
||||
let uVec: Vector2 = new Vector2(line.getEnd()).sub(line.getStart());
|
||||
uVec.normalize;
|
||||
|
||||
const localLine: Line2D = new Line2D(localStart, localEnd);
|
||||
const aabb: AABB = new AABB(box.getMin, box.getMax);
|
||||
uVec.x = !uVec.equals(Vector2.zero) ? 1.0 / uVec.x : 0.0;
|
||||
uVec.y = !uVec.equals(Vector2.zero) ? 1.0 / uVec.y : 0.0;
|
||||
|
||||
return this.lineAndAABB(localLine, aabb);
|
||||
}
|
||||
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.lengthSqr();
|
||||
}
|
||||
|
||||
public static lineAndBox2D(line: Line2D, box: Box2D): boolean {
|
||||
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);
|
||||
|
||||
const localLine: Line2D = new Line2D(localStart, localEnd);
|
||||
const aabb: AABB = new AABB(box.getMin, box.getMax);
|
||||
|
||||
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,25 +1,24 @@
|
||||
import Vector2 from "$lib/vector";
|
||||
import Vector2 from '$lib/vector';
|
||||
|
||||
class Rigidbody2D {
|
||||
private position: Vector2 = new Vector2();
|
||||
private rotation: number = 0.0;
|
||||
private position: Vector2 = new Vector2();
|
||||
private rotation: number = 0.0;
|
||||
|
||||
public get getPosition(): Vector2 {
|
||||
return this.position;
|
||||
}
|
||||
public get Position(): Vector2 {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
public set setPosition(position: Vector2) {
|
||||
this.position = position;
|
||||
}
|
||||
public set setPosition(position: Vector2) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public get getRotation(): number {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
public set setRotation(rotation: number) {
|
||||
this.rotation = rotation
|
||||
}
|
||||
public get Rotation(): number {
|
||||
return this.rotation;
|
||||
}
|
||||
|
||||
public set setRotation(rotation: number) {
|
||||
this.rotation = rotation;
|
||||
}
|
||||
}
|
||||
|
||||
export default Rigidbody2D;
|
||||
@@ -1,27 +1,25 @@
|
||||
import Vector2 from "$lib/vector";
|
||||
import Vector2 from '$lib/vector';
|
||||
|
||||
class Line2D {
|
||||
private from: Vector2;
|
||||
private to: Vector2;
|
||||
private from: Vector2;
|
||||
private to: Vector2;
|
||||
|
||||
constructor(from: Vector2, to: Vector2) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
constructor(from: Vector2, to: Vector2) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
public getStart(): Vector2 {
|
||||
return this.from;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
public getEnd(): Vector2 {
|
||||
return this.to;
|
||||
}
|
||||
|
||||
public lengthSqr(): number {
|
||||
return new Vector2(this.to).sub(this.from).sqrLength;
|
||||
}
|
||||
}
|
||||
|
||||
export default Line2D;
|
||||
Reference in New Issue
Block a user