moooooore physics
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "$lib/stores/vector";
|
import Vector2 from "$lib/vector";
|
||||||
import Rigidbody2D from "../rigidbody/rigidbody2d";
|
import Rigidbody2D from "../rigidbody/rigidbody2d";
|
||||||
|
|
||||||
class AABB {
|
class AABB {
|
||||||
@@ -6,7 +6,6 @@ class AABB {
|
|||||||
private halfSize: Vector2 = new Vector2();
|
private halfSize: Vector2 = new Vector2();
|
||||||
private rigidbody: Rigidbody2D | null = null;
|
private rigidbody: Rigidbody2D | null = null;
|
||||||
|
|
||||||
constructor();
|
|
||||||
constructor(min?: Vector2, max?: Vector2) {
|
constructor(min?: Vector2, max?: Vector2) {
|
||||||
if(min !== undefined && max !== undefined)
|
if(min !== undefined && max !== undefined)
|
||||||
this.size = new Vector2(max).sub(min);
|
this.size = new Vector2(max).sub(min);
|
||||||
@@ -22,3 +21,5 @@ class AABB {
|
|||||||
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
|
return new Vector2(this.rigidbody.getPosition).add(this.halfSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default AABB;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "$lib/stores/vector"
|
import Vector2 from "$lib/vector"
|
||||||
import type Rigidbody2D from "../rigidbody/rigidbody2d";
|
import type Rigidbody2D from "../rigidbody/rigidbody2d";
|
||||||
|
|
||||||
class Box2D {
|
class Box2D {
|
||||||
@@ -15,6 +15,36 @@ class Box2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get getMin(): Vector2 {
|
public get getMin(): Vector2 {
|
||||||
|
return new Vector2(this.rigidbody.getPosition).sub(this.halfSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "$lib/stores/vector";
|
import Vector2 from "$lib/vector";
|
||||||
|
|
||||||
class Collider2D {
|
class Collider2D {
|
||||||
protected offset: Vector2 = new Vector2();
|
protected offset: Vector2 = new Vector2();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "$lib/stores/vector";
|
import Vector2 from "$lib/vector";
|
||||||
|
|
||||||
class Rigidbody2D {
|
class Rigidbody2D {
|
||||||
private position: Vector2 = new Vector2();
|
private position: Vector2 = new Vector2();
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "./vector";
|
import Vector2 from "../vector";
|
||||||
|
|
||||||
export class Shimeji {
|
export class Shimeji {
|
||||||
private element: HTMLElement;
|
private element: HTMLElement;
|
||||||
@@ -19,9 +19,9 @@ export class Shimeji {
|
|||||||
|
|
||||||
private readonly TIMESCALE: number = 1;
|
private readonly TIMESCALE: number = 1;
|
||||||
private readonly RESTITUTION: number = 0.4;
|
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 DAMPING: number = 1;
|
||||||
private readonly MASS: number = 0.1;
|
private readonly MASS: number = 1;
|
||||||
private readonly FRICTION: number = 0.9;
|
private readonly FRICTION: number = 0.9;
|
||||||
private readonly GRAVITY: number = 50;
|
private readonly GRAVITY: number = 50;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import Vector2 from "./vector";
|
import Vector2 from "../vector";
|
||||||
|
|
||||||
export class Shimeji {
|
export class Shimeji {
|
||||||
private element: HTMLElement;
|
private element: HTMLElement;
|
||||||
|
|||||||
@@ -159,12 +159,6 @@ class Vector2 {
|
|||||||
return this;
|
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 {
|
get perpendicular(): Vector2 {
|
||||||
return new Vector2(-this.y, this.x);
|
return new Vector2(-this.y, this.x);
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
Reference in New Issue
Block a user