Skip to content

Physics and orbital simulation engine in Java with extensible force laws, JSON scenarios, and a Swing GUI (UCM – Tecnología de la Programación II, 2022–23).

License

Notifications You must be signed in to change notification settings

aparreno14/physics-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Physics Simulator (Java)

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.

What this project demonstrates

  • 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.

Features

  • 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.

Repository structure

  • 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”).

Simulation model (high level)

  • Each BodiesGroup advances as:

    1. reset all body forces,
    2. apply the selected ForceLaws to compute net forces,
    3. advance each body with dt.
  • MovingBody integrates position/velocity under constant acceleration a = f/m: p <- p + vdt + 0.5adt^2 v <- v + adt

Input format (JSON)

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

Output format (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

How to run

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:

  1. Open/import as a Java project (IntelliJ/Eclipse).

  2. Ensure you have the required libraries on the classpath:

    • org.json
    • Apache Commons CLI (used by the launcher).
  3. Run the main class: src/simulator/launcher/Main.java

Batch mode example (program arguments)

(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

GUI mode example (program arguments)

-m gui -i resources/examples/input/ex1.json

Viewer controls (GUI)

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

Tests are located under tests/simulator (model + factories).

Recommendation: add Maven/Gradle to run tests in CI (see below).

Recommendations (to make this a portfolio-grade repo)

  • 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.

License

MIT (recommended for portfolio projects). Add a LICENSE file at repository root.

About

Physics and orbital simulation engine in Java with extensible force laws, JSON scenarios, and a Swing GUI (UCM – Tecnología de la Programación II, 2022–23).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published