From dde690818b9aa33a8a0a9016aa8e40b1baf2bf96 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sat, 18 Oct 2025 21:56:14 +0200 Subject: [PATCH] feat: add simple track generator --- assets/tracks/simple/meta.json | 8 +- assets/tracks/simple/track.json | 260 ++++++++++++++++++++++++++++---- scripts/track_gen.py | 40 +++++ 3 files changed, 272 insertions(+), 36 deletions(-) create mode 100644 scripts/track_gen.py diff --git a/assets/tracks/simple/meta.json b/assets/tracks/simple/meta.json index ee789cb..0fb5482 100644 --- a/assets/tracks/simple/meta.json +++ b/assets/tracks/simple/meta.json @@ -2,12 +2,12 @@ "name": "Simple Track", "start": { "pos": [ - 0, - -3 + 30, + 0 ], "direction": [ - 1, - 0 + 0, + 1 ] } } \ No newline at end of file diff --git a/assets/tracks/simple/track.json b/assets/tracks/simple/track.json index 86a9515..9ee67c8 100644 --- a/assets/tracks/simple/track.json +++ b/assets/tracks/simple/track.json @@ -3,59 +3,255 @@ "type": "road", "pts": [ [ - 0, - -3, - 0, - -1, + 30.0, + 0.0, + 1.0, + 0.0, 1 ], [ - 2, - -2, - 1, - -1, + 29.544, + 5.209, + 0.985, + 0.174, 1 ], [ - 3, - 0, - 1, - 0, + 28.191, + 10.261, + 0.94, + 0.342, 1 ], [ - 2, - 2, - 1, - 1, + 25.981, + 15.0, + 0.866, + 0.5, 1 ], [ - 0, - 3, - 0, - 1, + 22.981, + 19.284, + 0.766, + 0.643, 1 ], [ - -2, - 2, - -1, - 1, + 19.284, + 22.981, + 0.643, + 0.766, 1 ], [ - -3, - 0, - -1, - 0, + 15.0, + 25.981, + 0.5, + 0.866, 1 ], [ - -2, - -2, - -1, - -1, + 10.261, + 28.191, + 0.342, + 0.94, + 1 + ], + [ + 5.209, + 29.544, + 0.174, + 0.985, + 1 + ], + [ + 0.0, + 30.0, + 0.0, + 1.0, + 1 + ], + [ + -5.209, + 29.544, + -0.174, + 0.985, + 1 + ], + [ + -10.261, + 28.191, + -0.342, + 0.94, + 1 + ], + [ + -15.0, + 25.981, + -0.5, + 0.866, + 1 + ], + [ + -19.284, + 22.981, + -0.643, + 0.766, + 1 + ], + [ + -22.981, + 19.284, + -0.766, + 0.643, + 1 + ], + [ + -25.981, + 15.0, + -0.866, + 0.5, + 1 + ], + [ + -28.191, + 10.261, + -0.94, + 0.342, + 1 + ], + [ + -29.544, + 5.209, + -0.985, + 0.174, + 1 + ], + [ + -30.0, + 0.0, + -1.0, + 0.0, + 1 + ], + [ + -29.544, + -5.209, + -0.985, + -0.174, + 1 + ], + [ + -28.191, + -10.261, + -0.94, + -0.342, + 1 + ], + [ + -25.981, + -15.0, + -0.866, + -0.5, + 1 + ], + [ + -22.981, + -19.284, + -0.766, + -0.643, + 1 + ], + [ + -19.284, + -22.981, + -0.643, + -0.766, + 1 + ], + [ + -15.0, + -25.981, + -0.5, + -0.866, + 1 + ], + [ + -10.261, + -28.191, + -0.342, + -0.94, + 1 + ], + [ + -5.209, + -29.544, + -0.174, + -0.985, + 1 + ], + [ + -0.0, + -30.0, + -0.0, + -1.0, + 1 + ], + [ + 5.209, + -29.544, + 0.174, + -0.985, + 1 + ], + [ + 10.261, + -28.191, + 0.342, + -0.94, + 1 + ], + [ + 15.0, + -25.981, + 0.5, + -0.866, + 1 + ], + [ + 19.284, + -22.981, + 0.643, + -0.766, + 1 + ], + [ + 22.981, + -19.284, + 0.766, + -0.643, + 1 + ], + [ + 25.981, + -15.0, + 0.866, + -0.5, + 1 + ], + [ + 28.191, + -10.261, + 0.94, + -0.342, + 1 + ], + [ + 29.544, + -5.209, + 0.985, + -0.174, 1 ] ] diff --git a/scripts/track_gen.py b/scripts/track_gen.py new file mode 100644 index 0000000..a482d15 --- /dev/null +++ b/scripts/track_gen.py @@ -0,0 +1,40 @@ +import json +from math import radians +from pathlib import Path +from src.utils import ROOT +from src.vec import Vec + + +def gen_circle( + folder: Path, center: Vec, radius: float, n_sides: int, width: float = 1 +): + with open(folder / "track.json", "w") as f: + pts: list[tuple[float, ...]] = [] + + for i in range(n_sides): + angle: float = radians(i / n_sides * 360) + v: Vec = Vec(1, 0).rotate(angle) + pos: Vec = center + v * radius + normal: Vec = v + pts.append((pos.x, pos.y, normal.x, normal.y, width)) + + for i, pt in enumerate(pts): + pts[i] = tuple(round(v, 3) for v in pt) + + json.dump([{"type": "road", "pts": pts}], f, indent=4) + + with open(folder / "meta.json", "r") as f: + meta: dict = json.load(f) + + meta["start"] = {"pos": [radius, 0], "direction": [0, 1]} + with open(folder / "meta.json", "w") as f: + json.dump(meta, f, indent=4) + + +def main(): + folder: Path = ROOT / "assets" / "tracks" / "simple" + gen_circle(folder, Vec(0, 0), 30, 36) + + +if __name__ == "__main__": + main()