AI-Powered Physics Simulation & Educational Platform
Project Einstein is an interactive physics education platform that combines computer vision, AI agents, and real-time physics simulation to help students understand mechanical systems through visual learning.
- 📷 Diagram Upload & Recognition: Upload physics diagrams and automatically detect objects (masses, pulleys, springs, etc.)
- 🤖 AI Physics Tutor: GPT-powered conversational agent that explains physics concepts and answers questions
- ⚙️ Real-time Physics Simulation: Matter.js-based 2D rigid body physics engine with accurate simulation
- 🎨 Interactive Whiteboard: Visual canvas for creating and manipulating simulation boxes
- 📊 Frame-by-Frame Analysis: Step through simulation frames to observe motion in detail
- 🔧 Entity Editing: Modify object properties (mass, position, friction) and re-simulate
Project-Einstein/
├── frontend/ # Next.js 14 + React + TypeScript
│ ├── src/
│ │ ├── app/ # Next.js app router pages
│ │ ├── components/ # Simulation components (controls, renderer, layer)
│ │ ├── whiteboard/ # Whiteboard canvas & simulation boxes
│ │ ├── lib/ # API clients, utilities, test fixtures
│ │ └── simulation/ # Simulation context & state management
│ └── package.json
│
├── backend/ # FastAPI + Python 3.11+
│ ├── app/
│ │ ├── routers/ # API endpoints (chat, diagram, simulation)
│ │ ├── agent/ # AI agent (tools, labeler, context)
│ │ ├── chat/ # Chat engine & service
│ │ ├── sim/ # Physics scene builder & universal builder
│ │ └── models/ # Settings & configuration
│ ├── sim_worker/ # Node.js Matter.js physics worker
│ └── pyproject.toml
│
└── docs/ # Documentation & specifications
-
Frontend:
- Node.js 18+ (for Next.js and Matter.js worker)
- pnpm (recommended) or npm
-
Backend:
- Python 3.11+
uvpackage manager (recommended) or pip- OpenAI API key (for GPT-5 agent features)
cd backend
uv sync --devOr with pip:
pip install -e .Create a .env file in the backend/ directory:
cd backend
cp .env.example .env # if .env.example existsEdit .env and add the following:
# Environment
APP_ENV=dev
# OpenAI Configuration
OPENAI_API_KEY=sk-... # Your OpenAI API key
OPENAI_MODEL=gpt-5
# Chat Settings
CHAT_SYSTEM_PROMPT=You are Project Einstein, an AI physics tutor...
CHAT_AUDIT_LOG_ENABLED=true
CHAT_AUDIT_LOG_PATH=logs/chat-turns.log
# CORS (must match frontend port)
FRONTEND_ORIGIN=http://localhost:9002Key Settings:
OPENAI_API_KEY: Required for AI agent features (GPT-powered chat & entity labeling)FRONTEND_ORIGIN: Must match the frontend dev server port (default: 9002)- Without OpenAI key, the backend falls back to an echo engine for testing
For physics simulation:
cd backend/sim_worker
npm installNote: If not installed, the backend falls back to an analytic solver.
cd backend
uv run uvicorn app.main:app --reloadOr without uv:
uvicorn app.main:app --reloadBackend runs at: http://127.0.0.1:8000
API Docs: http://127.0.0.1:8000/docs (Swagger UI)
Health Check:
curl http://127.0.0.1:8000/healthcd frontend
pnpm installOr with npm:
npm installpnpm run devOr with npm:
npm run devFrontend runs at: http://localhost:9002
Available Scripts:
pnpm run dev- Start Next.js dev server with Turbopack (port 9002)pnpm run build- Build for productionpnpm run start- Start production serverpnpm run lint- Run ESLintpnpm run test- Run Vitest testspnpm run typecheck- TypeScript type checking
Open two terminals:
Terminal 1 - Backend:
cd backend
uv run uvicorn app.main:app --reloadTerminal 2 - Frontend:
cd frontend
pnpm run devThen open http://localhost:9002 in your browser.
- Create a Simulation Box on the whiteboard canvas
- Upload a Physics Diagram (e.g., Atwood machine, pulley system)
- AI Agent Processes the image:
- Detects objects (masses, pulleys, ropes)
- Labels entities with physics properties
- Builds a physics scene
- Run Simulation to see motion
- Chat with AI Tutor to ask questions about the physics
- Edit & Re-simulate by modifying object properties
- Upload an image showing two masses connected by a rope over a pulley
- Agent detects: 2 masses, 1 pulley, rope constraint
- Simulation shows the heavier mass descending
- Ask: "Why does mass A accelerate faster?"
- AI explains: "The net force is (m_a - m_b) * g..."
cd backend
uv run pytestOr:
pytestcd frontend
pnpm run test- Next.js 14 (App Router, React Server Components)
- TypeScript (Type-safe development)
- Matter.js (2D physics engine)
- Tailwind CSS (Styling)
- Radix UI (Accessible components)
- Zustand (State management for whiteboard)
- FastAPI (High-performance async API)
- OpenAI API (GPT-4/5 for AI agent)
- Pydantic (Data validation)
- Pillow (Image processing)
- HTTPX (Async HTTP client)
src/
├── app/ # Next.js app router
│ ├── page.tsx # Main dashboard page
│ └── layout.tsx # Root layout
├── components/
│ └── simulation/ # Simulation components
│ ├── simulation-layer.tsx # Matter.js integration layer
│ ├── simulation-renderer.tsx # Canvas rendering
│ ├── simulation-controls.tsx # Playback controls
│ └── agent-chat-panel.tsx # AI chat interface
├── whiteboard/
│ ├── components/
│ │ └── simulation-box-node.tsx # Simulation box component
│ ├── context.ts # Whiteboard state (Zustand)
│ └── types.ts # Type definitions
├── lib/
│ ├── api.ts # Backend API client
│ ├── agent-api.ts # Agent-specific API calls
│ └── test-fixtures.ts # Sample simulation data
└── simulation/
└── SimulationContext.tsx # Global simulation state
app/
├── main.py # FastAPI application entry point
├── routers/
│ ├── unified_chat.py # Chat API endpoints
│ ├── diagram.py # Image upload & parsing
│ ├── init_sim.py # Simulation initialization
│ └── run_sim.py # Simulation execution
├── agent/
│ ├── tool_registry.py # Available tools for agent
│ ├── labeler.py # GPT-powered entity labeling
│ └── agent_context.py # Conversation context management
├── chat/
│ ├── engine.py # Chat engine (OpenAI, Echo)
│ ├── service.py # Chat orchestration
│ └── schemas.py # Pydantic models
├── sim/
│ ├── universal_builder.py # Scene construction
│ └── schema.py # Scene data schemas
└── models/
└── settings.py # Configuration (env variables)
| Variable | Description | Default |
|---|---|---|
APP_ENV |
Environment (dev/prod) | dev |
OPENAI_API_KEY |
OpenAI API key | (required for AI features) |
OPENAI_MODEL |
GPT model name | gpt-4o |
FRONTEND_ORIGIN |
CORS allowed origin | http://localhost:9002 |
CHAT_AUDIT_LOG_ENABLED |
Enable chat logging | true |
CHAT_AUDIT_LOG_PATH |
Log file path | logs/chat-turns.log |
- Dev Server Port: 9002 (configured in
package.json) - Backend API URL: Defaults to
http://127.0.0.1:8000 - Turbopack: Enabled for faster development builds
Problem: ModuleNotFoundError when running uvicorn
Solution: Make sure you're in the backend/ directory and dependencies are installed:
cd backend
uv sync --devProblem: CORS errors in browser
Solution: Check that FRONTEND_ORIGIN in .env matches your frontend port (9002)
Problem: OpenAI API errors
Solution: Verify OPENAI_API_KEY is set correctly in .env
Problem: Cannot connect to backend
Solution: Ensure backend is running on port 8000:
curl http://127.0.0.1:8000/healthProblem: pnpm: command not found
Solution: Install pnpm globally:
npm install -g pnpmProblem: Physics simulation not rendering
Solution: Check browser console for errors. Try reinstalling backend/sim_worker:
cd backend/sim_worker
npm installOnce the backend is running, visit:
- Swagger UI:
http://127.0.0.1:8000/docs - ReDoc:
http://127.0.0.1:8000/redoc
POST /chat- Send message to AI agentPOST /diagram/upload- Upload physics diagram imagePOST /init_sim- Initialize simulation from imagePOST /run_sim- Execute physics simulationGET /health- Health check
- Frontend README: frontend/README.md
- Backend README: backend/README.md
- Documentation: docs/
- Architecture Spec: backend/ARCHITECTURE_V04.md