Concept
My concept is inspired by the red explosive barrels from the Half-Life video game series. I wanted to recreate that same mechanic of an exploding barrel using matter.js where objects can collide with a barrel, build up damage, and trigger an explosion. The idea of explosive barrels spread into other video games with objects that explode either on impact or as it builds up damage and growing up I always enjoyed playing games with destructible elements.
Code Highlight
explode(allBoxes) {
if (this.isExploded) {
return;
}
const center = this.body.position;
for (let i = 0; i < allBoxes.length; i++) {
const currentBox = allBoxes[i];
if (!currentBox || currentBox.body.isStatic) {
continue;
}
const boxPos = currentBox.body.position;
const delta = Vector.sub(boxPos, center);
const distance = Vector.magnitude(delta);
if (distance <= 0 || distance > EXPLOSION_RADIUS) {
continue;
}
const direction = Vector.normalise(delta);
const falloff = 1 - distance / EXPLOSION_RADIUS;
const totalForce = max(EXPLOSION_MIN_FORCE, EXPLOSION_FORCE * falloff);
const force = Vector.mult(direction, totalForce);
Body.applyForce(currentBox.body, boxPos, force);
// Add a direct velocity impulse so the explosion is always visible.
Body.setVelocity(currentBox.body, {
x: currentBox.body.velocity.x + direction.x * EXPLOSION_VELOCITY_BOOST,
y: currentBox.body.velocity.y + direction.y * EXPLOSION_VELOCITY_BOOST
});
}
Composite.remove(world, this.body);
this.isExploded = true;
}
The part of the code I am most proud of is the explosion function. I track the number of collisions for the barrel, change the barrel color from gray toward red as damage increases, and once it reaches the hit limit which I defined as a global variable I remove the barrel body and apply outward force and increase the initial velocity to nearby boxes.
Embedded Sketch
Milestones and Challenges
Stage 1:

Focused on getting the core interaction working by spawning rectangles with the mouse, and getting used to coding with matter.js.
Stage 2:

Added a grounded barrel in the center area to establish the main object of the simulation and changed the background color to something I liked. At this point I began thinking about aesthetics.
Stage 3:


Introduced the damage mechanic where the barrel changes from gray toward red with each hit. A key challenge was collision handling, especially making sure hits were counted reliably and visual feedback was clear.
Stage 4:

Finally I combined everything and added the final explosion behavior after the hit threshold. I had to tune radius, force, and velocity boost values so the explosion effect was strong and readable without becoming too chaotic. I found that the force value was not as important as the initial velocity boost because of air resistance. Then I added text to show the controls and allowed the user to drag their mouse to spawn squares rather than only clicking and made the boundary cover the sides and not only the ground.
Reflection and Future Improvements
If I continue this project, I want to replace simple shapes with sprite-based visuals so the barrel and debris feel more game-like. I also want to explore whether Matter.js can be combined with a 3D workflow or whether a different engine would be better for a full 3D version. Future improvements could include sound effects, particle debris, and different barrel types with unique blast strengths.