trainsim/track.ts

153 lines
4.5 KiB
TypeScript

import { PathSegment } from "./math/path.ts";
import { Vector } from "./math/vector.ts";
import { Train } from "./train.ts";
export class Track extends PathSegment {
editable = false;
next: Track;
prev: Track;
id: string;
constructor(points: [Vector, Vector, Vector, Vector], next?: Track, prev?: Track) {
super(points);
this.id = crypto.randomUUID();
this.next = next || this;
this.prev = prev || this;
}
followTrack(train: Train): [Vector, number] {
const predict = train.velocity.copy();
predict.normalize();
predict.mult(1);
const predictpos = Vector.add(train.position, predict)
// const leading = train.leadingPoint;
// let closest = this.points[0];
// let closestDistance = this.getClosestPoint(leading);
let [closest, closestDistance, closestT] = this.getClosestPoint(predictpos);
// deno-lint-ignore no-this-alias
let mostValid: Track = this;
if (this.next !== this) {
const [point, distance, t] = this.next.getClosestPoint(predictpos);
if (distance < closestDistance) {
closest = point;
closestDistance = distance;
mostValid = this.next;
closestT = t;
}
}
if (this.prev !== this) {
const [point, distance, t] = this.next.getClosestPoint(predictpos);
if (distance < closestDistance) {
closest = point;
closestDistance = distance;
mostValid = this.next;
closestT = t;
}
}
train.currentTrack = mostValid;
train.arrive(closest);
// if (predictpos.dist(closest) > 2) train.arrive(closest);
return [closest, closestT];
}
getNearestPoint(p: Vector) {
let [closest, closestDistance, closestT] = this.getClosestPoint(p);
// deno-lint-ignore no-this-alias
let mostValid: Track = this;
if (this.next !== this) {
const [point, distance, t] = this.next.getClosestPoint(p);
if (distance < closestDistance) {
closest = point;
closestDistance = distance;
mostValid = this.next;
closestT = t;
}
}
if (this.prev !== this) {
const [point, distance, t] = this.next.getClosestPoint(p);
if (distance < closestDistance) {
closest = point;
closestDistance = distance;
mostValid = this.next;
closestT = t;
}
}
return closest;
}
getAllPointsInRange(v: Vector, r: number) {
const points: [number, PathSegment][] = this.getPointsWithinRadius(v, r).concat(this.next.getPointsWithinRadius(v, r), this.prev.getPointsWithinRadius(v, r))
return points;
}
draw(): void {
super.draw();
if (this.ctx && this.editable)
for (const e of this.points) {
this.ctx.fillStyle = 'blue';
e.drawDot(this.ctx);
}
}
}
export class Spline<T extends PathSegment = PathSegment> {
segments: T[] = [];
ctx?: CanvasRenderingContext2D;
constructor(segs: T[]) {
this.segments = segs;
}
setContext(ctx: CanvasRenderingContext2D) {
this.ctx = ctx;
for (const segment of this.segments) {
segment.setContext(ctx);
}
}
draw() {
for (const segment of this.segments) {
segment.draw();
}
}
}
export const generateSquareTrack = () => {
const first = new Track([new Vector(20, 40), new Vector(20, 100), new Vector(20, 300), new Vector(20, 360)]);
const second = new Track([first.points[3], new Vector(20, 370), new Vector(30, 380), new Vector(40, 380)]);
const third = new Track([second.points[3], new Vector(100, 380), new Vector(300, 380), new Vector(360, 380)]);
const fourth = new Track([third.points[3], new Vector(370, 380), new Vector(380, 370), new Vector(380, 360)]);
const fifth = new Track([fourth.points[3], new Vector(380, 300), new Vector(380, 100), new Vector(380, 40)]);
const sixth = new Track([fifth.points[3], new Vector(380, 30), new Vector(370, 20), new Vector(360, 20)]);
const seventh = new Track([sixth.points[3], new Vector(300, 20), new Vector(100, 20), new Vector(40, 20)]);
const eighth = new Track([seventh.points[3], new Vector(30, 20), new Vector(20, 30), first.points[0]]);
const tracks = [first, second, third, fourth, fifth, sixth, seventh, eighth];
for (const [i, track] of tracks.entries()) {
track.next = tracks[(i + 1) % tracks.length];
track.prev = tracks.at(i - 1)!;
}
// first.next = second;
// first.prev = eighth;
// second.next = third;
// second.prev = first;
// third.
return new Spline<Track>([first, second, third, fourth, fifth, sixth, seventh, eighth]);
}