WIP: No idea what exactly i did over those past 2 days
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
import Line2D from '$lib/shimagic/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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static pointInBox2D(point: Vector2, box: Box2D): boolean {
|
||||
const pointLocal = new Vector2(point);
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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.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
|
||||
|
||||
RaycastResult.reset(result);
|
||||
|
||||
const halfSize: Vector2 = primitive.getHalfSize;
|
||||
|
||||
const xAxis: Vector2 = new Vector2(1, 0);
|
||||
const yAxis: Vector2 = new Vector2(0, 1);
|
||||
VMath.rotate(xAxis, primitive.getRigidbody.Rotation, Vector2.zero);
|
||||
VMath.rotate(yAxis, primitive.getRigidbody.Rotation, Vector2.zero);
|
||||
|
||||
const p: Vector2 = new Vector2(primitive.getRigidbody.Position).sub(ray.Origin);
|
||||
const f: Vector2 = new Vector2(xAxis.dot(ray.Direction), yAxis.dot(ray.Direction));
|
||||
const e: Vector2 = new Vector2(xAxis.dot(p), yAxis.dot(p));
|
||||
|
||||
const tArr: number[] = [0, 0, 0, 0];
|
||||
if (VMath.compare(f.x, 0)) {
|
||||
if (-e.x - halfSize.x > 0 || -e.x + halfSize.x < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
f.x = 0.00001;
|
||||
tArr[0 * 2 + 0] = e.x + halfSize.x / f.x;
|
||||
tArr[0 * 2 + 1] = e.x - halfSize.x / f.x;
|
||||
|
||||
if (VMath.compare(f.y, 0)) {
|
||||
if (-e.y - halfSize.y > 0 || -e.y + halfSize.y < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
f.y = 0.00001;
|
||||
tArr[1 * 2 + 0] = e.y + halfSize.y / f.y;
|
||||
tArr[1 * 2 + 1] = e.x - halfSize.x / f.x;
|
||||
|
||||
const tmin: number = Math.max(Math.min(tArr[0], tArr[1]), Math.min(tArr[2], tArr[3]));
|
||||
const tmax: number = Math.min(Math.max(tArr[0], tArr[1]), Math.max(tArr[2], tArr[3]));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static circleAndLine()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user