https://webgl-physics-simulation.vercel.app
This is another project I completed for a computer animation class at WPI, where it simulates 3 object types or "species" interacting with each other. Certain colors are attracted to certain others, and repel other colors as well.
Blue chases green, green chases red, and red chases blue. Green avoids blue, red avoids blue, and blue avoids red. if Blue catches green, it will change it's color to green, and so on with the other colors.
every object has a detection radius, if another object reaches that radius, the attraction and repelling forces get applied to the objects and their new velocities are calculated and applied as well
all objects avoid the grey square in the middle
New feature in progress: mouse cursor also repels objects as it moves around the screen.
So the idea is to simply add a new particle function, "addMouseForce", that will also be called in update. It will essentially be the same as addObjectForce but using the mouse position instead of the object position.
We will create a global mousePosition variable, and create a canvas eventlistener for the "mousemove" event. everytime the mouse moves, we will grab its new position and update the mousePosition variable.
the addMouseForce function will simply read the mousePosition and use that to determine if a particle is close enough to it to have a force applied to it