Skip to content

Interactive real-time black hole simulation with gravitational lensing and accretion disk physics. Built with React, TypeScript, Three.js, and WebGL shaders for educational visualization of relativistic effects.

License

Notifications You must be signed in to change notification settings

chrismatgit/black-hole-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒŒ Black Hole Simulation

Interactive Real-Time Physics Simulation with WebGL

Explore the mysteries of spacetime through cutting-edge visualization

License: MIT TypeScript React Three.js Vite WebGL

Build Status Version PRs Welcome

Medium

๐Ÿš€ Live Demo โ€ข ๐Ÿ“– Documentation โ€ข ๐Ÿ› Report Bug โ€ข โœจ Request Feature โ€ข ๐Ÿ“ Article on Medium

Black Hole Simulation


๐Ÿ“‹ Table of Contents


๐ŸŒŸ Features

A physics-based black hole simulation built with React, TypeScript, Three.js, and WebGL shaders, showcasing relativistic effects including gravitational lensing and accretion disk dynamics. Here are the live demo links hosted on GitHub pages and Vercel. Please read my Medium article for further explanation.

โœจ Core Features

  • ๐ŸŒ€ Real-time gravitational lensing - Watch light bend around massive objects
  • ๐Ÿ’ซ Interactive accretion disk - Visualize matter spiraling into the black hole
  • ๐ŸŽฎ Responsive controls - Desktop navigation bar and mobile-friendly interface
  • ๐Ÿ”„ Real-time physics - GPU-accelerated calculations for smooth performance
  • ๐Ÿ“ฑ Cross-platform - Works seamlessly on desktop, tablet, and mobile devices
  • ๐ŸŽจ Stunning visuals - Realistic rendering with custom WebGL shaders

๐Ÿš€ Technical Highlights

  • GPU-Accelerated Physics - Utilizing WebGL fragment shaders for real-time calculations
  • Responsive Design - Adaptive UI that works across all screen sizes
  • Modern Architecture - Built with React 18, TypeScript, and Vite
  • Educational Focus - Perfect for physics students and space enthusiasts

๐Ÿ”ฌ Physics Implementation

Our simulation implements cutting-edge astrophysics concepts with mathematical precision:

๐ŸŒŒ Relativistic Effects

  • Schwarzschild Metric - Proper spacetime curvature around a black hole
  • Geodesic Integration - Runge-Kutta 4th order integration for accurate light path calculations
  • Gravitational Lensing - Light bending effects around massive objects
  • Event Horizon - Proper black hole boundary visualization with Schwarzschild radius

โš›๏ธ Advanced Physics

  • Doppler Shift - Relativistic frequency shifts in the accretion disk
  • Gravitational Redshift - Energy loss of light escaping gravitational wells
  • Angular Momentum Conservation - Realistic orbital mechanics
  • Fractal Noise Generation - Procedural accretion disk structure

๐Ÿ› ๏ธ Technologies

Frontend Physics & Graphics Development Deployment
React Three.js TypeScript Vite
HTML5 WebGL ESLint Node.js
CSS3 GLSL npm GitHub

๐Ÿ—๏ธ Architecture

  • React 18 - Modern React with hooks and functional components
  • TypeScript - Full type safety and enhanced developer experience
  • Three.js - 3D graphics engine and WebGL abstraction
  • Custom GLSL Shaders - GPU-accelerated physics calculations
  • Vite - Lightning-fast build tool and development server
  • Responsive CSS - Mobile-first design with breakpoints

๐Ÿš€ Quick Start

๐Ÿ“‹ Prerequisites

Ensure you have the following installed:

  • Node.js >=18.0.0 (Download here)
  • npm >=8.0.0 (comes with Node.js)
  • Modern browser with WebGL 2.0 support

๐Ÿ’ป Installation

  1. Clone the repository

    git clone https://github.com/chrismatgit/black-hole-simulation.git
    cd black-hole-simulation
  2. Install dependencies

    npm install
  3. Start development server

    npm run dev
  4. Open in browser Navigate to http://localhost:5173

๐Ÿ—๏ธ Build for Production

# Build the project
npm run build

# Preview production build
npm run preview

# Run linting
npm run lint

๐Ÿณ Docker Support (Optional)

# Build Docker image
docker build -t black-hole-simulation .

# Run container
docker run -p 5173:80 black-hole-simulation

๐Ÿ“ฑ Usage

๐Ÿ–ฅ๏ธ Desktop Experience

  • Navigation Bar - Full-featured controls with dropdown menus
  • Real-time Sliders - Adjust physics parameters instantly
  • Keyboard Shortcuts - Quick access to common functions
  • Mouse Interaction - Zoom with scroll wheel

๐Ÿ“ฑ Mobile Experience

  • Slide-out Panel - Touch-friendly control interface
  • Gesture Support - Pinch to zoom, tap to control
  • Optimized Layout - Responsive design for all screen sizes

๐ŸŽฎ Interactive Features

  • Play/Pause Simulation - Control time flow
  • Adjust Physics Parameters - Real-time parameter modification
  • Toggle Accretion Disk - Show/hide matter visualization
  • Camera Animation - Automated cinematic views
  • Reset to Defaults - Quick restoration of initial settings

๐Ÿ—๏ธ Project Structure

black-hole-simulation/
โ”œโ”€โ”€ ๐Ÿ“ src/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ components/           # React components
โ”‚   โ”‚   โ”œโ”€โ”€ BlackHoleSimulation.tsx    # Main simulation engine
โ”‚   โ”‚   โ”œโ”€โ”€ NavigationBar.tsx          # Desktop navigation
โ”‚   โ”‚   โ”œโ”€โ”€ MobileControlPanel.tsx     # Mobile interface
โ”‚   โ”‚   โ””โ”€โ”€ GitHubLink.tsx             # Attribution component
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ shaders/              # WebGL shaders
โ”‚   โ”‚   โ”œโ”€โ”€ canvasVertexShader.ts      # Vertex transformations
โ”‚   โ”‚   โ””โ”€โ”€ canvasFragmentShader.ts    # Physics calculations
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ utils/                # Utility functions
โ”‚   โ”‚   โ””โ”€โ”€ math.ts                    # Mathematical helpers
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ App.tsx               # Root application component
โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ main.tsx              # React entry point
โ”‚   โ””โ”€โ”€ ๐ŸŽจ *.css                # Styling files
โ”œโ”€โ”€ ๐Ÿ“ public/                   # Static assets
โ”‚   โ””โ”€โ”€ ๐Ÿ–ผ๏ธ *.jpg                # Background textures
โ”œโ”€โ”€ ๐Ÿ“ dist/                     # Production build output
โ”œโ”€โ”€ ๐Ÿ“„ package.json              # Dependencies and scripts
โ”œโ”€โ”€ ๐Ÿ“„ vite.config.ts           # Vite configuration
โ”œโ”€โ”€ ๐Ÿ“„ tsconfig.json            # TypeScript configuration
โ””โ”€โ”€ ๐Ÿ“„ README.md                # This file

๐Ÿ”ง Key Components

Component Purpose Features
BlackHoleSimulation Core physics engine WebGL rendering, Three.js scene management
NavigationBar Desktop controls Dropdown menus, real-time sliders
MobileControlPanel Mobile interface Touch-friendly slide-out panel
Fragment Shader Physics calculations GPU-accelerated ray tracing

โš™๏ธ Configuration

๐ŸŽ›๏ธ Physics Parameters

Customize the simulation by modifying these constants in the fragment shader:

// Core physics constants
#define SPEED_OF_LIGHT 1.0          // Normalized light speed
#define EVENT_HORIZON_RADIUS 1.0    // Schwarzschild radius
#define BACKGROUND_DISTANCE 10000.0 // Distance to background stars
#define PROJECTION_DISTANCE 1.0     // Camera projection distance

// Accretion disk parameters
float innerDiskRadius = 2.0;        // Inner disk boundary
float outerDiskRadius = 8.0;        // Outer disk boundary
float disk_flow = 10.0;             // Orbital flow rate

๐ŸŽจ Visual Settings

Adjust visual quality and performance:

interface SimulationSettings {
  accretion_disk: boolean; // Show/hide accretion disk
  animate: boolean; // Enable camera animation
  speed: number; // Animation speed (0-1)
  max_iterations: number; // Ray tracing precision (50-2000)
}

๐Ÿ“ฑ Responsive Breakpoints

/* Desktop navigation */
@media (min-width: 1200px) {
  ...;
}

/* Tablet and mobile */
@media (max-width: 1199px) {
  ...;
}

๐ŸŽฏ Controls

๐Ÿ–ฑ๏ธ Mouse & Keyboard

Action Control Description
Zoom Mouse Wheel Zoom in/out of the simulation
Pause Spacebar Toggle simulation pause/play
Reset R Key Reset to default parameters

๐ŸŽ›๏ธ Interface Controls

Desktop Navigation Bar

  • โš›๏ธ Physics - Adjust simulation parameters
    • Max Iterations (50-2000) - Ray tracing precision
    • Animation Speed (0-1) - Camera movement speed
  • ๐ŸŽจ Visuals - Control visual elements
    • Accretion Disk toggle - Show/hide disk visualization
    • Auto-animate Camera - Enable cinematic movement
  • โ“ Help - Usage instructions and tips

Mobile Control Panel

  • ๐ŸŽฎ Controls - Play/Pause/Restart buttons
  • โš™๏ธ Settings - Same parameters as desktop in touch-friendly format
  • ๐Ÿ“ฑ Responsive - Optimized for touch interaction

๐Ÿ“ Physics Parameters

๐Ÿ”ข Core Constants

The simulation implements real physics with these carefully chosen parameters:

// Fundamental Constants (Normalized Units)
#define SPEED_OF_LIGHT 1.0          // c = 1 (natural units)
#define EVENT_HORIZON_RADIUS 1.0    // rs = 2GM/cยฒ
#define BACKGROUND_DISTANCE 10000.0 // Distance to celestial sphere
#define PROJECTION_DISTANCE 1.0     // Observer screen distance

// Accretion Disk Parameters
float innerRadius = 2.0;            // Inner stable circular orbit (3rs)
float outerRadius = 8.0;            // Outer disk boundary
float angularVelocity = 10.0;       // Orbital flow rate

๐ŸŽ›๏ธ Adjustable Parameters

Parameter Range Description Physics Impact
Max Iterations 50-2000 Ray tracing precision Higher = more accurate lensing
Animation Speed 0.0-1.0 Camera orbit rate Cinematic effect control
Accretion Disk On/Off Matter visualization Shows infalling material

โšก Performance Optimization

  • Low Settings (Mobile): 50-200 iterations, disk off
  • Medium Settings (Desktop): 200-800 iterations, disk on
  • High Settings (High-end): 800-2000 iterations, all effects

๐Ÿค Contributing

We welcome contributions from the physics and programming community! Whether you're fixing bugs, adding features, or improving documentation, your help is appreciated.

๐Ÿš€ How to Contribute

  1. ๐Ÿด Fork the repository

    git clone https://github.com/chrismatgit/black-hole-simulation.git
    cd black-hole-simulation
  2. ๐ŸŒŸ Create a feature branch

    git checkout -b feature/amazing-new-feature
  3. โœจ Make your changes

    • Follow the existing code style
    • Add tests for new features
    • Update documentation as needed
  4. ๐Ÿงช Test your changes

    npm run lint      # Check code quality
    npm run build     # Ensure production build works
    npm run preview   # Test production build
  5. ๐Ÿ“ Commit and push

    git add .
    git commit -m "โœจ Add amazing new feature"
    git push origin feature/amazing-new-feature
  6. ๐ŸŽฏ Create a Pull Request

    • Use clear, descriptive titles
    • Explain the changes and their purpose
    • Reference any related issues

๐Ÿ› Bug Reports

Found a bug? Please create an issue with:

  • Clear description of the problem
  • Steps to reproduce the issue
  • Expected vs actual behavior
  • Browser/device information
  • Screenshots if applicable

๐Ÿ’ก Feature Requests

Have an idea? We'd love to hear it! Please include:

  • Use case - Why is this feature needed?
  • Description - What should it do?
  • Implementation ideas - Any technical thoughts?

๐Ÿ† Recognition

Contributors will be added to our Contributors section. Significant contributions may be highlighted in release notes.


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License

Copyright (c) 2025 Chris Matabaro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

๐Ÿ‘ Acknowledgments

๐ŸŒŸ Original Work

  • Chris Matabaro - Original black hole simulation implementation
  • Three.js Community - 3D graphics framework and ecosystem
  • WebGL Specification - Graphics API standards

๐Ÿ”ฌ Scientific References

  • Karl Schwarzschild - Schwarzschild metric and black hole geometry
  • Einstein's General Relativity - Theoretical foundation for spacetime curvature
  • Chandrasekhar - Black hole physics and accretion disk theory
  • NASA/ESA - Reference imagery and astrophysical data

๐Ÿ› ๏ธ Technical Stack

  • React Team - Modern UI framework
  • TypeScript Team - Type-safe JavaScript development
  • Vite Team - Fast build tool and development server
  • ESLint Team - Code quality and consistency

๐ŸŽจ Assets & Resources

  • Space Background Textures - NASA/ESA public domain imagery
  • Mathematical Formulations - Open physics literature
  • Shader Techniques - WebGL and OpenGL communities

๐Ÿค Community

Special thanks to:

  • Physics simulation enthusiasts who provided feedback
  • Open source contributors who suggest improvements
  • Educational institutions using this for physics demonstrations
  • The broader scientific computing community

โญ Star History

Star History Chart


Made with โค๏ธ for physics education and space exploration

"The most beautiful thing we can experience is the mysterious." - Albert Einstein

๐Ÿ” Back to Top

About

Interactive real-time black hole simulation with gravitational lensing and accretion disk physics. Built with React, TypeScript, Three.js, and WebGL shaders for educational visualization of relativistic effects.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published