This week, I was looking to explore more with gravitational attraction. Previously, I had utilized both attractive and repulsion forces, so this time, I wanted to focus on just one aspect. I noticed that a lot of projects were centered around planets, so I decided to look for inspiration elsewhere.
In nature, a similar attractive 'force' can be witnessed with insects, who are drawn to light:

Start off by creating an array of 'movers' who are the moths in this case. Push new moths into the array.

Assign the physics components to the mover object (moth); set the mass of the object to the initial value of 'm' in order to create the force that can be applied later on.

Set the gravitational constant 'G' into an arbitrary value to scale the force of attraction in the 'attract' method.

Apply the smooth movement to the attractor object to allow for arrow key control; as done previously in the vector assignment, movement is achieved through changing the velocity, rather than shifting pixels directly.
Link to Sketch (move with arrow keys): https://editor.p5js.org/kdaiii/sketches/O7r2rdFbv