# gfc9110/spring-physics-2d architecture

> Architecture diagram of the GitHub repository gfc9110/spring-physics-2d, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gfc9110/spring-physics-2d
- Repository: https://github.com/gfc9110/spring-physics-2d

## Overview

##### Main Components
a. User Interface/Frontend:
- The presence of src/index.html and CSS/SCSS style files (in src/styles) indicates a UI that renders the simulation.
- Editor components (src/editor.ts and src/editorTool.ts) hint at interactive tools for editing or controlling the simulation.
b. Physics Engine Core:
- Files like src/spring.ts, src/world.ts, src/vector.ts, and src/point.ts suggest mathematical and physical computations for the spring-based simulation.
- src/structures.ts implies that there is an internal representation for the simulation state.
c. Input and State Management:
- src/inputs.ts likely manages user interactions (mouse, keyboard) that influence the simulation.
- src/stats.ts might be used for performance monitoring or simulation metrics.
- src/domHelpers.ts provides utility functions for DOM interactions.
d. Build and Configuration Tools:
- Config files such as webpack.config.js, tsconfig.json, package.json, and related webpack scripts indicate module bundling, transpilation, and overall project build configuration.

##### Relationships and Interactions
- The Browser UI (created from index.html and styled by SCSS files) loads and runs the bundled JavaScript (compiled from TypeScript files using webpack).
- User actions (e.g., clicks, drags) are captured in the UI and channeled through inputs.ts, which then trigger updates in the physics engine.
- The simulation core (world.ts, spring.ts, vector.ts) calculates new positions and physics states, updating the display accordingly.
- Editor functionalities (via editor.ts and editorTool.ts) allow on-the-fly adjustments to simulation parameters or objects in the simulation world.
- The relationship flows roughly from User Interaction → Input Handling → Simulation Updates → UI Rendering.
- Support files like domHelpers.ts and stats.ts assist with DOM manipulation and performance metrics respectively.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project clearly divides UI rendering, simulation calculations, user input, and editor customizations into separate modules.
- Modular Design: TypeScript modules under the src directory are organized logically (for physics, UI, inputs, etc.).
- Layered Architecture: At the lowest layer are the core simulation math and physics calculations; the middle tier handles state and input management; and the top layer is the UI/editor interaction.
- Build Abstraction: The use of webpack and tsconfig shows a clear build and compilation pipeline aiding in separation between development and production builds.

##### Relevant Technologies/Frameworks
- TypeScript for writing strongly typed, modular code.
- Webpack for module bundling, with separate configurations for development (webpack.dev.js) and production (webpack.prod.js).
- SCSS for styling.
- The project likely runs in a browser environment, as indicated by index.html and live hosting on Netlify.

- Clear labels for each component:
- "Index (UI/HTML)", "Style / SCSS", "Editor Modules", "Physics Engine (Spring, Vector, World)", "Input/Events Handling", "Build & Configuration Layer".
- From user interactions (Inputs) to simulation updates and back to the UI.
- UI components from logic components.
- Core simulation modules vs. utility/helper modules.
- Build configuration segments as the “infrastructure” layer.
- Optionally, include a section for external integrations (e.g., showing that the app is hosted on Netlify) though the core is self-contained.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Layer
    subgraph "User Interface Layer"
        UI_Index["Index (UI/HTML)"]:::ui
        UI_Styles["Styles (SCSS)"]:::ui
        Editor_Ts["Editor (editor.ts)"]:::ui
        EditorTool["Editor Tool (editorTool.ts)"]:::ui
    end

    %% Simulation Core Layer
    subgraph "Simulation Core Layer"
        subgraph "Physics Engine Core"
            PE_Spring["Spring"]:::physics
            PE_World["World"]:::physics
            PE_Vector["Vector"]:::physics
            PE_Point["Point"]:::physics
            PE_Struct["Structures"]:::physics
        end
        subgraph "Input & State Management"
            Input_Inputs["Inputs"]:::input
            Input_DOM["DOM Helpers"]:::input
            Input_Stats["Stats"]:::input
        end
    end

    %% Build & Configuration Layer
    subgraph "Build & Configuration Layer"
        Build_Webpack["Webpack Config"]:::build
        Build_TS["TypeScript Config"]:::build
        Build_NPM["NPM Files"]:::build
        Build_WebpackDir["Webpack Directory"]:::build
    end

    %% Relationships within UI and Simulation Core
    UI_Index -->|"triggers"| Input_Inputs
    Input_Inputs -->|"updates"| PE_World
    PE_World -->|"reports"| Input_Stats
    Input_DOM -->|"assists"| UI_Index
    Editor_Ts -->|"adjusts"| PE_World
    EditorTool -->|"adjusts"| PE_World
    PE_Spring -->|"drives"| PE_World
    PE_Vector -->|"influences"| PE_Spring

    %% Relationships from Build & Configuration to other layers
    Build_Webpack -->|"bundles"| UI_Index
    Build_TS -->|"compiles"| Editor_Ts
    Build_TS -->|"compiles"| EditorTool
    Build_TS -->|"compiles"| PE_Spring
    Build_TS -->|"compiles"| PE_Vector
    Build_TS -->|"compiles"| PE_World
    Build_TS -->|"compiles"| PE_Point
    Build_TS -->|"compiles"| PE_Struct
    Build_NPM -->|"manages"| Input_Inputs
    Build_NPM -->|"installs"| Input_DOM
    Build_NPM -->|"installs"| Input_Stats
    Build_WebpackDir -->|"configs"| UI_Styles

    %% Click Events
    click UI_Index "https://github.com/gfc9110/spring-physics-2d/blob/master/src/index.html"
    click UI_Styles "https://github.com/gfc9110/spring-physics-2d/tree/master/src/styles"
    click Editor_Ts "https://github.com/gfc9110/spring-physics-2d/blob/master/src/editor.ts"
    click EditorTool "https://github.com/gfc9110/spring-physics-2d/blob/master/src/editorTool.ts"
    click PE_Spring "https://github.com/gfc9110/spring-physics-2d/blob/master/src/spring.ts"
    click PE_World "https://github.com/gfc9110/spring-physics-2d/blob/master/src/world.ts"
    click PE_Vector "https://github.com/gfc9110/spring-physics-2d/blob/master/src/vector.ts"
    click PE_Point "https://github.com/gfc9110/spring-physics-2d/blob/master/src/point.ts"
    click PE_Struct "https://github.com/gfc9110/spring-physics-2d/blob/master/src/structures.ts"
    click Input_Inputs "https://github.com/gfc9110/spring-physics-2d/blob/master/src/inputs.ts"
    click Input_DOM "https://github.com/gfc9110/spring-physics-2d/blob/master/src/domHelpers.ts"
    click Input_Stats "https://github.com/gfc9110/spring-physics-2d/blob/master/src/stats.ts"
    click Build_Webpack "https://github.com/gfc9110/spring-physics-2d/blob/master/webpack.config.js"
    click Build_TS "https://github.com/gfc9110/spring-physics-2d/blob/master/tsconfig.json"
    click Build_NPM "https://github.com/gfc9110/spring-physics-2d/blob/master/package.json"
    click Build_WebpackDir "https://github.com/gfc9110/spring-physics-2d/tree/master/webpack"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#0066cc,stroke-width:2px;
    classDef physics fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef input fill:#fff3cd,stroke:#ffc107,stroke-width:2px;
    classDef build fill:#f8d7da,stroke:#dc3545,stroke-width:2px;
```
