EXPLORING INTERACTIVE
PARTICLE SYSTEMS
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To make particles collide with the mouse in Three.js, raycast the pointer onto a plane to place a sphere collider, then every frame check each particle distance to the sphere center. If it is inside, push it back to the surface and reflect its velocity along the surface normal. Here 1,800 colorful balls rain down, bounce off your dark blue sphere and settle on a neon floor.
A beginner walkthrough of InstancedMesh particles, typed-array physics, gravity, sphere collision detection and velocity reflection in Three.js. The particle rain behind this page is the scene you build; launch the Live Demo to control it.
LIVE DEMO
The particle rain behind this page is a live Three.js scene. On the page the sphere drifts on its own. Launch the full-screen demo to move the dark blue sphere with your mouse or one finger, drag or use two fingers to orbit and zoom, and watch 1,800 particles bounce off it. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · Move the sphere with mouse or touch · Esc to exit
What Collision Detection Means for Particles
Collision detection is just a question asked over and over: is this thing inside that thing? For a sphere the answer is easy. Measure the distance from the particle to the sphere center. If it is smaller than the sphere radius plus the particle radius, they are touching.
Knowing they touch is half the job. The other half is the response. We move the particle back out to the surface along the line from the center (the normal), then flip the part of its speed that was heading into the sphere, keeping 55 percent of it as bounce. That is the same reflection math used for balls hitting walls in games.
Why InstancedMesh and Typed Arrays?
Drawing 1,800 separate meshes means 1,800 draw calls, which is slow. An InstancedMesh draws one sphere shape 1,800 times in a single call, each with its own matrix and color. Positions and velocities live in plain Float32Arrays, which JavaScript loops through very fast.
We also split each frame into two smaller physics steps so fast particles do not tunnel through the sphere. For a gentler start, try the fun interactive smiley particle system, or ball-to-ball physics in 3D spheres with collision events.
How It Works
- Set up the scene, camera, renderer and lights. We make a nearly black world, point a camera at the middle, and add soft and direct light so the balls look round.
- Create the particles with one InstancedMesh. We make one small sphere shape and ask the GPU to draw it 1,800 times, each in a random bright color.
- Store positions and velocities and spawn particles. Two long lists hold where every ball is and how fast it moves, and a spawn function drops a ball from the sky.
- Add the sphere collider and the floor. We add a solid dark blue ball that will block the rain, plus a round floor with a polar grid.
- Move the collider and orbit the camera. Hover the mouse (or slide one finger) and the sphere follows; drag (or use two fingers) to orbit and zoom the camera.
- Detect and resolve collisions. Each ball falls with gravity; if it is inside the sphere we push it out and bounce it away, if it hits the floor it bounces lower, and resting balls respawn at the top.
- Write instance matrices and run the loop. Every frame we copy the new positions into the GPU list, glide the sphere toward its target, run physics twice and draw.
The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.
Comparing squared distances avoids a square root for the thousands of particles that are not touching the sphere. Only the few that collide pay for Math.sqrt.
Frequently Asked Questions
How do you detect particle collisions in Three.js?
Three.js has no built-in physics, so you check it yourself. For a sphere collider, compute the squared distance from each particle to the sphere center and compare it with the squared sum of their radii. If it is smaller, the particle is inside and needs to be pushed out.
How do you bounce a particle off a sphere?
Find the normal, the direction from the sphere center to the particle. Move the particle onto the surface along that normal, then subtract (1 + bounce) times the velocity component along the normal. That reflects the motion and keeps a fraction of the speed.
How do I make particles follow or react to the mouse in 3D?
Convert the pointer to normalized device coordinates, call raycaster.setFromCamera, and intersect the ray with a Plane to get a world position. Move a collider or attractor to that point.
Why use InstancedMesh for particles?
InstancedMesh draws many copies of one geometry in a single draw call, each with its own matrix and color, so thousands of 3D particles stay fast. Points is even cheaper but draws flat squares instead of lit spheres.
Do the particles collide with each other?
Not in this demo. Particle to particle checks grow with the square of the count, so they need a spatial grid or a physics engine. Particles colliding with one moving sphere and a floor is enough for a fun interactive effect.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.
Play It and Keep Learning
Instanced particles and hand-written collision checks keep the effects fast in Super Soldier Battle, a free Three.js browser game. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.
Play Game