Sketch: p5.js Web Editor | black hole
Concept
This space simulation project evolved from the foundation of the vehicle sketch code provided on WordPress for current weekly objective, transforming the basic principles of object movement and forces into a more planetary scale simulation. The original vehicle concept was as inspiration for implementing celestial bodies that respond to gravitational forces. By adapting the core mechanics of velocity and acceleration from the vehicle example, I developed a more complex system that models the behavior of various celestial objects interacting with a central black hole. The simulation aims to create an immersive experience that, while not strictly scientifically accurate, captures the wonder and dynamic nature of cosmic interactions.
Process
The development began with establishing the CelestialBody class as the center of the simulation. This class handles the physics calculations and rendering for all space objects, including planets, stars, comets, and the central black hole. I implemented Newton’s law of universal gravitation to create realistic orbital mechanics, though with modified constants to ensure visually appealing movement within the canvas constraints.
The black hole visualization required special attention to create a convincing representation of its extreme gravitational effects. I developed an accretion disk system using separate particle objects that orbit the black hole, complete with temperature-based coloring to simulate the intense energy of matter approaching the event horizon. The background starfield and nebula effects were added to create depth and atmosphere in the simulation.
The implementation process involved several iterations to fine-tune the visual effects and physics calculations. I spent a lot of time on creation of the particle system for the accretion disk, which needed to balance performance with visual fidelity. The addition of comet trails and star glows helped to create a more dynamic and engaging visual experience.
Challenges
One of the primary challenges was balancing realistic physics with visual appeal. True gravitational forces would result in either extremely slow movement or very quick collisions, so finding the right constants and limits for the simulation required careful tuning. Another significant challenge was creating convincing visual effects for the black hole’s event horizon and gravitational lensing without overwhelming the system’s performance.
The implementation of the accretion disk presented its own challenges, particularly in managing particle behavior and ensuring smooth orbital motion while maintaining good performance with hundreds of particles. Creating a visually striking distortion effect around the black hole without impacting the frame rate was also difficult. I spent a lot of time on gravitiational lensing component but despite this could not get it to work like I imagined. However, that is beyond the scope of weekly assignment, and it could be something I would work for bigger timeframe.
Code I’m Proud Of
The following section creates multiple layers of distortion to simulate gravitational lensing:
for (let i = 20; i > 0; i--) { let radius = this.radius * (i * 0.7); let alpha = map(i, 0, 20, 100, 0); for (let angle = 0; angle < TWO_PI; angle += 0.05) { let time = frameCount * 0.02; let xOff = cos(angle + time) * radius; let yOff = sin(angle + time) * radius; let distortion1 = noise(xOff * 0.01, yOff * 0.01, time) * 20; let distortion2 = noise(xOff * 0.02, yOff * 0.02, time + 1000) * 15; let finalDistortion = distortion1 + distortion2; let spiralFactor = (sin(angle * 3 + time) * cos(angle * 2 + time * 0.5)) * radius * 0.1;
This code combines Perlin noise with circular motion to create a dynamic, organic-looking distortion field that suggests the warping of space-time around the black hole. The layered approach with varying alpha values creates a sense of depth and intensity that enhances the overall visual effect. The addition of the spiral factor creates a more complex and realistic representation of the gravitational distortion.
Reflection and Future Considerations
The project successfully achieves its goal of creating an engaging and visually impressive space simulation. The interaction between celestial bodies and the central black hole creates emergent behaviors that can be both predictable and surprising, making the simulation entertaining to watch. The visual effects, particularly around the black hole, effectively convey the sense of powerful gravitational forces at work.
For future iterations, several enhancements could be considered. Implementing relativistic effects could make the simulation more scientifically accurate, though this would need to be balanced against performance and visual clarity. Adding user interaction capabilities, such as allowing viewers to create new celestial bodies or adjust gravitational constants in real-time, could make the simulation more engaging and educational.
Another potential improvement would be the addition of collision detection and handling between celestial bodies, which could lead to interesting events like the formation of new bodies or the creation of debris fields. The visual effects could also be enhanced with WebGL shaders to create more sophisticated gravitational lensing and accretion disk effects while potentially improving performance.
The addition of sound effects and music could enhance the immersive experience, perhaps with dynamic audio that responds to the movement and interactions of celestial bodies. A more sophisticated particle system could be implemented to simulate solar winds, cosmic radiation, and other space phenomena, further enriching the visual experience.
Additionally, implementing a system to generate and track interesting events in the simulation could provide educational value, helping viewers understand concepts like orbital mechanics and the behavior of matter around black holes.