👉 View the live demo on CodePen
Ever wondered how those mesmerizing 3D animations work without heavy libraries? Turns out, the secret is hiding in plain sight—it's physics! I recently built a particle system that feels genuinely three-dimensional using just vanilla JavaScript and Canvas. Let me show you how physics transforms flat pixels into an immersive experience.
Why Physics Matters in 3D
Our brains are hardwired to understand physical motion. When digital objects behave according to the laws we intuitively know, they instantly feel more real. It's not about perfect graphics—it's about movement that makes sense to our brains.
Step 1: Creating Depth Perception
The foundation of any 3D effect is perspective projection—making distant objects appear smaller. In nature, this happens because of how light travels through space. We mimic this with a simple but powerful formula:
scale = focalLength / (focalLength + z);
projectedX = x * scale + (screenWidth / 2);
projectedY = y * scale + (screenHeight / 2);
This z-division is what gives our flat screen the illusion of depth. Objects closer to the viewer (smaller z values) appear larger than distant ones—just like in real life. Just like when you look down a hallway—walls seem to converge in the distance. That's perspective projection in action. The focal length controls the perceived field of view—smaller values exaggerate depth, while larger ones flatten it.
Step 2: Adding Forces
Static objects are boring. Real things move, accelerate, and respond to forces. In our particle system, I implemented:
- Gravitational attraction: Particles are pulled toward a central point
- Friction: Velocities gradually decrease, simulating air resistance
- Elastic collisions: Particles bounce off boundaries while losing energy
- Inverse square forces: We simulate a repulsive inverse-square force similar to electrostatic repulsion around the mouse. For simplicity, we treat all particles as unit-mass objects.
The key to realistic motion lies in how we update positions and velocities:
// Apply acceleration to velocity
this.vx += accelerationX;
this.vy += accelerationY;
// Apply friction
this.vx *= friction;
this.vy *= friction;
// Update position
this.x += this.vx;
this.y += this.vy;
Step 3: Natural Light Behavior
Light follows physical laws too. I used these principles to enhance realism:
- Distance attenuation: Objects farther away appear dimmer
- Radial gradients: Particles have soft edges that simulate light diffusion
- Depth-based opacity: Atmospheric perspective makes distant objects fade
We mimic light attenuation and softness using simple radial gradients and distance-based opacity—not physically accurate, but perceptually effective. These visual approximations create convincing depth cues without the computational cost of real light simulation.
Step 4: Adding Organic Movement
Nothing in nature moves in perfect straight lines. Adding subtle oscillations creates more convincing motion:
this.x += Math.sin(time * 0.3) * 0.3;
this.y += Math.cos(time * 0.2) * 0.3;
These small perturbations mimic the tiny random forces that affect objects in the real world.
Step 5: Depth Sorting
In the real world, closer objects obscure farther ones. We achieve this by sorting particles by their z-position and drawing them back-to-front:
particles.sort((a, b) => b.z - a.z);
The Final Touch: Interactive Forces
Think of the mouse like a magnetic field—particles get "pushed" away as you approach. The coup de grâce is adding mouse interaction that follows the inverse square law—just like gravity and electromagnetic forces in the real world. Forces get exponentially stronger as you get closer:
const force = strength / Math.max(1, distance);
This creates a visceral feeling of pushing through a field of particles that responds naturally to your movements.
Why It Works
The magic isn't in fancy 3D engines—it's in physics. When digital objects follow the same rules as physical ones, our brains instantly recognize the patterns. The simulation becomes believable not because it looks photorealistic, but because it behaves realistically.
Next time you're crafting visual effects, remember that adding even simplified physics can transform flat graphics into immersive experiences. Our visual system evolved to understand a world governed by physical laws—tap into those expectations and watch your creations come alive!
Try waving your mouse through the particle field in the demo. Feel that? That's physics doing its magic! ✨
✨ Want to feel it in action?
👉 View the live demo on CodePen