A sophisticated 3D physics simulation platform that uses cutting-edge AI to generate and refine physics scenes through natural language prompts. Built with Elm frontend and FastAPI backend, featuring real-time physics simulation powered by Rapier.js.
- Claude Sonnet 4.5: Latest Anthropic AI model for intelligent scene creation
- Natural Language Prompts: Generate complex physics scenes from text descriptions
- Scene Refinement: Modify existing scenes with conversational AI
- Context-Aware: AI understands physics principles and realistic object interactions
- Rapier Physics Engine: High-performance, WebAssembly-powered physics
- Realistic Physics: Gravity, collisions, friction, and restitution
- Multiple Object Types: Boxes, spheres, cylinders with customizable properties
- Dynamic Interactions: Objects interact naturally in real-time
- Transform Controls: Move, rotate, and scale objects with visual gizmos
- Property Editing: Adjust mass, friction, restitution, and visual properties
- Object Selection: Click to select objects with visual feedback
- Real-Time Updates: See physics changes instantly
- Undo/Redo System: Full history tracking with keyboard shortcuts
- Local Storage: Scenes persist across browser sessions
- Keyboard Shortcuts: Space (play/pause), G/R/S (transform modes), Ctrl+Z/Y (undo/redo)
- Responsive UI: Modern, clean interface optimized for physics simulation
- Elm 0.19.1: Functional programming language for reliable UIs
- Three.js: 3D graphics and rendering
- Rapier.js: WebAssembly physics engine
- Vite: Fast development server and build tool
- FastAPI: High-performance Python web framework
- OpenRouter API: Access to Claude Sonnet 4.5 via OpenAI-compatible interface
- Pydantic: Data validation and serialization
- Uvicorn: ASGI server for production deployment
- Claude Sonnet 4.5: Anthropic's most advanced AI model
- 1M Token Context: Massive context window for complex scene generation
- OpenRouter: Multi-provider AI routing with automatic fallbacks
- Python 3.14+ with virtual environment support
- Node.js 16+ and npm
- OpenRouter API Key (for AI scene generation)
git clone <repository-url>
cd physics-simulatorcd backend
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt# In a separate terminal
npm installCreate a .env file in the backend directory:
OPENROUTER_API_KEY=your_openrouter_api_key_here# Terminal 1: Backend
cd backend && source venv/bin/activate
OPENROUTER_API_KEY=your_key python main.py
# Terminal 2: Frontend
npm run devNavigate to http://localhost:5173
- Enter a natural language prompt in the left panel
- Click "Generate Scene" to create a new physics scene
- Watch as AI creates realistic objects with proper physics properties
- "a red ball bouncing on a wooden table"
- "stack of colorful boxes with a sphere rolling between them"
- "pyramid of spheres on a ramp with a cube at the bottom"
- Select Objects: Click on objects in the 3D view
- Transform: Use G (move), R (rotate), S (scale) keys
- Properties: Adjust physics properties in the right panel
- Refine: Use the "Refine Scene" feature to modify with AI
- Play/Pause: Spacebar or play button
- Reset: Reload the page to reset the scene
- Undo/Redo: Ctrl+Z / Ctrl+Y
POST /api/generate
Content-Type: application/json
{
"prompt": "a bouncing ball and a wooden box"
}POST /api/refine
Content-Type: application/json
{
"scene": {...},
"prompt": "make the ball blue"
}{
"objects": {
"object_id": {
"id": "object_id",
"transform": {
"position": {"x": 0.0, "y": 5.0, "z": 0.0},
"rotation": {"x": 0.0, "y": 0.0, "z": 0.0},
"scale": {"x": 1.0, "y": 1.0, "z": 1.0}
},
"physicsProperties": {
"mass": 1.0,
"friction": 0.5,
"restitution": 0.3
},
"visualProperties": {
"color": "#ff0000",
"shape": "Box"
}
}
},
"selectedObject": null
}Main.elm
โโโ Model: Scene, UI state, simulation state
โโโ Update: Message handling and state transitions
โโโ View: Three-panel layout with canvas integration
โโโ Ports: Communication with JavaScript/Three.js
main.py
โโโ AI Client: OpenRouter integration with Claude Sonnet 4.5
โโโ Scene Generation: AI-powered scene creation
โโโ Scene Refinement: Conversational scene modification
โโโ API Endpoints: RESTful physics scene management
PhysicsRenderer.js (Three.js + Rapier)
โโโ Scene Management: Object creation and updates
โโโ Physics Simulation: Real-time physics calculations
โโโ Transform Controls: Interactive object manipulation
โโโ Rendering: WebGL-accelerated 3D graphics
- API Key Security: OpenRouter API key stored server-side only
- No Data Persistence: Scenes exist only in browser memory
- Local Storage: Optional scene saving in browser localStorage
- CORS Protection: Configured for localhost development
# Backend
cd backend && source venv/bin/activate
uvicorn main:app --reload --host 0.0.0.0 --port 8000
# Frontend
npm run dev -- --host 0.0.0.0# Frontend
npm run build
# Backend (using gunicorn)
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make changes and test thoroughly
- Commit changes:
git commit -am 'Add feature' - Push to branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Anthropic: For Claude Sonnet 4.5 AI model
- OpenRouter: For AI model access and routing
- Rapier Physics: For high-performance WebAssembly physics
- Three.js: For 3D graphics and WebGL rendering
- Elm: For reliable functional programming frontend
For questions or issues:
- Create an issue on GitHub
- Check the troubleshooting section below
Backend won't start:
- Ensure Python virtual environment is activated
- Check OPENROUTER_API_KEY environment variable
- Verify all dependencies are installed:
pip install -r requirements.txt
Frontend compilation errors:
- Run
npm installto ensure all dependencies - Check Elm version:
elm --version(should be 0.19.1) - Clear Elm cache:
rm -rf elm-stuff && elm install
Physics not working:
- Check browser console for WebAssembly errors
- Ensure modern browser with WebGL support
- Try refreshing the page
AI generation fails:
- Verify OpenRouter API key is valid
- Check API quota and billing status
- Ensure internet connection for API calls
- Use browser developer tools to inspect 3D scene
- Check browser console for Elm runtime errors
- Monitor network tab for API request/response
- Use browser localStorage inspector for saved scenes
Built with โค๏ธ using cutting-edge AI and modern web technologies