Files
rally-racer/scripts/track_gen.py

41 lines
1.1 KiB
Python

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()