r/Python • gitlab.com/MisterBiggs • Jul 26 '19

Double Pendulum made with Processing.py

https://gfycat.com/feistycompetentgarpike
2.0k Upvotes

89 comments sorted by

100

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19 edited Jul 26 '19

I noticed Processing had an interface for Python: Processing.py. So, I figured I would try to port Daniel Shiffman from the Coding Trains Double Pendulum Code over to Python and it was ridiculously easy. I'm surprised I hadn't heard of the Python version of Processing so I figured I would share with Reddit.

Python code here

20

u/Groentekroket Jul 26 '19

Nice work!

I love the style of Shiffman. I'm not learning Java(Script) but I watch most of his videos just for the flow of the programs. I would love if he did more Python tutorials though.

4

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

Same here I really wish we had a python equivalent to him, but I still learn so much from him even though I don't really know JavaScript or Java.

3

u/MrHobbits Jul 27 '19

Don't let not having a Python version of him stop you! I took one of his tutorials and transcribed it into the Python version. You can still learn and apply the concepts for what he is doing in js with Python. I love his energy and how well he teaches as well as explains what he is doing. Fantastic teacher!

4

u/explodes Jul 26 '19

I agree. The programs are always fun and he teaches them really well.

2

u/SuperRokas Jul 26 '19

I love Daniel Shiffman.

2

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

Definitely my favorite programmer to watch. We just need to convince him to do some Python.

8

u/ivosaurus pip'ing it up Jul 26 '19

Ever so slightly sad that you have to write python 2.7 code for processing's Jython :/

27

u/JackdeAlltrades Jul 26 '19

16

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

It goes on forever so I had to end it somewhere!

6

u/JackdeAlltrades Jul 26 '19

Two hours sounds good to me.

2

u/[deleted] Jul 27 '19

You could have streamed it on twitch for all of us to enjoy.... For.... Ever.....

15

u/pa7x1 Jul 26 '19

Very cool! If you are interested you could do some simulations to measure the Lyapunov exponent that demonstrate the chaotic behavior of the double pendulum.

Select a starting point and fix a simulation time (the longer the better but keep it manageable for your computation). Simulate the double pendulum and record its position at time t. Then rinse and repeat with slight changes of the initial conditions. You should observe that very small changes of the initial conditions result in very different positions at time t.

8

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

I'm taking Dynamics next semester and the end of the class is a double pendulum simulation. Apparently it's pretty in depth so I'll probably post on this sub again if I manage to do it in Python instead of Matlab like the rest of my University.

6

u/[deleted] Jul 26 '19

Holy crap universities are still using Matlab? I haven't used it once, not even octave, outside of a university in 2010. But then I'm not so much with the science.

3

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

Apparently it's huge in aerospace engineering and I believe space physics uses it heavily too. I use Python every chance I get but unfortunately for most programming assignments either the professor requires Matlab since that's all they know how to grade, or it's a group project and I'm the only person that knows any programming besides Matlab.

4

u/porcelain_robots Jul 26 '19

Can you ELI5 how the bottom part can swing higher than its orginial release point? What's the physics behind this?

4

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

I'm probably not the best person to answer it, but one important thing to note about this particular simulation is that there's no loss of energy.

If you look when it goes higher than the starting position the radius is smaller than when it started. So, the first ball didn't swing as high but the second was able to flip around since it has some of the energy that the first ball originally had.

2

u/[deleted] Jul 27 '19 edited Jul 27 '19

[removed] — view removed comment

1

u/porcelain_robots Jul 29 '19

My understanding is that energy can be conserved if it's a smaller radius for the centripetal motion.

1

u/[deleted] Jul 29 '19

[removed] — view removed comment

1

u/porcelain_robots Jul 30 '19

Yeah, you're right. There's no way they can both swing higher than their original point at the same time.

2

u/[deleted] Jul 26 '19

Is Processing.py a Processing plugin for Python or a Python plugin for Processing?

2

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

According to the getting started tutorial its a mode for the processing IDE. I think that would make it a Python plugin for Processing? It still does everything in Java in the background.

1

u/[deleted] Jul 26 '19

Thank you :)

2

u/thekalmanfilter Jul 27 '19

So this is the output that shows up in the console?

1

u/MisterBlggs gitlab.com/MisterBiggs Jul 27 '19

Processing has its own editor and it opens a window with the animation similar to how matplotlib works when you use it to plot something.

To get the video I saved each frame individually and put them all together using Blender.

1

u/thekalmanfilter Jul 27 '19

THANK YOU for the insight! I always wonder how people do animations or programs that track movement on video “using python” when python is just lined of code on a screen. I’m also a bit dyslexic/ autistic and take things very literally so as a beginner (barely started learning) I have trouble understanding how we move from lines on a screen to actual pictures and videos etc. Thanks again!

75

u/piotrulu Jul 26 '19

Or as I like to call it: flower petals generator. Good job my man.

10

u/bluemerilin Jul 26 '19

That’s pretty cool. It looks like it’s a cross section of it drawing in 3D

9

u/ThortheAssGuardian Jul 26 '19

Very cool. Makes me wonder if you could model such a pendulum in 3D and use the line tracing as a model for a crazy looking 3D printed fruit bowl.

7

u/[deleted] Jul 26 '19

Well, the problem with a double pendulum is that only the inner mass stays on a circular path (in 2D, or a bowl in 3D). The outer mass will move pretty randomly throughout the volume of the bowl, so you might not be able to put anything into it.

This can be somewhat solved by making the inner pendulum longer than the outer one, but it's still gonna be a hard print because 3d printers cannot print random loops in the air, every layer has to be supported by the one below it.

Still, it's a cool idea and I might try to do it later this summer :)

5

u/est94 Jul 26 '19 edited Jul 26 '19

Ok, hear me out: what if you modeled this two pendulum system, but had pendulum 2 tethered to a frictionless sphere/ellipsoid? So in 2D it would look exactly like OP’s gif, but in 3D it would create a useable bowl? You would just calculate the 3D coordinates of pendulum 2 using the geometric constraints. Maybe that would force the length of the line between P1 and P2 to be variable.

Edit: in my idea, pendulum 1 (P1) keeps its 2D constraints, and pendulum 2(P2) can move freely in 3D, but angle P1-P2 must be between 90° and 270°. So theoretically P2 would move along a tire-shaped hollow toroid. In reality it would create a real cool, wild looking bowl. I think.

Edit 2: the shape would only be a weird toroid if you kept the length of P2 constant. If it was variable, you could have it travel within a sphere, always tangent to the inner surface of the sphere.

4

u/[deleted] Jul 26 '19 edited Jul 26 '19

Yes, that's a nice way to get a working model. I'm not sure if the resulting movement would be 'cool', though.

Like in the 2D case, if you constrained the outer mass to stay on a circular path, then it would just move back and forth like a regular pendulum.

The 3D case has more degrees of freedom, but the behaviour might be similar.

edit: I missed your edit while writing out my replay. I really like the idea to constrain the angle of P2!

A constant length makes for a way easier simulation, but the second option with variable length could basically be solved with a projection from P2 to the sphere afterwards. Really cool!

4

u/[deleted] Jul 26 '19

2

u/kudzuwirewraps Jul 26 '19

Came here to say this

1

u/LYLY2019 Jul 27 '19

Agreed:-)

1

u/3nigma42 Jul 26 '19

Nice work, did you include any type of friction

1

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

Nope. I just used the same equation as Daniel Shiffmans video. I could be lazy and just multiply the velocity of the pendulum by .999 or something every frame so it would eventually stop but that's just making a random guess so I just left it frictionless.

1

u/-walter-sobchak- Jul 26 '19

Why doesn’t it ever come to rest?

2

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

This is using code to simulate the double pendulum with gravity. It doesn't account for any air resistance or anything like that so the system never loses energy.

1

u/[deleted] Jul 26 '19

As someone that teaches processing.py I want to say how much I hate processing.py. 😂

1

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

I think my only complaint would be the editor, but I didn't get that deep into it.

1

u/[deleted] Jul 26 '19

It’s actually not python, it’s python syntax that gets converted into java on the backend.

1

u/_sachin_reddy_ Jul 27 '19

Would've been more satisfying if it drew something symmetric. Good job though. Guess things don't tend to be symmetric always.

3

u/MisterBlggs gitlab.com/MisterBiggs Jul 27 '19

One of the cool things about the double pendulum is that it's chaotic, which is kinda the opposite of satisfying but very interesting in its own right.

3

u/[deleted] Jul 26 '19

Huge fan of processing.org. I used it in college in my art degree over a decade ago. Super excited to play with this, thanks for sharing!

3

u/[deleted] Jul 26 '19

Also, seriously, I could watch this for hours. Nice work.

3

u/poditoo Jul 27 '19

I am the only one who gets too excited when the second pendulum makes a full loop?

It's like watching soccer, you see the angle of attack is right, the speed seems ok and it's getting close and SCOOOOOOOOOOORE.

Edit oh and none of the players are overpaid.

5

u/Thesludger Jul 26 '19

Pretty impressive. I wish I could create stuff like that as well

18

u/malicart Jul 26 '19

You can, that is the great thing about code.

3

u/Thesludger Jul 26 '19

Well, I'm trying my best and working on it

1

u/mahmud41 Jul 26 '19

curious to know if it is following actual physics. Anyone else?

0

u/[deleted] Jul 26 '19

[deleted]

2

u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19

If you watch Daniel Shiffmans video this is based on a formula that someone else derived, and it doesn't account for any loss of energy so it's definitely not a great simulation but it gets the idea across and it's pretty.

2

u/[deleted] Jul 26 '19
  1. Double pendulum python simulation

  2. Turn 3D and print

  3. Fruit basket

  4. ???

  5. Profit

2

u/Ol-Hull-Wrecker Jul 26 '19

How did you get it to copy my handwriting so well

2

u/Radical_Posture Jul 26 '19

That is fantastic!

2

u/CheeseIndustries Jul 27 '19

This is super cool. Thanks for sharing!

2

u/BrandNewThanos Jul 27 '19

I was interested in making designs using Processing, but I didn't know java. But thanks for letting us know about processing.py, I will be able to do it via python.

2

u/TimeTraveller-San Jul 26 '19

Reminds me of chaos theory

8

u/[deleted] Jul 26 '19

Isn't a two pendulum system chaotic?

8

u/PhyzzyKitty Jul 26 '19

It is

2

u/[deleted] Jul 26 '19

I was gonna say that... reminded me of Jurassic park too lol

5

u/PhyzzyKitty Jul 26 '19

The key to time travel may be the inverted Mobius strip, but cloning dinosaurs? That's double pendulum.

2

u/[deleted] Jul 26 '19

You sob you did it...

1

u/JanDerion47 flair=None Jul 26 '19

What about editing the base code of the universe?

3

u/PhyzzyKitty Jul 26 '19

Life will find a way... by exploiting the double pendulum

1

u/MildlyGoodWithPython Jul 26 '19

I watched this way more than I was supposed to. Very nice job

1

u/mahmud41 Jul 26 '19

I want this as my screen saver

1

u/foadsf Jul 26 '19

are you aware of pydy and its visualizations?

1

u/luckydo8a Jul 26 '19

Can be used as random number generator!

1

u/Silencer306 Jul 26 '19

This is interesting. I’d be interested in knowing if you can do this with more pendulums.

1

u/thebrashbhullar Jul 27 '19

Source code?

1

u/mrthordavis Jul 27 '19

I was waiting for some satisfying pattern to be made lol

1

u/nojeproks Jul 27 '19

I am amazed. 😍

1

u/jimflann Jul 27 '19

What's that sub where videos end too early? I was hoping to see it create a perfect curve. Very neat, kudos!

1

u/Sammanfattning Jul 27 '19

Satisfying •o•

1

u/FlowingLiquidity Oct 08 '19

Shiffman's tutorials are the best. I made the double pendulum swing yesterday. However, I'm trying to build in an SVG export so I can draw the created drawings on my DIY pen plotter but sadly I cannot find a way to create the full rendered image. My svg export only keeps exporting a single frame of the animation.

1

u/[deleted] Jul 26 '19

I almost thought it was gonna be dickbutt

2

u/limasxgoesto0 Jul 27 '19

Thanks, I thought I was the only one who thought this

1

u/Fig_g Jul 26 '19

Entropy

1

u/wolf2600 Jul 26 '19

I was waiting for it to draw dickbutt.

-1

u/onequestion1168 Jul 26 '19

this actually proves the earth is flat

0

u/2theduck Jul 27 '19

What happens when Niburu shows up

0

u/fork-private Jul 27 '19

Or as I like to call it, Naruto’s floppy ‘Rasengan’

-1

u/[deleted] Jul 26 '19

This is the most unsatisfying thing I’ve seen