This is the continuation from the part 1 of this article series.
What we have accomplished here so far is adding of bubbles with random color and radius on mouse click.
Next step is to make those bubble move around the screen, to do that we need to keep track of bubbles x, y location and as well as its velocity.
1. Creating a bubble object
We need to create a bubbles variable to contain all the bubbles that has been added to our canvas.
Next is to create a Bubble Object that will contain position, velocity, color, radius.
Next is to add show function in it, which is the same code we have in the mousePressed() function
Now let's move to our mousePressed() function and add new bubble object in the bubbles variable we have.
createVector() takes 3 argument x, y, z which is the coordinates of our bubble, since we are doing this on 2d we are going to ignore z axis
If you try to run this now on our browser, you'll see nothing is displayed on the canvas, its because we have to loop and display all the bubbles.
On our draw() and setup() function, let's display all bubbles that has been created.
If you run this on the browser we get to see same results as to our part1 of this article series. But what has improved or change, if we still get the same results?
In this case its easier to keep track and manipulate each bubble's properties since its already wrapped in an Object.
2. Making the Bubbles move
Next step is making our bubbles move, which a lot more easier than you think.
On our bubble object let's add an update() function that adds the velocity to our position, to make the bubbles move.
On our mousePressed() function let's add a random velocity to our bubble, every mouse click
And call the update() function of the bubble on draw
If we run this on browser, here's the output.
Next step, is to make the bubbles not to leave the canvas when it hit the edges.
3. Making bubbles stay at the screen
Let's write an edges() function inside the Bubble object, which detects and inverses the velocity if it hits the edges of the canvas.
And let's call the edges() function inside the update() function.
After running, we can now see this as our final result.
Source code: https://github.com/onecompileman/simple-bubble-screen-animation
Try it here: https://www.openprocessing.org/sketch/631480