2D physics / orbital simulation engine with an extensible force-laws system, plus a Swing GUI and a batch (CLI) runner that produces JSON time-series outputs.
Coursework project for Tecnología de la Programación II (UCM) (2022/23). Individual project.
-
Modular OO design with a clear separation of concerns:
- Model: simulation core (bodies, groups, force laws, integrator).
- Control: JSON loading + execution orchestration.
- View: Swing GUI using the Observer pattern.
-
Extensibility via Factory/Builder: bodies and force laws can be created from JSON “type tags”.
-
Reproducible examples and expected outputs under resources/examples.
-
Unit tests covering model + factories.
-
Multiple body types:
- Moving body (mv_body)
- Stationary body (st_body)
-
Multiple force laws:
- Newton universal gravitation (nlug)
- Moving towards a fixed point (mtfp)
- No force (nf)
-
Batch mode: run N steps and output a JSON array of simulation states.
-
GUI mode:
- Tables for groups and bodies.
- Status bar (time, group count).
- Viewer window with zoom/pan and vector rendering.
- src/simulator/model Core simulation entities (Body, BodiesGroup, PhysicsSimulator, ForceLaws, etc.).
- src/simulator/factories Builder-based factories for bodies and force laws.
- src/simulator/control Controller: loads scenarios from JSON and runs the simulation.
- src/simulator/view Swing GUI (MainWindow, ControlPanel, ViewerWindow, dialogs, table models).
- src/simulator/launcher Main entry point (CLI + GUI/batch dispatcher).
- resources/examples Input JSON examples and expected output commands/results.
Note: src/extra contains small standalone examples used during development/classes (dialogs, JTable, JSON usage). For a cleaner “portfolio” repo, consider moving it to a separate branch or a dedicated folder (see “Recommendations”).
-
Each BodiesGroup advances as:
- reset all body forces,
- apply the selected ForceLaws to compute net forces,
- advance each body with dt.
-
MovingBody integrates position/velocity under constant acceleration a = f/m: p <- p + vdt + 0.5adt^2 v <- v + adt
Scenarios are loaded from JSON and include:
- groups: list of group IDs
- optional laws: per-group force law configuration
- bodies: list of bodies with type tags and parameters
See ready-to-run examples in:
- resources/examples/input/ex1.json .. ex4.json
Batch mode writes:
- an object with "states": an array of simulator snapshots
- each snapshot includes "time" and "groups"
- each group contains "id" and "bodies"
- each body contains: id, m, p, v, f
This repository is structured as a plain Java project (no Maven/Gradle build file included yet). The simplest way to run it is from an IDE:
-
Open/import as a Java project (IntelliJ/Eclipse).
-
Ensure you have the required libraries on the classpath:
- org.json
- Apache Commons CLI (used by the launcher).
-
Run the main class: src/simulator/launcher/Main.java
(These arguments match the commands used to generate resources/examples/expected_output.)
-m batch -i resources/examples/input/ex1.json -o out.json -s 1000 -dt 1000 -fl nlug
-m gui -i resources/examples/input/ex1.json
In the Viewer window:
- h: toggle help
- v: toggle vectors
- +: zoom-in
- -: zoom-out
- =: fit (auto-scale)
- l/j/i/m: pan right/left/up/down
- k: reset pan
- g: show next group (cycle groups, then show all)
Tests are located under tests/simulator (model + factories).
Recommendation: add Maven/Gradle to run tests in CI (see below).
-
Add a Maven or Gradle build (dependencies + test runner + jar packaging).
-
Add a GitHub Actions workflow to compile + run tests on every push.
-
Add screenshots/GIF of the GUI in a docs/ folder and embed them here.
-
Move src/extra to:
- a separate branch (e.g., course-notes), or
- a folder clearly marked as non-project material.
MIT (recommended for portfolio projects). Add a LICENSE file at repository root.