one step forward, one step back
This commit is contained in:
@@ -30,19 +30,55 @@ export class SBendRight extends StraightTrack {
|
||||
}
|
||||
}
|
||||
|
||||
export class BankLeft extends StraightTrack {
|
||||
export class BankLeft extends TrackSegment {
|
||||
constructor(start?: Vector) {
|
||||
start = start || new Vector(100, 100);
|
||||
super(start);
|
||||
this.points[2].add(0, -25);
|
||||
this.points[3].add(0, 25);
|
||||
|
||||
const p1 = start.copy();
|
||||
const p2 = start.copy();
|
||||
const p3 = start.copy();
|
||||
const p4 = start.copy();
|
||||
const scale = 33;
|
||||
|
||||
p2.add(new Vector(1, 0).mult(scale));
|
||||
p3.set(p2);
|
||||
const dirToP3 = Vector.fromAngle(-Math.PI / 12).mult(scale);
|
||||
p3.add(dirToP3);
|
||||
p4.set(p3);
|
||||
const dirToP4 = Vector.fromAngle(-Math.PI / 6).mult(scale);
|
||||
p4.add(dirToP4);
|
||||
|
||||
super([
|
||||
p1,
|
||||
p2,
|
||||
p3,
|
||||
p4,
|
||||
]);
|
||||
}
|
||||
}
|
||||
export class BankRight extends StraightTrack {
|
||||
export class BankRight extends TrackSegment {
|
||||
constructor(start?: Vector) {
|
||||
start = start || new Vector(100, 100);
|
||||
super(start);
|
||||
this.points[2].add(0, 25);
|
||||
this.points[3].add(0, -25);
|
||||
|
||||
const p1 = start.copy();
|
||||
const p2 = start.copy();
|
||||
const p3 = start.copy();
|
||||
const p4 = start.copy();
|
||||
const scale = 33;
|
||||
|
||||
p2.add(new Vector(1, 0).mult(scale));
|
||||
p3.set(p2);
|
||||
const dirToP3 = Vector.fromAngle(Math.PI / 12).mult(scale);
|
||||
p3.add(dirToP3);
|
||||
p4.set(p3);
|
||||
const dirToP4 = Vector.fromAngle(Math.PI / 6).mult(scale);
|
||||
p4.add(dirToP4);
|
||||
|
||||
super([
|
||||
p1,
|
||||
p2,
|
||||
p3,
|
||||
p4,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
200
track/system.ts
200
track/system.ts
@@ -1,5 +1,5 @@
|
||||
import { Doodler, Point, Vector } from "@bearmetal/doodler";
|
||||
import { PathSegment } from "../math/path.ts";
|
||||
import { ComplexPath, PathSegment } from "../math/path.ts";
|
||||
import { getContextItem, setDefaultContext } from "../lib/context.ts";
|
||||
|
||||
export class TrackSystem {
|
||||
@@ -103,12 +103,13 @@ export class TrackSystem {
|
||||
return track;
|
||||
}
|
||||
|
||||
static deserialize(data: any) {
|
||||
static deserialize(data: SerializedTrackSegment[]) {
|
||||
if (data.length === 0) return undefined;
|
||||
const track = new TrackSystem([]);
|
||||
const neighborMap = new Map<string, [string[], string[]]>();
|
||||
for (const segment of data) {
|
||||
track.segments.set(segment.id, TrackSegment.deserialize(segment));
|
||||
neighborMap.set(segment.id, [segment.fNeighbors, segment.bNeighbors]);
|
||||
}
|
||||
for (const segment of track.segments.values()) {
|
||||
segment.setTrack(track);
|
||||
@@ -122,6 +123,7 @@ export class TrackSystem {
|
||||
).filter((s) => s) as TrackSegment[];
|
||||
}
|
||||
}
|
||||
console.log(track.segments);
|
||||
return track;
|
||||
}
|
||||
|
||||
@@ -130,6 +132,61 @@ export class TrackSystem {
|
||||
segment.translate(v);
|
||||
}
|
||||
}
|
||||
|
||||
private _path?: Spline<TrackSegment>;
|
||||
|
||||
get path() {
|
||||
if (!this._path) {
|
||||
this._path = this.generatePath();
|
||||
}
|
||||
return this._path;
|
||||
}
|
||||
|
||||
generatePath() {
|
||||
if (!this.firstSegment) throw new Error("No first segment");
|
||||
const rightOnlyPath = [
|
||||
this.firstSegment.copy(),
|
||||
...this.findRightPath(this.firstSegment),
|
||||
];
|
||||
|
||||
rightOnlyPath.forEach((s, i, arr) => {
|
||||
if (i === 0) return;
|
||||
const prev = arr[i - 1];
|
||||
s.points[0] = prev.points[3];
|
||||
s.prev = prev;
|
||||
prev.next = s;
|
||||
});
|
||||
|
||||
console.log(rightOnlyPath);
|
||||
|
||||
return new Spline<TrackSegment>(rightOnlyPath);
|
||||
}
|
||||
|
||||
*findRightPath(start: TrackSegment): Generator<TrackSegment> {
|
||||
if (start.frontNeighbours.length === 0) {
|
||||
yield start;
|
||||
return;
|
||||
}
|
||||
let rightMost = start.frontNeighbours[0].copy();
|
||||
for (const segment of start.frontNeighbours) {
|
||||
if (segment.id === rightMost.id) continue;
|
||||
const rotatedSegment = segment.copy();
|
||||
rotatedSegment.rotateAboutPoint(
|
||||
rotatedSegment.tangent(0).heading(),
|
||||
rotatedSegment.points[0],
|
||||
);
|
||||
const rotatedRightMost = rightMost.copy();
|
||||
rotatedRightMost.rotateAboutPoint(
|
||||
rotatedRightMost.tangent(0).heading(),
|
||||
rotatedRightMost.points[0],
|
||||
);
|
||||
if (rotatedSegment.points[3].y > rotatedRightMost.points[3].y) {
|
||||
rightMost = segment;
|
||||
}
|
||||
}
|
||||
yield rightMost.copy();
|
||||
yield* this.findRightPath(rightMost);
|
||||
}
|
||||
}
|
||||
|
||||
type VectorSet = [Vector, Vector, Vector, Vector];
|
||||
@@ -154,7 +211,7 @@ export class TrackSegment extends PathSegment {
|
||||
this.track = t;
|
||||
}
|
||||
|
||||
draw(showControls = false) {
|
||||
override draw(showControls = false) {
|
||||
this.doodler.drawBezier(
|
||||
this.points[0],
|
||||
this.points[1],
|
||||
@@ -180,7 +237,7 @@ export class TrackSegment extends PathSegment {
|
||||
}
|
||||
}
|
||||
|
||||
serialize() {
|
||||
serialize(): SerializedTrackSegment {
|
||||
return {
|
||||
p: this.points.map((p) => p.array()),
|
||||
id: this.id,
|
||||
@@ -278,3 +335,138 @@ export class TrackSegment extends PathSegment {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export class Spline<T extends PathSegment = PathSegment> {
|
||||
segments: T[] = [];
|
||||
ctx?: CanvasRenderingContext2D;
|
||||
|
||||
evenPoints: Vector[];
|
||||
pointSpacing: number;
|
||||
|
||||
get points() {
|
||||
return Array.from(new Set(this.segments.flatMap((s) => s.points)));
|
||||
}
|
||||
|
||||
nodes: IControlNode[];
|
||||
|
||||
constructor(segs: T[]) {
|
||||
this.segments = segs;
|
||||
this.pointSpacing = 1;
|
||||
this.evenPoints = this.calculateEvenlySpacedPoints(1);
|
||||
this.nodes = [];
|
||||
for (let i = 0; i < this.points.length; i += 3) {
|
||||
const node: IControlNode = {
|
||||
anchor: this.points[i],
|
||||
controls: [
|
||||
this.points.at(i - 1)!,
|
||||
this.points[(i + 1) % this.points.length],
|
||||
],
|
||||
mirrored: false,
|
||||
tangent: true,
|
||||
};
|
||||
this.nodes.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
// setContext(ctx: CanvasRenderingContext2D) {
|
||||
// this.ctx = ctx;
|
||||
// for (const segment of this.segments) {
|
||||
// segment.setContext(ctx);
|
||||
// }
|
||||
// }
|
||||
|
||||
draw() {
|
||||
for (const segment of this.segments) {
|
||||
segment.draw();
|
||||
}
|
||||
}
|
||||
|
||||
calculateEvenlySpacedPoints(spacing: number, resolution = 1) {
|
||||
this.pointSpacing = 1;
|
||||
// return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
|
||||
const points: Vector[] = [];
|
||||
|
||||
points.push(this.segments[0].points[0]);
|
||||
let prev = points[0];
|
||||
let distSinceLastEvenPoint = 0;
|
||||
for (const seg of this.segments) {
|
||||
let t = 0;
|
||||
|
||||
const div = Math.ceil(seg.length * resolution * 10);
|
||||
while (t < 1) {
|
||||
t += 1 / div;
|
||||
const point = seg.getPointAtT(t);
|
||||
distSinceLastEvenPoint += prev.dist(point);
|
||||
|
||||
if (distSinceLastEvenPoint >= spacing) {
|
||||
const overshoot = distSinceLastEvenPoint - spacing;
|
||||
const evenPoint = Vector.add(
|
||||
point,
|
||||
Vector.sub(point, prev).normalize().mult(overshoot),
|
||||
);
|
||||
distSinceLastEvenPoint = overshoot;
|
||||
points.push(evenPoint);
|
||||
prev = evenPoint;
|
||||
}
|
||||
|
||||
prev = point;
|
||||
}
|
||||
}
|
||||
|
||||
this.evenPoints = points;
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
followEvenPoints(t: number) {
|
||||
if (t < 0) t += this.evenPoints.length;
|
||||
const i = Math.floor(t) % this.evenPoints.length;
|
||||
const a = this.evenPoints[i];
|
||||
const b = this.evenPoints[(i + 1) % this.evenPoints.length];
|
||||
|
||||
return Vector.lerp(a, b, t % 1);
|
||||
}
|
||||
|
||||
calculateApproxLength() {
|
||||
for (const s of this.segments) {
|
||||
s.calculateApproxLength();
|
||||
}
|
||||
}
|
||||
|
||||
toggleNodeTangent(p: Vector) {
|
||||
const node = this.nodes.find((n) => n.anchor === p);
|
||||
|
||||
node && (node.tangent = !node.tangent);
|
||||
}
|
||||
toggleNodeMirrored(p: Vector) {
|
||||
const node = this.nodes.find((n) => n.anchor === p);
|
||||
|
||||
node && (node.mirrored = !node.mirrored);
|
||||
}
|
||||
handleNodeEdit(p: Vector, movement: { x: number; y: number }) {
|
||||
const node = this.nodes.find((n) =>
|
||||
n.anchor === p || n.controls.includes(p)
|
||||
);
|
||||
if (!node || !(node.mirrored || node.tangent)) return;
|
||||
|
||||
if (node.anchor !== p) {
|
||||
if (node.mirrored || node.tangent) {
|
||||
const mover = node.controls.find((e) => e !== p)!;
|
||||
const v = Vector.sub(node.anchor, p);
|
||||
if (!node.mirrored) v.setMag(Vector.sub(node.anchor, mover).mag());
|
||||
mover.set(Vector.add(v, node.anchor));
|
||||
}
|
||||
} else {
|
||||
for (const control of node.controls) {
|
||||
control.add(movement.x, movement.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface IControlNode {
|
||||
anchor: Vector;
|
||||
controls: [Vector, Vector];
|
||||
tangent: boolean;
|
||||
mirrored: boolean;
|
||||
}
|
||||
|
Reference in New Issue
Block a user