Newton’s Laws of Motion with P5.JS

Jay (Vijayasimha BR)
Random Pink Hula
Published in
3 min readNov 2, 2021
that water drags the objects as they move through. source code

Once again, I am walking myself through the ‘nature of code’ book written by Daniel Shiffman. I am now in chapter 2 which talks about physics forces as described by Newton’s Laws of Motion.

The laws are simple enough and I paraphrase them here in my own words.

  1. Constant will remain constant unless something changes in the system in which the constant lives.
  2. Applied force depends on mass and velocity.
  3. You push 1, you get back 1.

Going through this chapter, I finally realize the point of having to learn all this stuff in high school. Of course, I would end up using these laws for some lame humor or double entendre. Otherwise, I never would use them in real life.

As usual, the code is available here.

Now sitting through the chapter and example code, I am liking it. The chapter can be divided into three simulations.

One: The simulation about downward force (like gravity), sideways force (like wind), and of course, cool looking bouncing balls.

gravity plus wind

Two: The same thing as above, but now, we have drag. When the objects pass through the liquid (I have put blue for water), they will slow down, but they remain fast when they are out of it.

gravity plus wind with liquid drag

Three: Finally, circular celestial objects that revolve around a center object. I was hoping to make it look like the moon that revolves around earth. I could not get that to happen though.

that big blue is earth. then, eclipsing it, celestial objects.

Of course, all of them look amazing when the code runs. I wish to start including videos here but I am right now on the move and don’t have access to my RTX 3060 or my GTX 1060 computers. Cannot record video yet.

Eventually, I will.

Final Note

The more I go through this books, the more math and physics in the pages. A lot of it I don’t understand, yet. However, I am getting a ton of ideas about simulations of everything nature.

Perhaps, one day, I will build a cool, user controllable physic simulator.

I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash.

--

--