Concept
I will be honest, I did not have that much of ideas for this week assignment… For some reason. Nevertheless, while experimenting with Matter.js I found myself with some interesting behaviors, such as gravity manipulation and collisions. Since I had a bit of interest in adding sound into my projects, I wondered how could I deliver something with at least a bit of creativity and interaction.
Sadly, most of the ideas that came into my mind were time-consuming, and thus, would be impossible to complete on time. Still, one was possible: A triangle which produces sound via the collision of circles.
Embedded sketch
Note: You can click on the Canva to spawn more circles.
Full-screen version: Go to the Full-screen version
Brief explanation of the code
The code itself is simple thanks to the use of Matter.js. That means that all the collision and interaction between bodies are managed by the library. Now, what is happening in this code is the following:
First, a set of circle bodies will be spawned and then attracted to the static circle in the middle:
//Draw circles. for (let i = 0; i < circles.length; i++) { //Attract to attractor and then display. let force = attractor.attract(circles[i]); circles[i].applyForce(force); circles[i].show(); //Check if there is any geometry outside the canvas. if (circles[i].isOffScreen()) { circles[i].removeFromWorld(); circles.splice(i, 1); i--; //This fixes the flickering in the code. } }
Secondly, while they are being pulled to the attractor, the circle bodies can move erratically around it due to the force applied. During this erratic movement, they can hit one of the three boundaries forming a triangle, or other circles. So, on hit, they will trigger an event in which a sound will reproduce (only when a boundary is hit) as well as change the color:
Collisions function:
//Done with help of the following material: https://nature-of-code-2nd-edition.netlify.app/physics-libraries/#collision-events function handleCollisions(event) { for (let pair of event.pairs) { let bodyA = pair.bodyA; let bodyB = pair.bodyB; //Retrieve the particles associated with the colliding bodies via the plugin. let particleA = bodyA.plugin.particle; let particleB = bodyB.plugin.particle; if (particleA instanceof Circle && particleB instanceof Circle) { particleA.change(); particleB.change(); } if (particleA instanceof Boundary && particleB instanceof Circle) { particleA.change(); particleB.change(); bell.play(); } } }
change() function of boundary.js:
change() { this.col = color(random(100, 255)); }
circle() function of circle.js:
change() { this.col = color(random(100, 255)); this.r = random(0, 10); }
There is also some code that helps with optimization, such as in the case that the circles go out boundaries, as well as cleaning the array appropriately:
//Check if there is any geometry outside the canvas. if (circles[i].isOffScreen()) { circles[i].removeFromWorld(); circles.splice(i, 1); i--; //This fixes the flickering in the code. }
Reflection and ideas for future work or improvements
I feel that this task end up being uninspiring. While simplistic and stylish in his own way, there are other interactions I would wish to implement. Such interactions include more “artistic” expressions once the circles hit the boundaries, or a more intricate physics simulation where the boundaries could be dynamically moved according to time (as in rotating in a 360 degree manner). I am confident that some ideas will be implemented in future projects, but at the moment, let’s conclude that this is still a phase of learning how to use Matter.js.
Used resources
a. “6. Physics Libraries.” Netlify.app, 2014, nature-of-code-2nd-edition.netlify.app/physics-libraries/#collision-events. Accessed 12 Nov. 2024.
b. “6.1 Matter.js – Introduction.” Thecodingtrain.com, thecodingtrain.com/tracks/the-nature-of-code-2/noc/6-physics-libraries/1-matterjs-introduction.
c. “6.2 Matter.js – Introduction, Continued.” Thecodingtrain.com, 2016, thecodingtrain.com/tracks/the-nature-of-code-2/noc/6-physics-libraries/2-matterjs-introduction-continued. Accessed 12 Nov. 2024.
d. “6.9 Matter.js Attraction by Natureofcode -P5.Js Web Editor.” P5js.org, 2024, editor.p5js.org/natureofcode/sketches/16sblEvax. Accessed 12 Nov. 2024.
e. “Freesound – BELL 0101.Wav by Zgump.” Freesound.org, 2024, freesound.org/people/zgump/sounds/83538/. Accessed 12 Nov. 2024.