Compare commits
8 Commits
20e6174658
...
overhaul
Author | SHA1 | Date | |
---|---|---|---|
5a5e490aa5 | |||
9500f6dabf | |||
7b6dbb295f | |||
3aea38f9f4 | |||
2176f67413 | |||
10d462edaf | |||
03e0b1afcb | |||
7b244526b9 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -26,3 +26,5 @@ dist-ssr
|
|||||||
|
|
||||||
# Packed devtools
|
# Packed devtools
|
||||||
devtools.zip
|
devtools.zip
|
||||||
|
|
||||||
|
temp.*
|
@@ -14,8 +14,9 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@bearmetal/doodler": "jsr:@bearmetal/doodler@0.0.5-c",
|
"@bearmetal/doodler": "jsr:@bearmetal/doodler@0.0.5-i",
|
||||||
"@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.4",
|
"@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.4",
|
||||||
"vite": "npm:vite@^6.0.1"
|
"vite": "npm:vite@^6.0.1"
|
||||||
}
|
},
|
||||||
|
"nodeModulesDir": "auto"
|
||||||
}
|
}
|
8
deno.lock
generated
8
deno.lock
generated
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"version": "4",
|
"version": "4",
|
||||||
"specifiers": {
|
"specifiers": {
|
||||||
"jsr:@bearmetal/doodler@0.0.5-c": "0.0.5-c",
|
"jsr:@bearmetal/doodler@0.0.5-i": "0.0.5-i",
|
||||||
"jsr:@std/assert@*": "1.0.10",
|
"jsr:@std/assert@*": "1.0.10",
|
||||||
"jsr:@std/assert@^1.0.10": "1.0.10",
|
"jsr:@std/assert@^1.0.10": "1.0.10",
|
||||||
"jsr:@std/internal@^1.0.5": "1.0.5",
|
"jsr:@std/internal@^1.0.5": "1.0.5",
|
||||||
@@ -13,8 +13,8 @@
|
|||||||
"npm:web-ext@*": "8.4.0"
|
"npm:web-ext@*": "8.4.0"
|
||||||
},
|
},
|
||||||
"jsr": {
|
"jsr": {
|
||||||
"@bearmetal/doodler@0.0.5-c": {
|
"@bearmetal/doodler@0.0.5-i": {
|
||||||
"integrity": "34b0db85af1393b1b01622915963a8b33ee923c14b381afe9c771efd3d631cf1"
|
"integrity": "5aa20e3d838218f0934a268639f7c2afe706aed7f87f59570a26650b968f8c8b"
|
||||||
},
|
},
|
||||||
"@std/assert@1.0.10": {
|
"@std/assert@1.0.10": {
|
||||||
"integrity": "59b5cbac5bd55459a19045d95cc7c2ff787b4f8527c0dd195078ff6f9481fbb3",
|
"integrity": "59b5cbac5bd55459a19045d95cc7c2ff787b4f8527c0dd195078ff6f9481fbb3",
|
||||||
@@ -2103,7 +2103,7 @@
|
|||||||
},
|
},
|
||||||
"workspace": {
|
"workspace": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"jsr:@bearmetal/doodler@0.0.5-c",
|
"jsr:@bearmetal/doodler@0.0.5-i",
|
||||||
"npm:@deno/vite-plugin@^1.0.4",
|
"npm:@deno/vite-plugin@^1.0.4",
|
||||||
"npm:vite@^6.0.1"
|
"npm:vite@^6.0.1"
|
||||||
]
|
]
|
||||||
|
BIN
public/blobs/snr/audio/ding.mp3
Normal file
BIN
public/blobs/snr/audio/ding.mp3
Normal file
Binary file not shown.
@@ -2,8 +2,12 @@ import { getContextItem } from "./lib/context.ts";
|
|||||||
import { InputManager } from "./lib/input.ts";
|
import { InputManager } from "./lib/input.ts";
|
||||||
import { StateMachine } from "./state/machine.ts";
|
import { StateMachine } from "./state/machine.ts";
|
||||||
import { States } from "./state/states/index.ts";
|
import { States } from "./state/states/index.ts";
|
||||||
|
import { TrackSystem } from "./track/system.ts";
|
||||||
|
|
||||||
export function bootstrapInputs() {
|
export function bootstrapInputs() {
|
||||||
|
addEventListener("keydown", (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
});
|
||||||
const inputManager = getContextItem<InputManager>("inputManager");
|
const inputManager = getContextItem<InputManager>("inputManager");
|
||||||
inputManager.onKey("e", () => {
|
inputManager.onKey("e", () => {
|
||||||
const state = getContextItem<StateMachine<States>>("state");
|
const state = getContextItem<StateMachine<States>>("state");
|
||||||
@@ -14,4 +18,22 @@ export function bootstrapInputs() {
|
|||||||
localStorage.removeItem("track");
|
localStorage.removeItem("track");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
inputManager.onKey("c", () => {
|
||||||
|
if (inputManager.getKeyState("Control")) {
|
||||||
|
const currentTrack = localStorage.getItem("track");
|
||||||
|
navigator.clipboard.writeText(currentTrack ?? "[]");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
addEventListener("paste", async (e) => {
|
||||||
|
let data = e.clipboardData?.getData("text/plain");
|
||||||
|
if (!data) return;
|
||||||
|
try {
|
||||||
|
// data = data.trim().replace(/^"|"$/g, "").replace(/\\"/g, '"');
|
||||||
|
console.log(data);
|
||||||
|
const track = TrackSystem.deserialize(JSON.parse(data));
|
||||||
|
localStorage.setItem("track", track.serialize());
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@@ -1,14 +1,35 @@
|
|||||||
|
import { ZoomableDoodler } from "@bearmetal/doodler";
|
||||||
import { TrackSegment } from "../track/system.ts";
|
import { TrackSegment } from "../track/system.ts";
|
||||||
import { Train, TrainCar } from "../train/train.ts";
|
import { Train, TrainCar } from "../train/train.ts";
|
||||||
import { InputManager } from "./input.ts";
|
import { InputManager } from "./input.ts";
|
||||||
|
import { ResourceManager } from "./resources.ts";
|
||||||
|
import { Debuggable } from "./debuggable.ts";
|
||||||
|
|
||||||
|
interface ContextMap {
|
||||||
|
inputManager: InputManager;
|
||||||
|
doodler: ZoomableDoodler;
|
||||||
|
resources: ResourceManager;
|
||||||
|
debug: Debug;
|
||||||
|
debuggables: Debuggable[];
|
||||||
|
colors: [
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
string,
|
||||||
|
...string[],
|
||||||
|
];
|
||||||
|
[key: string]: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
type ContextStore = Record<string, any>;
|
type ContextStore = Record<string, any>;
|
||||||
|
|
||||||
const defaultContext: ContextStore = {};
|
const defaultContext: ContextStore = {};
|
||||||
const contextStack: ContextStore[] = [defaultContext];
|
const contextStack: ContextStore[] = [defaultContext];
|
||||||
|
|
||||||
const debug = JSON.parse(localStorage.getItem("debug") || "false");
|
|
||||||
|
|
||||||
export function setDefaultContext(context: ContextStore) {
|
export function setDefaultContext(context: ContextStore) {
|
||||||
Object.assign(defaultContext, context);
|
Object.assign(defaultContext, context);
|
||||||
}
|
}
|
||||||
@@ -38,6 +59,10 @@ export const ctx = new Proxy(
|
|||||||
export function getContext() {
|
export function getContext() {
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
export function getContextItem<K extends keyof ContextMap>(
|
||||||
|
prop: K,
|
||||||
|
): ContextMap[K];
|
||||||
|
export function getContextItem<T>(prop: string): T;
|
||||||
export function getContextItem<T>(prop: string): T {
|
export function getContextItem<T>(prop: string): T {
|
||||||
return ctx[prop] as T;
|
return ctx[prop] as T;
|
||||||
}
|
}
|
||||||
|
@@ -12,22 +12,24 @@ export abstract class Debuggable extends Drawable {
|
|||||||
if (typeof debugKeys[0] === "boolean") {
|
if (typeof debugKeys[0] === "boolean") {
|
||||||
drawFirst = debugKeys.shift() as boolean;
|
drawFirst = debugKeys.shift() as boolean;
|
||||||
}
|
}
|
||||||
const draw = this.draw.bind(this);
|
const debuggables = getContextItem("debuggables");
|
||||||
this.draw = drawFirst
|
debuggables.push(this);
|
||||||
? (...args: unknown[]) => {
|
// const draw = this.draw.bind(this);
|
||||||
draw(...args);
|
// this.draw = drawFirst
|
||||||
const debug = getContextItem<Debug>("debug");
|
// ? (...args: unknown[]) => {
|
||||||
if (debugKeys.some((k) => debug[k as keyof Debug])) {
|
// draw(...args);
|
||||||
this.debugDraw();
|
// const debug = getContextItem<Debug>("debug");
|
||||||
}
|
// if (debugKeys.some((k) => debug[k as keyof Debug])) {
|
||||||
}
|
// this.debugDraw();
|
||||||
: (...args: unknown[]) => {
|
// }
|
||||||
const debug = getContextItem<Debug>("debug");
|
// }
|
||||||
if (debugKeys.some((k) => debug[k as keyof Debug])) {
|
// : (...args: unknown[]) => {
|
||||||
this.debugDraw();
|
// const debug = getContextItem<Debug>("debug");
|
||||||
}
|
// if (debugKeys.some((k) => debug[k as keyof Debug])) {
|
||||||
draw(...args);
|
// this.debugDraw();
|
||||||
};
|
// }
|
||||||
|
// draw(...args);
|
||||||
|
// };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,7 +41,7 @@ export class ResourceManager {
|
|||||||
) {
|
) {
|
||||||
const identifier = parseNamespacedId(name);
|
const identifier = parseNamespacedId(name);
|
||||||
if (typeof (value as EventSource).addEventListener === "function") {
|
if (typeof (value as EventSource).addEventListener === "function") {
|
||||||
if (value instanceof Image) {
|
if (value instanceof Image || value instanceof Audio) {
|
||||||
// During development, we can use the local file system
|
// During development, we can use the local file system
|
||||||
value.src =
|
value.src =
|
||||||
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
|
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
|
||||||
@@ -55,8 +55,10 @@ export class ResourceManager {
|
|||||||
const onload = () => {
|
const onload = () => {
|
||||||
this.resources.set(name, value);
|
this.resources.set(name, value);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
(value as EventSource).removeEventListener("loadeddata", onload);
|
||||||
(value as EventSource).removeEventListener("load", onload);
|
(value as EventSource).removeEventListener("load", onload);
|
||||||
};
|
};
|
||||||
|
(value as EventSource).addEventListener("loadeddata", onload);
|
||||||
(value as EventSource).addEventListener("load", onload);
|
(value as EventSource).addEventListener("load", onload);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@@ -27,7 +27,7 @@ setTimeout(() => {
|
|||||||
.imageSmoothingEnabled = false;
|
.imageSmoothingEnabled = false;
|
||||||
}, 0);
|
}, 0);
|
||||||
// doodler.minScale = 0.1;
|
// doodler.minScale = 0.1;
|
||||||
(doodler as any).scale = 3.14;
|
// (doodler as any).scale = 3.14;
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
"red",
|
"red",
|
||||||
@@ -47,6 +47,8 @@ const _fullDebug: Debug = {
|
|||||||
car: false,
|
car: false,
|
||||||
bogies: false,
|
bogies: false,
|
||||||
angles: false,
|
angles: false,
|
||||||
|
aabb: false,
|
||||||
|
segment: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");
|
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");
|
||||||
|
7
src/math/angle.ts
Normal file
7
src/math/angle.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export function angleToRadians(angle: number) {
|
||||||
|
return angle / 180 * Math.PI;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function angleToDegrees(angle: number) {
|
||||||
|
return angle * 180 / Math.PI;
|
||||||
|
}
|
66
src/physics/SpatialGrid.ts
Normal file
66
src/physics/SpatialGrid.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import { Vector } from "@bearmetal/doodler";
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
type GridItem = [Vector, Vector, Set<tag>];
|
||||||
|
}
|
||||||
|
export class SpatialHashGrid {
|
||||||
|
private grid: Map<string, GridItem[]> = new Map();
|
||||||
|
private cellSize: number;
|
||||||
|
|
||||||
|
constructor(cellSize: number) {
|
||||||
|
this.cellSize = cellSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getKey(x: number, y: number): string {
|
||||||
|
return `${Math.floor(x / this.cellSize)},${Math.floor(y / this.cellSize)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
insert(segment: GridItem) {
|
||||||
|
const [a, b] = segment;
|
||||||
|
const minX = Math.min(a.x, b.x);
|
||||||
|
const minY = Math.min(a.y, b.y);
|
||||||
|
const maxX = Math.max(a.x, b.x);
|
||||||
|
const maxY = Math.max(a.y, b.y);
|
||||||
|
|
||||||
|
for (let x = minX; x <= maxX; x += this.cellSize) {
|
||||||
|
for (let y = minY; y <= maxY; y += this.cellSize) {
|
||||||
|
const key = this.getKey(x, y);
|
||||||
|
if (!this.grid.has(key)) this.grid.set(key, []);
|
||||||
|
this.grid.get(key)!.push(segment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
query(position: Vector, radius: number, tag?: tag): GridItem[] {
|
||||||
|
const minX = position.x - radius;
|
||||||
|
const minY = position.y - radius;
|
||||||
|
const maxX = position.x + radius;
|
||||||
|
const maxY = position.y + radius;
|
||||||
|
|
||||||
|
const segments: Set<GridItem> = new Set();
|
||||||
|
|
||||||
|
for (let x = minX; x <= maxX; x += this.cellSize) {
|
||||||
|
for (let y = minY; y <= maxY; y += this.cellSize) {
|
||||||
|
const key = this.getKey(x, y);
|
||||||
|
if (this.grid.has(key)) {
|
||||||
|
for (const segment of this.grid.get(key)!) {
|
||||||
|
tag
|
||||||
|
? segment[2].has(tag) &&
|
||||||
|
segments.add(segment)
|
||||||
|
: segments.add(segment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(segments);
|
||||||
|
}
|
||||||
|
|
||||||
|
getAllSegments() {
|
||||||
|
return Array.from(this.grid.values()).flatMap((s) => s);
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.grid.clear();
|
||||||
|
}
|
||||||
|
}
|
39
src/physics/solver.ts
Normal file
39
src/physics/solver.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
export class PhysicsSolver {
|
||||||
|
tasks: SolverTask[] = [];
|
||||||
|
maxTickStep?: number;
|
||||||
|
|
||||||
|
addTask(c: SolverTask) {
|
||||||
|
c.solver = this;
|
||||||
|
this.tasks.push(c);
|
||||||
|
this.tasks.sort((a, b) => a.priority - b.priority);
|
||||||
|
}
|
||||||
|
removeTask(c: SolverTask) {
|
||||||
|
this.tasks = this.tasks.filter((c1) => c1 !== c);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanupTasks() {
|
||||||
|
this.tasks = this.tasks.filter((c) => c.active);
|
||||||
|
}
|
||||||
|
|
||||||
|
solve(dt: number) {
|
||||||
|
dt = Math.min(dt, this.maxTickStep ?? Infinity);
|
||||||
|
for (const c of this.tasks) {
|
||||||
|
for (let i = 0; i < c.iterations; i++) {
|
||||||
|
if (!c.apply(dt)) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export abstract class SolverTask {
|
||||||
|
iterations: number = 10;
|
||||||
|
solver?: PhysicsSolver;
|
||||||
|
priority: number = 0;
|
||||||
|
|
||||||
|
active = true;
|
||||||
|
|
||||||
|
abstract apply(dt: number): boolean;
|
||||||
|
remove() {
|
||||||
|
this.solver?.removeTask(this);
|
||||||
|
}
|
||||||
|
}
|
@@ -14,6 +14,10 @@ import {
|
|||||||
SBendLeft,
|
SBendLeft,
|
||||||
SBendRight,
|
SBendRight,
|
||||||
StraightTrack,
|
StraightTrack,
|
||||||
|
TightBankLeft,
|
||||||
|
TightBankRight,
|
||||||
|
WideBankLeft,
|
||||||
|
WideBankRight,
|
||||||
} from "../../track/shapes.ts";
|
} from "../../track/shapes.ts";
|
||||||
import { TrackSegment } from "../../track/system.ts";
|
import { TrackSegment } from "../../track/system.ts";
|
||||||
import { clamp } from "../../math/clamp.ts";
|
import { clamp } from "../../math/clamp.ts";
|
||||||
@@ -40,18 +44,6 @@ export class EditTrackState extends State<States> {
|
|||||||
const track = getContextItem<TrackSystem>("track");
|
const track = getContextItem<TrackSystem>("track");
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
|
|
||||||
// For moving a segment, i.e. the currently active one
|
|
||||||
// const segment = track.lastSegment;
|
|
||||||
// if (segment) {
|
|
||||||
// const firstPoint = segment.points[0].copy();
|
|
||||||
// const { x, y } = inputManager.getMouseLocation();
|
|
||||||
// segment.points.forEach((p, i) => {
|
|
||||||
// const relativePoint = Vector.sub(p, firstPoint);
|
|
||||||
// p.set(x, y);
|
|
||||||
// p.add(relativePoint);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (this.selectedSegment) {
|
if (this.selectedSegment) {
|
||||||
const segment = this.selectedSegment;
|
const segment = this.selectedSegment;
|
||||||
const firstPoint = segment.points[0].copy();
|
const firstPoint = segment.points[0].copy();
|
||||||
@@ -61,6 +53,7 @@ export class EditTrackState extends State<States> {
|
|||||||
p.set(mousePos);
|
p.set(mousePos);
|
||||||
p.add(relativePoint);
|
p.add(relativePoint);
|
||||||
});
|
});
|
||||||
|
segment.update();
|
||||||
|
|
||||||
const ends = track.findEnds();
|
const ends = track.findEnds();
|
||||||
setContextItem("showEnds", true);
|
setContextItem("showEnds", true);
|
||||||
@@ -109,18 +102,21 @@ export class EditTrackState extends State<States> {
|
|||||||
this.ghostRotated = false;
|
this.ghostRotated = false;
|
||||||
}
|
}
|
||||||
switch (this.closestEnd.frontOrBack) {
|
switch (this.closestEnd.frontOrBack) {
|
||||||
case "front":
|
case "front": {
|
||||||
this.ghostSegment.setPositionByPoint(
|
this.ghostSegment.setPositionByPoint(
|
||||||
this.closestEnd.pos,
|
this.closestEnd.pos,
|
||||||
this.ghostSegment.points[0],
|
this.ghostSegment.points[0],
|
||||||
);
|
);
|
||||||
// this.ghostSegment.points[0] = this.closestEnd.pos;
|
// this.ghostSegment.points[0] = this.closestEnd.pos;
|
||||||
|
const ghostEndTangent = this.ghostSegment.tangent(0);
|
||||||
|
|
||||||
!this.ghostRotated && this.ghostSegment.rotateAboutPoint(
|
!this.ghostRotated && this.ghostSegment.rotateAboutPoint(
|
||||||
this.closestEnd.tangent.heading(),
|
this.closestEnd.tangent.heading() - ghostEndTangent.heading(),
|
||||||
this.ghostSegment.points[0],
|
this.ghostSegment.points[0],
|
||||||
);
|
);
|
||||||
this.ghostRotated = true;
|
this.ghostRotated = true;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case "back": {
|
case "back": {
|
||||||
this.ghostSegment.setPositionByPoint(
|
this.ghostSegment.setPositionByPoint(
|
||||||
this.closestEnd.pos,
|
this.closestEnd.pos,
|
||||||
@@ -136,6 +132,8 @@ export class EditTrackState extends State<States> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.ghostSegment.update();
|
||||||
|
|
||||||
// } else if (closestEnd) {
|
// } else if (closestEnd) {
|
||||||
// this.closestEnd = closestEnd;
|
// this.closestEnd = closestEnd;
|
||||||
} else if (!this.closestEnd || !closestEnd) {
|
} else if (!this.closestEnd || !closestEnd) {
|
||||||
@@ -249,6 +247,7 @@ export class EditTrackState extends State<States> {
|
|||||||
|
|
||||||
if (translation.x !== 0 || translation.y !== 0) {
|
if (translation.x !== 0 || translation.y !== 0) {
|
||||||
track.translate(translation);
|
track.translate(translation);
|
||||||
|
track.recalculateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
@@ -286,6 +285,10 @@ export class EditTrackState extends State<States> {
|
|||||||
new SBendRight(),
|
new SBendRight(),
|
||||||
new BankLeft(),
|
new BankLeft(),
|
||||||
new BankRight(),
|
new BankRight(),
|
||||||
|
new WideBankLeft(),
|
||||||
|
new WideBankRight(),
|
||||||
|
new TightBankLeft(),
|
||||||
|
new TightBankRight(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const inputManager = getContextItem<InputManager>("inputManager");
|
const inputManager = getContextItem<InputManager>("inputManager");
|
||||||
@@ -306,14 +309,6 @@ export class EditTrackState extends State<States> {
|
|||||||
state.transitionTo(States.RUNNING);
|
state.transitionTo(States.RUNNING);
|
||||||
});
|
});
|
||||||
|
|
||||||
inputManager.onKey(" ", () => {
|
|
||||||
if (this.selectedSegment) {
|
|
||||||
this.selectedSegment = undefined;
|
|
||||||
} else {
|
|
||||||
this.selectedSegment = new StraightTrack();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
inputManager.onMouse("left", () => {
|
inputManager.onMouse("left", () => {
|
||||||
const track = getContextItem<TrackSystem>("track");
|
const track = getContextItem<TrackSystem>("track");
|
||||||
if (this.ghostSegment && this.closestEnd) {
|
if (this.ghostSegment && this.closestEnd) {
|
||||||
@@ -383,6 +378,18 @@ export class EditTrackState extends State<States> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
inputManager.onKey("r", () => {
|
||||||
|
if (!this.selectedSegment) return;
|
||||||
|
const segment = this.selectedSegment;
|
||||||
|
let angle = Math.PI / 12;
|
||||||
|
segment.rotate(angle);
|
||||||
|
});
|
||||||
|
inputManager.onKey("R", () => {
|
||||||
|
if (!this.selectedSegment) return;
|
||||||
|
const segment = this.selectedSegment;
|
||||||
|
let angle = -Math.PI / 12;
|
||||||
|
segment.rotate(angle);
|
||||||
|
});
|
||||||
// TODO
|
// TODO
|
||||||
// Cache trains and save
|
// Cache trains and save
|
||||||
|
|
||||||
@@ -404,6 +411,9 @@ export class EditTrackState extends State<States> {
|
|||||||
const inputManager = getContextItem<InputManager>("inputManager");
|
const inputManager = getContextItem<InputManager>("inputManager");
|
||||||
inputManager.offKey("e");
|
inputManager.offKey("e");
|
||||||
inputManager.offKey("w");
|
inputManager.offKey("w");
|
||||||
|
inputManager.offKey("z");
|
||||||
|
inputManager.offKey("r");
|
||||||
|
inputManager.offKey("R");
|
||||||
inputManager.offKey("Escape");
|
inputManager.offKey("Escape");
|
||||||
inputManager.offMouse("left");
|
inputManager.offMouse("left");
|
||||||
if (this.heldEvents.size > 0) {
|
if (this.heldEvents.size > 0) {
|
||||||
|
@@ -39,9 +39,11 @@ export class LoadState extends State<States> {
|
|||||||
resources.set("snr:sprite/LargeLady", new Image());
|
resources.set("snr:sprite/LargeLady", new Image());
|
||||||
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
|
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
|
||||||
// "/sprites/EngineSprites.png";
|
// "/sprites/EngineSprites.png";
|
||||||
|
|
||||||
|
resources.set("snr:audio/ding", new Audio());
|
||||||
resources.ready().then(() => {
|
resources.ready().then(() => {
|
||||||
this.stateMachine.transitionTo(States.RUNNING);
|
this.stateMachine.transitionTo(States.RUNNING);
|
||||||
});
|
}).catch((e) => console.error(e));
|
||||||
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
// this.layers.push(doodler.createLayer((_, __, dTime) => {
|
// this.layers.push(doodler.createLayer((_, __, dTime) => {
|
||||||
|
@@ -1,11 +1,13 @@
|
|||||||
import { Doodler } from "@bearmetal/doodler";
|
import { Doodler, Point, Vector, ZoomableDoodler } from "@bearmetal/doodler";
|
||||||
import { getContext, getContextItem } from "../../lib/context.ts";
|
import { getContext, getContextItem } from "../../lib/context.ts";
|
||||||
import { InputManager } from "../../lib/input.ts";
|
import { InputManager } from "../../lib/input.ts";
|
||||||
import { TrackSystem } from "../../track/system.ts";
|
import { TrackSystem } from "../../track/system.ts";
|
||||||
import { Tender } from "../../train/cars.ts";
|
import { Tender } from "../../train/cars.ts";
|
||||||
import { RedEngine } from "../../train/engines.ts";
|
import { RedEngine } from "../../train/engines.ts";
|
||||||
import { DotFollower } from "../../train/newTrain.ts";
|
// import { Train } from "../../train/train.ts";
|
||||||
import { Train } from "../../train/train.ts";
|
import { Train } from "../../train/newTrain/Train.ts";
|
||||||
|
import { Bogie, Driver } from "../../train/newTrain/Bogie.ts";
|
||||||
|
import { TrainCar } from "../../train/newTrain/TrainCar.ts";
|
||||||
import { State } from "../machine.ts";
|
import { State } from "../machine.ts";
|
||||||
import { States } from "./index.ts";
|
import { States } from "./index.ts";
|
||||||
import { LargeLady, LargeLadyTender } from "../../train/LargeLady.ts";
|
import { LargeLady, LargeLadyTender } from "../../train/LargeLady.ts";
|
||||||
@@ -19,8 +21,20 @@ export class RunningState extends State<States> {
|
|||||||
|
|
||||||
layers: number[] = [];
|
layers: number[] = [];
|
||||||
|
|
||||||
|
activeTrain: Train | undefined;
|
||||||
|
|
||||||
override update(dt: number): void {
|
override update(dt: number): void {
|
||||||
const ctx = getContext() as { trains: Train[]; track: TrackSystem };
|
const ctx = getContext() as { trains: Train[]; track: TrackSystem };
|
||||||
|
const doodler = getContextItem<ZoomableDoodler>(
|
||||||
|
"doodler",
|
||||||
|
);
|
||||||
|
// if (this.activeTrain) {
|
||||||
|
// // (doodler as any).origin = doodler.worldToScreen(
|
||||||
|
// // doodler.width - this.activeTrain.aabb.center.x,
|
||||||
|
// // doodler.height - this.activeTrain.aabb.center.y,
|
||||||
|
// // );
|
||||||
|
// doodler.centerCameraOn(this.activeTrain.aabb.center);
|
||||||
|
// }
|
||||||
// const ctx = getContext() as { trains: DotFollower[]; track: TrackSystem };
|
// const ctx = getContext() as { trains: DotFollower[]; track: TrackSystem };
|
||||||
// TODO
|
// TODO
|
||||||
// Update trains
|
// Update trains
|
||||||
@@ -28,7 +42,7 @@ export class RunningState extends State<States> {
|
|||||||
// Handle input
|
// Handle input
|
||||||
// Monitor world events
|
// Monitor world events
|
||||||
for (const train of ctx.trains) {
|
for (const train of ctx.trains) {
|
||||||
train.move(dt);
|
train.update(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
override start(): void {
|
override start(): void {
|
||||||
@@ -59,33 +73,44 @@ export class RunningState extends State<States> {
|
|||||||
// const path = track.path;
|
// const path = track.path;
|
||||||
// const follower = new DotFollower(path, path.points[0].copy());
|
// const follower = new DotFollower(path, path.points[0].copy());
|
||||||
// ctx.trains.push(follower);
|
// ctx.trains.push(follower);
|
||||||
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
|
// const train = new Train(track.path, [
|
||||||
// ctx.trains.push(train);
|
// new LargeLady(),
|
||||||
});
|
// new LargeLadyTender(),
|
||||||
const train = new Train(track.path, [
|
// ]);
|
||||||
new LargeLady(),
|
const bogies = [new Bogie(20, 20, 1), new Driver(20, 20, 1)];
|
||||||
new LargeLadyTender(),
|
const train = new Train(track, [
|
||||||
|
new TrainCar(
|
||||||
|
bogies,
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
track.generatePath();
|
||||||
|
const firstPoint = track.path.points[0].copy();
|
||||||
|
let prevOffset = 0;
|
||||||
|
for (let i = 0; i < bogies.length; i++) {
|
||||||
|
const b = bogies[i];
|
||||||
|
b.position = firstPoint.add(b.leadingOffset + prevOffset, 0);
|
||||||
|
b.prevPos = b.position.copy();
|
||||||
|
prevOffset += b.trailingOffset;
|
||||||
|
}
|
||||||
ctx.trains.push(train);
|
ctx.trains.push(train);
|
||||||
// const trainCount = 1000;
|
});
|
||||||
// for (let i = 0; i < trainCount; i++) {
|
|
||||||
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
|
|
||||||
// ctx.trains.push(train);
|
|
||||||
// }
|
|
||||||
|
|
||||||
inputManager.onKey("ArrowUp", () => {
|
inputManager.onKey("ArrowUp", () => {
|
||||||
const trains = getContextItem<Train[]>("trains");
|
const trains = getContextItem<Train[]>("trains");
|
||||||
for (const train of trains) {
|
for (const train of trains) {
|
||||||
train.speed += 1;
|
train.bogies.filter((b) => b instanceof Driver).forEach((b) => {
|
||||||
|
b.drivingForce += 10;
|
||||||
|
if (b.dir.mag() < 0.1) b.updateDirection(new Vector(1, 0));
|
||||||
|
b.velocity = new Vector(10, 0);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// for (const [i, train] of trains.entries()) {
|
|
||||||
// train.speed += .01 * i;
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
inputManager.onKey("ArrowDown", () => {
|
inputManager.onKey("ArrowDown", () => {
|
||||||
const trains = getContextItem<Train[]>("trains");
|
const trains = getContextItem<Train[]>("trains");
|
||||||
for (const train of trains) {
|
for (const train of trains) {
|
||||||
train.speed -= 1;
|
train.bogies.filter((b) => b instanceof Driver).forEach((b) => {
|
||||||
|
b.drivingForce -= 10;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,9 @@ export class SBendLeft extends TrackSegment {
|
|||||||
start = start || new Vector(100, 100);
|
start = start || new Vector(100, 100);
|
||||||
super([
|
super([
|
||||||
start,
|
start,
|
||||||
start.copy().add(60, 0),
|
start.copy().add(80, 0),
|
||||||
start.copy().add(90, -25),
|
start.copy().add(120, -25),
|
||||||
start.copy().add(150, -25),
|
start.copy().add(200, -25),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,9 +29,9 @@ export class SBendRight extends TrackSegment {
|
|||||||
start = start || new Vector(100, 100);
|
start = start || new Vector(100, 100);
|
||||||
super([
|
super([
|
||||||
start,
|
start,
|
||||||
start.copy().add(60, 0),
|
start.copy().add(80, 0),
|
||||||
start.copy().add(90, 25),
|
start.copy().add(120, 25),
|
||||||
start.copy().add(150, 25),
|
start.copy().add(200, 25),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,6 +62,110 @@ export class BankLeft extends TrackSegment {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class WideBankLeft extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 70.4;
|
||||||
|
|
||||||
|
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 TightBankLeft extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 61.57;
|
||||||
|
|
||||||
|
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 TightBankRight extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 61.57;
|
||||||
|
|
||||||
|
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 WideBankRight extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 70.4;
|
||||||
|
|
||||||
|
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 TrackSegment {
|
export class BankRight extends TrackSegment {
|
||||||
constructor(start?: Vector) {
|
constructor(start?: Vector) {
|
||||||
start = start || new Vector(100, 100);
|
start = start || new Vector(100, 100);
|
||||||
|
@@ -2,15 +2,27 @@ import { Doodler, Point, Vector } from "@bearmetal/doodler";
|
|||||||
import { ComplexPath, PathSegment } from "../math/path.ts";
|
import { ComplexPath, PathSegment } from "../math/path.ts";
|
||||||
import { getContextItem, setDefaultContext } from "../lib/context.ts";
|
import { getContextItem, setDefaultContext } from "../lib/context.ts";
|
||||||
import { clamp } from "../math/clamp.ts";
|
import { clamp } from "../math/clamp.ts";
|
||||||
|
import { Debuggable } from "../lib/debuggable.ts";
|
||||||
|
import { SpatialHashGrid } from "../physics/SpatialGrid.ts";
|
||||||
|
|
||||||
export class TrackSystem {
|
export class TrackSystem extends Debuggable {
|
||||||
private _segments: Map<string, TrackSegment> = new Map();
|
private _segments: Map<string, TrackSegment> = new Map();
|
||||||
private doodler: Doodler;
|
private doodler: Doodler;
|
||||||
|
public grid: SpatialHashGrid = new SpatialHashGrid(10);
|
||||||
|
|
||||||
constructor(segments: TrackSegment[]) {
|
constructor(segments: TrackSegment[]) {
|
||||||
|
super("track");
|
||||||
this.doodler = getContextItem<Doodler>("doodler");
|
this.doodler = getContextItem<Doodler>("doodler");
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
this._segments.set(segment.id, segment);
|
this._segments.set(segment.id, segment);
|
||||||
|
let [prev] = segment.evenPoints[0];
|
||||||
|
for (const [p] of segment.evenPoints.slice(1)) {
|
||||||
|
const seg: GridItem = [prev, p, new Set([segment.id])];
|
||||||
|
segment.lineSegments ??= [];
|
||||||
|
segment.lineSegments.push(seg);
|
||||||
|
this.grid.insert(seg);
|
||||||
|
prev = p;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +38,19 @@ export class TrackSystem {
|
|||||||
return this._segments.values().toArray().pop();
|
return this._segments.values().toArray().pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNearestSegment(pos: Vector) {
|
||||||
|
let minDistance = Infinity;
|
||||||
|
let nearestSegment: TrackSegment | undefined;
|
||||||
|
for (const segment of this._segments.values()) {
|
||||||
|
const distance = segment.getDistanceTo(pos);
|
||||||
|
if (distance < minDistance) {
|
||||||
|
minDistance = distance;
|
||||||
|
nearestSegment = segment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nearestSegment;
|
||||||
|
}
|
||||||
|
|
||||||
optimize(percent: number) {
|
optimize(percent: number) {
|
||||||
console.log("Optimizing track", percent * 100 / 4);
|
console.log("Optimizing track", percent * 100 / 4);
|
||||||
for (const segment of this._segments.values()) {
|
for (const segment of this._segments.values()) {
|
||||||
@@ -35,7 +60,7 @@ export class TrackSystem {
|
|||||||
|
|
||||||
recalculateAll() {
|
recalculateAll() {
|
||||||
for (const segment of this._segments.values()) {
|
for (const segment of this._segments.values()) {
|
||||||
segment.recalculateRailPoints();
|
segment.update();
|
||||||
segment.length = segment.calculateApproxLength();
|
segment.length = segment.calculateApproxLength();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,30 +84,21 @@ export class TrackSystem {
|
|||||||
for (const [i, segment] of this._segments.entries()) {
|
for (const [i, segment] of this._segments.entries()) {
|
||||||
segment.draw(showControls);
|
segment.draw(showControls);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// try {
|
override debugDraw(): void {
|
||||||
// if (getContextItem<boolean>("showEnds")) {
|
const debug = getContextItem("debug");
|
||||||
// const ends = this.findEnds();
|
if (debug.track) {
|
||||||
// for (const end of ends) {
|
for (const segment of this._segments.values()) {
|
||||||
// this.doodler.fillCircle(end.pos, 2, {
|
segment.drawAABB();
|
||||||
// color: "red",
|
}
|
||||||
// // weight: 3,
|
}
|
||||||
// });
|
this.grid.getAllSegments().forEach((segment) => {
|
||||||
// if (getContextItem<boolean>("debug")) {
|
this.doodler.drawLine(segment.slice(0, 2) as Vector[], {
|
||||||
// this.doodler.line(
|
color: "red",
|
||||||
// end.pos,
|
weight: 2,
|
||||||
// end.pos.copy().add(end.tangent.copy().mult(20)),
|
});
|
||||||
// {
|
});
|
||||||
// color: "blue",
|
|
||||||
// // weight: 3,
|
|
||||||
// },
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// } catch {
|
|
||||||
// setDefaultContext({ showEnds: false });
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ends: Map<TrackSegment, [End, End]> = new Map();
|
ends: Map<TrackSegment, [End, End]> = new Map();
|
||||||
@@ -183,6 +199,11 @@ export class TrackSystem {
|
|||||||
const prev = arr[i - 1];
|
const prev = arr[i - 1];
|
||||||
s.points[0] = prev.points[3];
|
s.points[0] = prev.points[3];
|
||||||
s.prev = prev;
|
s.prev = prev;
|
||||||
|
let [prevEvenPoint] = prev.evenPoints[0];
|
||||||
|
for (const [p] of s.evenPoints.slice(1)) {
|
||||||
|
this.grid.insert([prevEvenPoint, p, new Set([s.id])]);
|
||||||
|
prevEvenPoint = p;
|
||||||
|
}
|
||||||
prev.next = s;
|
prev.next = s;
|
||||||
});
|
});
|
||||||
if (flags.looping) {
|
if (flags.looping) {
|
||||||
@@ -281,15 +302,58 @@ export class TrackSegment extends PathSegment {
|
|||||||
normalPoints: Vector[] = [];
|
normalPoints: Vector[] = [];
|
||||||
antiNormalPoints: Vector[] = [];
|
antiNormalPoints: Vector[] = [];
|
||||||
evenPoints: [Vector, number][] = [];
|
evenPoints: [Vector, number][] = [];
|
||||||
|
lineSegments?: GridItem[];
|
||||||
|
|
||||||
|
aabb!: AABB;
|
||||||
|
|
||||||
|
private trackGuage = 12;
|
||||||
|
|
||||||
constructor(p: VectorSet, id?: string) {
|
constructor(p: VectorSet, id?: string) {
|
||||||
super(p);
|
super(p);
|
||||||
this.doodler = getContextItem<Doodler>("doodler");
|
this.doodler = getContextItem<Doodler>("doodler");
|
||||||
this.id = id ?? crypto.randomUUID();
|
this.id = id ?? crypto.randomUUID();
|
||||||
this.recalculateRailPoints();
|
this.update();
|
||||||
|
}
|
||||||
|
|
||||||
const spacing = Math.ceil(this.length / 10);
|
getDistanceTo(pos: Vector) {
|
||||||
this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing);
|
return Vector.dist(this.aabb.center, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateAABB() {
|
||||||
|
let minX = Infinity;
|
||||||
|
let maxX = -Infinity;
|
||||||
|
let minY = Infinity;
|
||||||
|
let maxY = -Infinity;
|
||||||
|
|
||||||
|
[...this.normalPoints, ...this.antiNormalPoints].forEach((p) => {
|
||||||
|
minX = Math.min(minX, p.x);
|
||||||
|
maxX = Math.max(maxX, p.x);
|
||||||
|
minY = Math.min(minY, p.y);
|
||||||
|
maxY = Math.max(maxY, p.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
const width = maxX - minX;
|
||||||
|
const height = maxY - minY;
|
||||||
|
if (width < this.trackGuage) {
|
||||||
|
const extra = (this.trackGuage - width) / 2;
|
||||||
|
minX -= extra;
|
||||||
|
maxX += extra;
|
||||||
|
}
|
||||||
|
if (height < this.trackGuage) {
|
||||||
|
const extra = (this.trackGuage - height) / 2;
|
||||||
|
minY -= extra;
|
||||||
|
maxY += extra;
|
||||||
|
}
|
||||||
|
this.aabb = {
|
||||||
|
pos: new Vector(minX, minY),
|
||||||
|
x: minX,
|
||||||
|
y: minY,
|
||||||
|
width: maxX - minX,
|
||||||
|
height: maxY - minY,
|
||||||
|
center: new Vector(minX, minY).add(
|
||||||
|
new Vector(maxX - minX, maxY - minY).div(2),
|
||||||
|
),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
recalculateRailPoints(resolution = 60) {
|
recalculateRailPoints(resolution = 60) {
|
||||||
@@ -298,12 +362,22 @@ export class TrackSegment extends PathSegment {
|
|||||||
for (let i = 0; i <= resolution; i++) {
|
for (let i = 0; i <= resolution; i++) {
|
||||||
const t = i / resolution;
|
const t = i / resolution;
|
||||||
const normal = this.tangent(t).rotate(Math.PI / 2);
|
const normal = this.tangent(t).rotate(Math.PI / 2);
|
||||||
normal.setMag(6);
|
normal.setMag(this.trackGuage / 2);
|
||||||
const p = this.getPointAtT(t);
|
const p = this.getPointAtT(t);
|
||||||
this.normalPoints.push(p.copy().add(normal));
|
this.normalPoints.push(p.copy().add(normal));
|
||||||
this.antiNormalPoints.push(p.copy().add(normal.rotate(Math.PI)));
|
this.antiNormalPoints.push(p.copy().add(normal.rotate(Math.PI)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
recalculateTiePoints() {
|
||||||
|
const spacing = Math.ceil(this.length / 10);
|
||||||
|
this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing);
|
||||||
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
this.recalculateRailPoints();
|
||||||
|
this.recalculateTiePoints();
|
||||||
|
this.updateAABB();
|
||||||
|
}
|
||||||
|
|
||||||
setTrack(t: TrackSystem) {
|
setTrack(t: TrackSystem) {
|
||||||
this.track = t;
|
this.track = t;
|
||||||
@@ -389,6 +463,15 @@ export class TrackSegment extends PathSegment {
|
|||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawAABB() {
|
||||||
|
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
|
||||||
|
color: "lime",
|
||||||
|
});
|
||||||
|
this.doodler.drawCircle(this.aabb.center, 2, {
|
||||||
|
color: "cyan",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
serialize(): SerializedTrackSegment {
|
serialize(): SerializedTrackSegment {
|
||||||
return {
|
return {
|
||||||
p: this.points.map((p) => p.array()),
|
p: this.points.map((p) => p.array()),
|
||||||
@@ -431,6 +514,7 @@ export class TrackSegment extends PathSegment {
|
|||||||
rotate(angle: number | Vector) {
|
rotate(angle: number | Vector) {
|
||||||
const [p1, p2, p3, p4] = this.points;
|
const [p1, p2, p3, p4] = this.points;
|
||||||
let newP2;
|
let newP2;
|
||||||
|
|
||||||
if (angle instanceof Vector) {
|
if (angle instanceof Vector) {
|
||||||
const tan = angle;
|
const tan = angle;
|
||||||
angle = tan.heading() - (this.lastHeading ?? 0);
|
angle = tan.heading() - (this.lastHeading ?? 0);
|
||||||
@@ -467,7 +551,7 @@ export class TrackSegment extends PathSegment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rotateAboutPoint(angle: number, point: Vector) {
|
rotateAboutPoint(angle: number, point: Vector) {
|
||||||
if (!this.points.includes(point)) return;
|
// if (!this.points.includes(point)) return;
|
||||||
point = point.copy();
|
point = point.copy();
|
||||||
this.points.forEach((p, i) => {
|
this.points.forEach((p, i) => {
|
||||||
const relativePoint = Vector.sub(p, point);
|
const relativePoint = Vector.sub(p, point);
|
||||||
|
@@ -3,7 +3,7 @@ import { Train, TrainCar } from "./train.ts";
|
|||||||
import { getContextItem } from "../lib/context.ts";
|
import { getContextItem } from "../lib/context.ts";
|
||||||
import { ResourceManager } from "../lib/resources.ts";
|
import { ResourceManager } from "../lib/resources.ts";
|
||||||
import { debug } from "node:console";
|
import { debug } from "node:console";
|
||||||
import { averageAngles } from "../math/lerp.ts";
|
import { averageAngles, lerpAngle } from "../math/lerp.ts";
|
||||||
|
|
||||||
export class LargeLady extends TrainCar {
|
export class LargeLady extends TrainCar {
|
||||||
scale = 1;
|
scale = 1;
|
||||||
@@ -68,8 +68,12 @@ export class LargeLady extends TrainCar {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
this.leading = 23;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawAngle?: number;
|
||||||
|
|
||||||
override draw(): void {
|
override draw(): void {
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
for (const b of this.bogies) {
|
for (const b of this.bogies) {
|
||||||
@@ -95,11 +99,13 @@ export class LargeLady extends TrainCar {
|
|||||||
if (debug.bogies) return;
|
if (debug.bogies) return;
|
||||||
|
|
||||||
const difAngle = Vector.sub(a.pos, b.pos).heading();
|
const difAngle = Vector.sub(a.pos, b.pos).heading();
|
||||||
|
if (this.drawAngle == undefined) this.drawAngle = b.angle + Math.PI;
|
||||||
const origin = b.pos.copy().add(new Vector(33, 0).rotate(difAngle));
|
const origin = b.pos.copy().add(new Vector(33, 0).rotate(difAngle));
|
||||||
const angle = b.angle;
|
const angle = b.angle;
|
||||||
const avgAngle = averageAngles(difAngle, angle) + Math.PI;
|
const avgAngle = averageAngles(difAngle, angle) + Math.PI;
|
||||||
|
this.drawAngle = lerpAngle(this.drawAngle, avgAngle, .2);
|
||||||
|
|
||||||
doodler.drawRotated(origin, avgAngle, () => {
|
doodler.drawRotated(origin, this.drawAngle, () => {
|
||||||
this.sprite
|
this.sprite
|
||||||
? doodler.drawSprite(
|
? doodler.drawSprite(
|
||||||
this.img,
|
this.img,
|
||||||
@@ -121,7 +127,7 @@ export class LargeLady extends TrainCar {
|
|||||||
// doodler.drawCircle(origin, 4, { color: "blue" });
|
// doodler.drawCircle(origin, 4, { color: "blue" });
|
||||||
|
|
||||||
doodler.deferDrawing(() => {
|
doodler.deferDrawing(() => {
|
||||||
doodler.drawRotated(origin, avgAngle + Math.PI, () => {
|
doodler.drawRotated(origin, this.drawAngle! + Math.PI, () => {
|
||||||
doodler.drawSprite(
|
doodler.drawSprite(
|
||||||
this.img,
|
this.img,
|
||||||
new Vector(133, 0),
|
new Vector(133, 0),
|
||||||
@@ -146,7 +152,7 @@ export class LargeLadyTender extends TrainCar {
|
|||||||
height: 23,
|
height: 23,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.leading = 10;
|
this.leading = 19;
|
||||||
}
|
}
|
||||||
|
|
||||||
override draw(): void {
|
override draw(): void {
|
||||||
|
@@ -1,147 +0,0 @@
|
|||||||
import { Doodler, Vector } from "@bearmetal/doodler";
|
|
||||||
import { getContextItem } from "../lib/context.ts";
|
|
||||||
import { Spline, TrackSegment } from "../track/system.ts";
|
|
||||||
|
|
||||||
export class DotFollower {
|
|
||||||
position: Vector;
|
|
||||||
velocity: Vector;
|
|
||||||
acceleration: Vector;
|
|
||||||
maxSpeed: number;
|
|
||||||
maxForce: number;
|
|
||||||
_trailingPoint: number;
|
|
||||||
protected _leadingPoint: number;
|
|
||||||
|
|
||||||
path: Spline<TrackSegment>;
|
|
||||||
|
|
||||||
get trailingPoint() {
|
|
||||||
const desired = this.velocity.copy();
|
|
||||||
desired.normalize();
|
|
||||||
desired.mult(-this._trailingPoint);
|
|
||||||
|
|
||||||
return Vector.add(this.position, desired);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(path: Spline<TrackSegment>, pos: Vector) {
|
|
||||||
this.path = path;
|
|
||||||
this.position = pos;
|
|
||||||
this.velocity = new Vector();
|
|
||||||
this.acceleration = new Vector();
|
|
||||||
this.maxSpeed = 3;
|
|
||||||
this.maxForce = 0.3;
|
|
||||||
|
|
||||||
this._trailingPoint = 0;
|
|
||||||
this._leadingPoint = 0;
|
|
||||||
|
|
||||||
this.init();
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
|
||||||
}
|
|
||||||
|
|
||||||
move(dt: number) {
|
|
||||||
dt *= 10;
|
|
||||||
const force = calculatePathForce(this, this.path.points);
|
|
||||||
this.applyForce(force.mult(dt));
|
|
||||||
this.velocity.limit(this.maxSpeed);
|
|
||||||
this.acceleration.limit(this.maxForce);
|
|
||||||
this.velocity.add(this.acceleration.copy().mult(dt));
|
|
||||||
this.position.add(this.velocity.copy().mult(dt));
|
|
||||||
this.edges();
|
|
||||||
}
|
|
||||||
|
|
||||||
edges() {
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
|
||||||
|
|
||||||
if (this.position.x > doodler.width) this.position.x = 0;
|
|
||||||
if (this.position.y > doodler.height) this.position.y = 0;
|
|
||||||
if (this.position.x < 0) this.position.x = doodler.width;
|
|
||||||
if (this.position.y < 0) this.position.y = doodler.height;
|
|
||||||
}
|
|
||||||
|
|
||||||
draw() {
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
|
||||||
doodler.drawRotated(this.position, this.velocity.heading() || 0, () => {
|
|
||||||
doodler.fillCenteredRect(this.position, 20, 20, { fillColor: "white" });
|
|
||||||
});
|
|
||||||
for (const point of this.path.points) {
|
|
||||||
doodler.drawCircle(point, 4, { color: "red", weight: 3 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
applyForce(force: Vector) {
|
|
||||||
this.velocity.add(force);
|
|
||||||
}
|
|
||||||
|
|
||||||
static edges(point: Vector, width: number, height: number) {
|
|
||||||
if (point.x > width) point.x = 0;
|
|
||||||
if (point.y > height) point.y = 0;
|
|
||||||
if (point.x < 0) point.x = width;
|
|
||||||
if (point.y < 0) point.y = height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function closestPointOnLineSegment(p: Vector, a: Vector, b: Vector): Vector {
|
|
||||||
// Vector AB
|
|
||||||
// const AB = { x: b.x - a.x, y: b.y - a.y };
|
|
||||||
const AB = Vector.sub(b, a);
|
|
||||||
// Vector AP
|
|
||||||
// const AP = { x: p.x - a.x, y: p.y - a.y };
|
|
||||||
const AP = Vector.sub(p, a);
|
|
||||||
// Dot product of AP and AB
|
|
||||||
// const AB_AB = AB.x * AB.x + AB.y * AB.y;
|
|
||||||
const AB_AB = Vector.dot(AB, AB);
|
|
||||||
// const AP_AB = AP.x * AB.x + AP.y * AB.y;
|
|
||||||
const AP_AB = Vector.dot(AP, AB);
|
|
||||||
// Project AP onto AB
|
|
||||||
const t = AP_AB / AB_AB;
|
|
||||||
|
|
||||||
// Clamp t to the range [0, 1] to restrict to the segment
|
|
||||||
const tClamped = Math.max(0, Math.min(1, t));
|
|
||||||
|
|
||||||
// Closest point on the segment
|
|
||||||
return new Vector({ x: a.x + AB.x * tClamped, y: a.y + AB.y * tClamped });
|
|
||||||
}
|
|
||||||
|
|
||||||
function calculatePathForce(f: DotFollower, path: Vector[]) {
|
|
||||||
let closestPoint: Vector = path[0];
|
|
||||||
let minDistance = Infinity;
|
|
||||||
|
|
||||||
// Loop through each segment to find the closest point on the path
|
|
||||||
for (let i = 0; i < path.length - 1; i++) {
|
|
||||||
const segmentStart = path[i];
|
|
||||||
const segmentEnd = path[i + 1];
|
|
||||||
|
|
||||||
// Find the closest point on the segment
|
|
||||||
const closest = closestPointOnLineSegment(
|
|
||||||
f.position,
|
|
||||||
segmentStart,
|
|
||||||
segmentEnd,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate the distance from the follower to the closest point
|
|
||||||
// const distance = Math.sqrt(
|
|
||||||
// Math.pow(follower.position.x - closest.x, 2) +
|
|
||||||
// Math.pow(follower.position.y - closest.y, 2),
|
|
||||||
// );
|
|
||||||
|
|
||||||
const distance = Vector.dist(f.position, closest);
|
|
||||||
|
|
||||||
// Track the closest point
|
|
||||||
if (distance < minDistance) {
|
|
||||||
minDistance = distance;
|
|
||||||
closestPoint = closest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate the force to apply toward the closest point
|
|
||||||
// const force = {
|
|
||||||
// x: closestPoint.x - f.position.x,
|
|
||||||
// y: closestPoint.y - f.position.y,
|
|
||||||
// };
|
|
||||||
const force = Vector.sub(closestPoint, f.position);
|
|
||||||
|
|
||||||
// Normalize the force and apply a magnitude (this will depend on your desired strength)
|
|
||||||
const magnitude = 100; // Adjust this based on your needs
|
|
||||||
force.setMag(magnitude);
|
|
||||||
return force;
|
|
||||||
}
|
|
75
src/train/newTrain/Bogie.ts
Normal file
75
src/train/newTrain/Bogie.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { Vector } from "@bearmetal/doodler";
|
||||||
|
import { getContextItem } from "../../lib/context.ts";
|
||||||
|
import { Debuggable } from "../../lib/debuggable.ts";
|
||||||
|
|
||||||
|
export class Bogie extends Debuggable {
|
||||||
|
private pos: Vector = new Vector(0, 0);
|
||||||
|
private prevPos: Vector = this.pos.copy();
|
||||||
|
public dir: Vector = new Vector(0, 0);
|
||||||
|
public velocity: Vector = new Vector(0, 0);
|
||||||
|
private acceleration: Vector = new Vector(0, 0);
|
||||||
|
private damping: number = 0.999;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public leadingOffset: number,
|
||||||
|
public trailingOffset: number,
|
||||||
|
public mass: number = 1,
|
||||||
|
) {
|
||||||
|
super("bogies");
|
||||||
|
}
|
||||||
|
|
||||||
|
get position() {
|
||||||
|
return this.pos;
|
||||||
|
}
|
||||||
|
set position(v: Vector) {
|
||||||
|
this.pos.set(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
applyForce(force: Vector) {
|
||||||
|
this.acceleration.add(force.copy().div(this.mass));
|
||||||
|
}
|
||||||
|
|
||||||
|
update(dt: number) {
|
||||||
|
// Compute velocity from previous position
|
||||||
|
this.velocity = this.pos.copy().sub(this.prevPos);
|
||||||
|
|
||||||
|
// Apply friction to the velocity instead of velocity storage
|
||||||
|
this.velocity.mult(this.damping);
|
||||||
|
|
||||||
|
// Apply Verlet integration with forces
|
||||||
|
const temp = this.pos.copy();
|
||||||
|
this.pos.add(this.velocity.add(this.acceleration.copy().mult(dt * dt)));
|
||||||
|
this.prevPos = temp; // Store previous position
|
||||||
|
this.acceleration.set(0, 0); // Reset acceleration after update
|
||||||
|
}
|
||||||
|
|
||||||
|
draw() {}
|
||||||
|
debugDraw() {
|
||||||
|
const d = getContextItem("doodler");
|
||||||
|
d.deferDrawing(() => {
|
||||||
|
d.dot(this.pos, {
|
||||||
|
color: this instanceof Driver ? "red" : "white",
|
||||||
|
weight: 3,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Driver extends Bogie {
|
||||||
|
public drivingForce: number = 0;
|
||||||
|
|
||||||
|
override update(dt: number): void {
|
||||||
|
const force = this.dir.copy();
|
||||||
|
force.setMag(this.drivingForce);
|
||||||
|
this.applyForce(force);
|
||||||
|
super.update(dt);
|
||||||
|
}
|
||||||
|
updateDirection(dir: Vector) {
|
||||||
|
if (this.dir.dot(dir) < -0.5) {
|
||||||
|
// If new dir is nearly opposite, blend gradually
|
||||||
|
this.dir = this.dir.mult(0.9).add(dir.mult(0.1)).normalize();
|
||||||
|
} else {
|
||||||
|
this.dir = dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
70
src/train/newTrain/Train.ts
Normal file
70
src/train/newTrain/Train.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import { getContextItem } from "../../lib/context.ts";
|
||||||
|
import { TrackSystem } from "../../track/system.ts";
|
||||||
|
import { Debuggable } from "../../lib/debuggable.ts";
|
||||||
|
import { PhysicsSolver } from "../../physics/solver.ts";
|
||||||
|
import { MoveTask, TrackConstraint, TrainTask } from "./physics.ts";
|
||||||
|
import { TrainCar } from "./TrainCar.ts";
|
||||||
|
import { Bogie } from "./Bogie.ts";
|
||||||
|
|
||||||
|
export class Train extends Debuggable {
|
||||||
|
private _bogies: Bogie[];
|
||||||
|
get bogies() {
|
||||||
|
return this._bogies;
|
||||||
|
}
|
||||||
|
|
||||||
|
private solver: PhysicsSolver;
|
||||||
|
|
||||||
|
constructor(private track: TrackSystem, private cars: TrainCar[] = []) {
|
||||||
|
super("train");
|
||||||
|
this._bogies = this.recalculateBogies();
|
||||||
|
this.solver = new PhysicsSolver();
|
||||||
|
this._bogies.forEach((b) =>
|
||||||
|
this.solver.addTask(new TrackConstraint(b, this.track))
|
||||||
|
);
|
||||||
|
let prev: Bogie | null = null;
|
||||||
|
for (const car of this.cars) {
|
||||||
|
for (const b of car.bogies) {
|
||||||
|
if (prev) {
|
||||||
|
this.solver.addTask(
|
||||||
|
new TrainTask(prev, b, prev.trailingOffset + b.leadingOffset),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
prev = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.solver.addTask(new MoveTask(this));
|
||||||
|
this.solver.maxTickStep = 1 / 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
addCar(car: TrainCar, toFront = false) {
|
||||||
|
this.cars.push(car);
|
||||||
|
for (const b of toFront ? car.bogies.reverse() : car.bogies) {
|
||||||
|
this.solver.addTask(new TrackConstraint(b, this.track));
|
||||||
|
}
|
||||||
|
this._bogies = this.recalculateBogies();
|
||||||
|
}
|
||||||
|
removeCar(car: TrainCar) {
|
||||||
|
this.cars = this.cars.filter((c) => c !== car);
|
||||||
|
}
|
||||||
|
|
||||||
|
recalculateBogies() {
|
||||||
|
return this.cars.flatMap((c) => c.bogies);
|
||||||
|
}
|
||||||
|
|
||||||
|
update(dt: number) {
|
||||||
|
this.solver.solve(dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
draw() {
|
||||||
|
for (const car of this.cars) {
|
||||||
|
car.draw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debugDraw(): void {
|
||||||
|
const d = getContextItem("doodler");
|
||||||
|
// this.track._segments.forEach((s) =>
|
||||||
|
// s.evenPoints.forEach((p) => d.dot(p[0], { color: "lime", weight: 2 }))
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
}
|
68
src/train/newTrain/TrainCar.ts
Normal file
68
src/train/newTrain/TrainCar.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import { Vector } from "@bearmetal/doodler";
|
||||||
|
import { getContextItem } from "../../lib/context.ts";
|
||||||
|
import { Debuggable } from "../../lib/debuggable.ts";
|
||||||
|
import { Bogie } from "./Bogie.ts";
|
||||||
|
|
||||||
|
export class TrainCar extends Debuggable {
|
||||||
|
private spriteImg: HTMLImageElement;
|
||||||
|
private sprite?: ISprite;
|
||||||
|
constructor(
|
||||||
|
public bogies: Bogie[] = [],
|
||||||
|
) {
|
||||||
|
super("car");
|
||||||
|
const res = getContextItem("resources");
|
||||||
|
this.spriteImg = res.get("snr:sprite/engine");
|
||||||
|
this.sprite = {
|
||||||
|
at: new Vector(80, 20),
|
||||||
|
width: 70,
|
||||||
|
height: 20,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
draw(): void {
|
||||||
|
const doodler = getContextItem("doodler");
|
||||||
|
for (const b of this.bogies) {
|
||||||
|
doodler.drawCircle(b.position, 4, { color: "blue" });
|
||||||
|
doodler.fillText(
|
||||||
|
b.velocity.mag().toFixed(1).toString(),
|
||||||
|
b.position.copy().add(10, 10),
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
color: "white",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const angle = Vector.sub(this.bogies[1].position, this.bogies[0].position)
|
||||||
|
.heading();
|
||||||
|
const origin = Vector.lerp(
|
||||||
|
this.bogies[0].position,
|
||||||
|
this.bogies[1].position,
|
||||||
|
.5,
|
||||||
|
);
|
||||||
|
doodler.drawRotated(origin, angle, () => {
|
||||||
|
this.sprite
|
||||||
|
? doodler.drawSprite(
|
||||||
|
this.spriteImg,
|
||||||
|
this.sprite.at,
|
||||||
|
this.sprite.width,
|
||||||
|
this.sprite.height,
|
||||||
|
origin.copy().sub(
|
||||||
|
this.sprite.width / 2,
|
||||||
|
this.sprite.height / 2,
|
||||||
|
),
|
||||||
|
this.sprite.width,
|
||||||
|
this.sprite.height,
|
||||||
|
)
|
||||||
|
: doodler.drawImage(
|
||||||
|
this.spriteImg,
|
||||||
|
origin.copy().sub(
|
||||||
|
this.spriteImg.width / 2,
|
||||||
|
this.spriteImg.height / 2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
debugDraw(): void {
|
||||||
|
}
|
||||||
|
}
|
138
src/train/newTrain/physics.ts
Normal file
138
src/train/newTrain/physics.ts
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import { Vector } from "@bearmetal/doodler";
|
||||||
|
import { SolverTask } from "../../physics/solver.ts";
|
||||||
|
import { Bogie, Driver } from "./Bogie.ts";
|
||||||
|
import { TrackSystem } from "../../track/system.ts";
|
||||||
|
import { clamp } from "../../math/clamp.ts";
|
||||||
|
import { Train } from "./Train.ts";
|
||||||
|
|
||||||
|
export class TrackConstraint extends SolverTask {
|
||||||
|
detached: boolean = false;
|
||||||
|
detachmentThreshold: number = 1;
|
||||||
|
reattachmentThreshold: number = 0.1;
|
||||||
|
pathTag?: tag;
|
||||||
|
constructor(
|
||||||
|
private node: Bogie,
|
||||||
|
private track: TrackSystem,
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.priority = 1;
|
||||||
|
this.iterations = 1;
|
||||||
|
}
|
||||||
|
override apply(): boolean {
|
||||||
|
if (this.detached) {
|
||||||
|
if (this.node.velocity.mag() < this.reattachmentThreshold) {
|
||||||
|
this.detached = false;
|
||||||
|
} else {
|
||||||
|
this.active = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (this.node.velocity.mag() < 0) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
const searchRadius = 10;
|
||||||
|
const nearbySegments = this.track.grid.query(
|
||||||
|
this.node.position,
|
||||||
|
searchRadius,
|
||||||
|
this.pathTag,
|
||||||
|
);
|
||||||
|
|
||||||
|
let closestSegment: GridItem | null = null;
|
||||||
|
let closestDistance = Infinity;
|
||||||
|
let projectedPoint: Vector | null = null;
|
||||||
|
for (const seg of nearbySegments) {
|
||||||
|
const candidate = this.closestPointOnSegment(seg, this.node.position);
|
||||||
|
const distance = this.node.position.dist(candidate);
|
||||||
|
if (distance < closestDistance) {
|
||||||
|
closestSegment = seg;
|
||||||
|
closestDistance = distance;
|
||||||
|
projectedPoint = candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (closestSegment && projectedPoint) {
|
||||||
|
const correctionForce = Vector.sub(projectedPoint, this.node.position);
|
||||||
|
const correctionMag = correctionForce.mag() * this.node.mass;
|
||||||
|
|
||||||
|
if (correctionMag > this.detachmentThreshold) {
|
||||||
|
this.detached = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.node.position = projectedPoint;
|
||||||
|
const pathDir = Vector.sub(closestSegment[1], closestSegment[0])
|
||||||
|
.normalize();
|
||||||
|
this.node.velocity.set(pathDir.mult(this.node.velocity.dot(pathDir)));
|
||||||
|
if (this.node instanceof Driver) {
|
||||||
|
this.node.updateDirection(pathDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
closestPointOnSegment([a, b]: GridItem, point: Vector): Vector {
|
||||||
|
const ab = Vector.sub(b, a);
|
||||||
|
const ap = Vector.sub(point, a);
|
||||||
|
const t = clamp(ab.dot(ap) / ab.magSq(), 0, 1);
|
||||||
|
|
||||||
|
return Vector.add(a, ab.mult(t));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MoveTask extends SolverTask {
|
||||||
|
constructor(
|
||||||
|
private train: Train,
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.priority = 2;
|
||||||
|
this.iterations = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
override apply(dt: number): boolean {
|
||||||
|
for (const bogie of this.train.bogies) {
|
||||||
|
bogie.update(dt);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TrainTask extends SolverTask {
|
||||||
|
constructor(
|
||||||
|
private a: Bogie,
|
||||||
|
private b: Bogie,
|
||||||
|
private restLength: number,
|
||||||
|
private tolerance = 0,
|
||||||
|
private solid = false,
|
||||||
|
private breakThreshold = 10,
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.iterations = 100;
|
||||||
|
}
|
||||||
|
override apply(): boolean {
|
||||||
|
const delta = Vector.sub(this.b.position, this.a.position);
|
||||||
|
const currentLength = delta.mag();
|
||||||
|
if (currentLength <= this.tolerance) return false;
|
||||||
|
|
||||||
|
const correction = currentLength - this.restLength;
|
||||||
|
const correctionVector = delta.normalize().mult(correction / currentLength);
|
||||||
|
|
||||||
|
const requiredForce = correction / this.restLength;
|
||||||
|
|
||||||
|
if (!this.solid && Math.abs(requiredForce) > this.breakThreshold) {
|
||||||
|
this.active = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalMass = this.a.mass + this.b.mass;
|
||||||
|
const aWeight = this.b.mass / totalMass;
|
||||||
|
const bWeight = this.b.mass / totalMass;
|
||||||
|
|
||||||
|
this.a.position.add(correctionVector.copy().mult(aWeight));
|
||||||
|
this.b.position.sub(correctionVector.copy().mult(bWeight));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@@ -12,9 +12,11 @@ export class Train extends Debuggable {
|
|||||||
path: Spline<TrackSegment>;
|
path: Spline<TrackSegment>;
|
||||||
t: number;
|
t: number;
|
||||||
|
|
||||||
spacing = 5;
|
spacing = 0;
|
||||||
|
|
||||||
speed = 0;
|
speed = 5;
|
||||||
|
|
||||||
|
aabb!: AABB;
|
||||||
|
|
||||||
get segments() {
|
get segments() {
|
||||||
return Array.from(
|
return Array.from(
|
||||||
@@ -25,7 +27,7 @@ export class Train extends Debuggable {
|
|||||||
constructor(track: Spline<TrackSegment>, cars: TrainCar[], t = 0) {
|
constructor(track: Spline<TrackSegment>, cars: TrainCar[], t = 0) {
|
||||||
super("train", "path");
|
super("train", "path");
|
||||||
this.path = track;
|
this.path = track;
|
||||||
this.path.pointSpacing = 5;
|
this.path.pointSpacing = 4;
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
this.t = this.cars.reduce((acc, c) => acc + c.length, 0) +
|
this.t = this.cars.reduce((acc, c) => acc + c.length, 0) +
|
||||||
(this.cars.length - 1) * this.spacing;
|
(this.cars.length - 1) * this.spacing;
|
||||||
@@ -51,6 +53,8 @@ export class Train extends Debuggable {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
this.updateAABB();
|
||||||
}
|
}
|
||||||
|
|
||||||
move(dTime: number) {
|
move(dTime: number) {
|
||||||
@@ -72,10 +76,27 @@ export class Train extends Debuggable {
|
|||||||
// car.draw();
|
// car.draw();
|
||||||
|
|
||||||
currentOffset += car.moveAlongPath(this.t - currentOffset) +
|
currentOffset += car.moveAlongPath(this.t - currentOffset) +
|
||||||
this.spacing / this.path.pointSpacing +
|
(this.spacing / this.path.pointSpacing);
|
||||||
car.leading / this.path.pointSpacing;
|
|
||||||
}
|
}
|
||||||
// this.draw();
|
// this.draw();
|
||||||
|
this.updateAABB();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateAABB() {
|
||||||
|
const minX = Math.min(...this.cars.map((c) => c.aabb.x));
|
||||||
|
const maxX = Math.max(...this.cars.map((c) => c.aabb.x + c.aabb.width));
|
||||||
|
const minY = Math.min(...this.cars.map((c) => c.aabb.y));
|
||||||
|
const maxY = Math.max(...this.cars.map((c) => c.aabb.y + c.aabb.height));
|
||||||
|
this.aabb = {
|
||||||
|
pos: new Vector(minX, minY),
|
||||||
|
x: minX,
|
||||||
|
y: minY,
|
||||||
|
width: maxX - minX,
|
||||||
|
height: maxY - minY,
|
||||||
|
center: new Vector(minX, minY).add(
|
||||||
|
new Vector(maxX - minX, maxY - minY).div(2),
|
||||||
|
),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw() {
|
// draw() {
|
||||||
@@ -133,6 +154,16 @@ export class Train extends Debuggable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (debug.aabb) {
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
|
||||||
|
color: "orange",
|
||||||
|
});
|
||||||
|
doodler.drawCircle(this.aabb.center, 2, {
|
||||||
|
color: "lime",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
real2Track(length: number) {
|
real2Track(length: number) {
|
||||||
@@ -164,6 +195,8 @@ export class TrainCar extends Debuggable {
|
|||||||
|
|
||||||
train?: Train;
|
train?: Train;
|
||||||
|
|
||||||
|
aabb!: AABB;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
length: number,
|
length: number,
|
||||||
trailing: number,
|
trailing: number,
|
||||||
@@ -191,6 +224,8 @@ export class TrainCar extends Debuggable {
|
|||||||
length: trailing,
|
length: trailing,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
this.updateAABB();
|
||||||
}
|
}
|
||||||
|
|
||||||
get length() {
|
get length() {
|
||||||
@@ -209,6 +244,59 @@ export class TrainCar extends Debuggable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateAABB() {
|
||||||
|
let minX = Infinity;
|
||||||
|
let maxX = -Infinity;
|
||||||
|
let minY = Infinity;
|
||||||
|
let maxY = -Infinity;
|
||||||
|
|
||||||
|
this.bogies.forEach((bogie, index) => {
|
||||||
|
// Unit vector in the direction the bogie is facing.
|
||||||
|
const u = new Vector(Math.cos(bogie.angle), Math.sin(bogie.angle));
|
||||||
|
// Perpendicular vector (to thicken the rectangle).
|
||||||
|
const v = new Vector(-Math.sin(bogie.angle), Math.cos(bogie.angle));
|
||||||
|
|
||||||
|
// For the first bogie, extend in the opposite direction by this.leading.
|
||||||
|
let front = bogie.pos.copy();
|
||||||
|
if (index === 0) {
|
||||||
|
front = front.sub(u.copy().rotate(Math.PI).mult(this.leading));
|
||||||
|
}
|
||||||
|
// Rear point is at bogie.pos plus the bogie length.
|
||||||
|
const rear = bogie.pos.copy().add(
|
||||||
|
u.copy().rotate(Math.PI).mult(bogie.length),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Calculate half the height to offset from the center line.
|
||||||
|
const halfHeight = this.imgHeight / 2;
|
||||||
|
|
||||||
|
// Calculate the four corners of the rectangle.
|
||||||
|
const corners = [
|
||||||
|
front.copy().add(v.copy().mult(halfHeight)),
|
||||||
|
front.copy().add(v.copy().mult(-halfHeight)),
|
||||||
|
rear.copy().add(v.copy().mult(halfHeight)),
|
||||||
|
rear.copy().add(v.copy().mult(-halfHeight)),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Update the overall AABB limits.
|
||||||
|
corners.forEach((corner) => {
|
||||||
|
minX = Math.min(minX, corner.x);
|
||||||
|
minY = Math.min(minY, corner.y);
|
||||||
|
maxX = Math.max(maxX, corner.x);
|
||||||
|
maxY = Math.max(maxY, corner.y);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this.aabb = {
|
||||||
|
pos: new Vector(minX, minY),
|
||||||
|
center: new Vector(minX, minY).add(
|
||||||
|
new Vector(maxX - minX, maxY - minY).div(2),
|
||||||
|
),
|
||||||
|
x: minX,
|
||||||
|
y: minY,
|
||||||
|
width: maxX - minX,
|
||||||
|
height: maxY - minY,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
moveAlongPath(t: number, initial = false): number {
|
moveAlongPath(t: number, initial = false): number {
|
||||||
if (!this.train) return 0;
|
if (!this.train) return 0;
|
||||||
let offset = this.leading / this.train.path.pointSpacing;
|
let offset = this.leading / this.train.path.pointSpacing;
|
||||||
@@ -224,6 +312,7 @@ export class TrainCar extends Debuggable {
|
|||||||
}
|
}
|
||||||
this.segments.add(a.segmentId);
|
this.segments.add(a.segmentId);
|
||||||
}
|
}
|
||||||
|
this.updateAABB();
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +383,26 @@ export class TrainCar extends Debuggable {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (debug.aabb) {
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
|
||||||
|
color: "white",
|
||||||
|
weight: .5,
|
||||||
|
});
|
||||||
|
doodler.drawCircle(this.aabb.center, 2, {
|
||||||
|
color: "yellow",
|
||||||
|
});
|
||||||
|
doodler.fillText(
|
||||||
|
this.aabb.width.toFixed(1).toString(),
|
||||||
|
this.aabb.center.copy().add(10, 10),
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
color: "white",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug.angles) {
|
if (debug.angles) {
|
||||||
@@ -323,8 +432,10 @@ export class TrainCar extends Debuggable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ISprite {
|
declare global {
|
||||||
|
interface ISprite {
|
||||||
at: Vector;
|
at: Vector;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
25
src/types.ts
25
src/types.ts
@@ -18,10 +18,35 @@ declare global {
|
|||||||
|
|
||||||
type Debug = {
|
type Debug = {
|
||||||
track: boolean;
|
track: boolean;
|
||||||
|
segment: boolean;
|
||||||
train: boolean;
|
train: boolean;
|
||||||
car: boolean;
|
car: boolean;
|
||||||
path: boolean;
|
path: boolean;
|
||||||
bogies: boolean;
|
bogies: boolean;
|
||||||
angles: boolean;
|
angles: boolean;
|
||||||
|
aabb: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type AABB = {
|
||||||
|
pos: Vector;
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
center: Vector;
|
||||||
|
};
|
||||||
|
|
||||||
|
type tag = string | number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyMixins(derivedCtor: any, baseCtors: any[]) {
|
||||||
|
baseCtors.forEach((baseCtor) => {
|
||||||
|
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
|
||||||
|
Object.defineProperty(
|
||||||
|
derivedCtor.prototype,
|
||||||
|
name,
|
||||||
|
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ?? {},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@@ -1 +0,0 @@
|
|||||||
[{"p":[[633.178123792903,100.31612523465073,0],[699.1781237929027,100.31612523465073,0],[762.9292283279814,117.39818221141712,0],[820.0869049777544,150.39818221141715,0]],"id":"83b9fc8c-778e-4e5e-a3db-1199863a2e13","bNeighbors":["a149432a-e04e-481a-ada2-c05a30ffb4c0"],"fNeighbors":["dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117"]},{"p":[[820.0869049777544,150.39818221141715,0],[877.2445816275272,183.398182211417,0],[923.9136291858395,230.06722976972924,0],[956.9136291858396,287.2249064195022,0]],"id":"dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117","bNeighbors":["83b9fc8c-778e-4e5e-a3db-1199863a2e13"],"fNeighbors":["1b6409da-5c9d-4b1d-b460-fcc5e40eaee4"]},{"p":[[956.9136291858396,287.2249064195022,0],[989.9136291858396,344.3825830692749,0],[1006.9956861626059,408.13368760435344,0],[1006.9956861626064,474.1336876043541,0]],"id":"1b6409da-5c9d-4b1d-b460-fcc5e40eaee4","bNeighbors":["dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117"],"fNeighbors":["24861245-ca99-4f97-9e0a-5828773fcf7a"]},{"p":[[1006.9956861626064,474.1336876043541,0],[1006.9956861626068,540.1336876043545,0],[989.9136291858408,603.884792139433,0],[956.9136291858412,661.042468789207,0]],"id":"24861245-ca99-4f97-9e0a-5828773fcf7a","bNeighbors":["1b6409da-5c9d-4b1d-b460-fcc5e40eaee4"],"fNeighbors":["98ad61a1-f2e7-4c53-9caa-022faa15ce68"]},{"p":[[956.9136291858412,661.042468789207,0],[923.9136291858417,718.2001454389806,0],[877.2445816275301,764.8691929972932,0],[820.0869049777573,797.8691929972947,0]],"id":"98ad61a1-f2e7-4c53-9caa-022faa15ce68","bNeighbors":["24861245-ca99-4f97-9e0a-5828773fcf7a"],"fNeighbors":["7f11837f-f043-468f-b1cb-254236501199"]},{"p":[[820.0869049777573,797.8691929972947,0],[762.9292283279847,830.8691929972961,0],[699.1781237929068,847.9512499740634,0],[633.1781237929063,847.9512499740653,0]],"id":"7f11837f-f043-468f-b1cb-254236501199","bNeighbors":["98ad61a1-f2e7-4c53-9caa-022faa15ce68"],"fNeighbors":["d89abe0b-7d84-4921-b690-23cdb6ec7c4a"]},{"p":[[633.1781237929063,847.9512499740653,0],[567.1781237929059,847.9512499740671,0],[503.4270192578273,830.8691929973024,0],[446.26934260805274,797.869192997304,0]],"id":"d89abe0b-7d84-4921-b690-23cdb6ec7c4a","bNeighbors":["7f11837f-f043-468f-b1cb-254236501199"],"fNeighbors":["3fb143a5-3d6e-409c-88b1-c20e9005a14f"]},{"p":[[446.26934260805274,797.869192997304,0],[389.1116659582787,764.869192997306,0],[342.44261839996534,718.2001454389954,0],[309.4426183999627,661.0424687892232,0]],"id":"3fb143a5-3d6e-409c-88b1-c20e9005a14f","bNeighbors":["d89abe0b-7d84-4921-b690-23cdb6ec7c4a"],"fNeighbors":["de299c76-bdbd-4b59-9514-722e3292ed49"]},{"p":[[309.4426183999627,661.0424687892232,0],[276.4426183999604,603.8847921394515,0],[259.36056142319165,540.1336876043738,0],[259.36056142318864,474.13368760437345,0]],"id":"de299c76-bdbd-4b59-9514-722e3292ed49","bNeighbors":["3fb143a5-3d6e-409c-88b1-c20e9005a14f"],"fNeighbors":["e573e278-4039-425f-89f5-245f96fe9096"]},{"p":[[259.36056142318864,474.13368760437345,0],[259.36056142318563,408.13368760437334,0],[276.4426183999492,344.38258306929436,0],[309.44261839994635,287.22490641951936,0]],"id":"e573e278-4039-425f-89f5-245f96fe9096","bNeighbors":["de299c76-bdbd-4b59-9514-722e3292ed49"],"fNeighbors":["87a06884-75e3-4802-ba38-3be366bac467"]},{"p":[[309.44261839994635,287.22490641951936,0],[342.4426183999434,230.06722976974456,0],[389.111665958253,183.39818221143025,0],[446.26934260802403,150.39818221142656,0]],"id":"87a06884-75e3-4802-ba38-3be366bac467","bNeighbors":["e573e278-4039-425f-89f5-245f96fe9096"],"fNeighbors":["a149432a-e04e-481a-ada2-c05a30ffb4c0"]},{"p":[[446.26934260802403,150.39818221142656,0],[503.42701925779517,117.3981822114228,0],[567.1781237928724,100.31612523465262,0],[633.1781237928726,100.31612523464773,0]],"id":"a149432a-e04e-481a-ada2-c05a30ffb4c0","bNeighbors":["87a06884-75e3-4802-ba38-3be366bac467"],"fNeighbors":["83b9fc8c-778e-4e5e-a3db-1199863a2e13"]}]
|
|
Reference in New Issue
Block a user