.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_newtons_cradle.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_newtons_cradle.py: Newtons Cradle =========================== This example shows how to implement newtons cradle with b2d .. GENERATED FROM PYTHON SOURCE LINES 7-69 .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/.local/lib/python3.10/site-packages/b2d/testbed/backend/matplotlib_gif_gui/matplotlib_gif_gui.py:52: UserWarning: You passed in an explicit save_count=240 which is being ignored in favor of frames=240. self.ani = animation.FuncAnimation( | .. code-block:: default from b2d.testbed import TestbedBase import b2d class NewtonsCradle(TestbedBase): name = "newton's cradle" def __init__(self, settings=None): super(NewtonsCradle, self).__init__(settings=settings) # radius of the circles r = 1.0 # length of the rope l = 10.0 # how many balls n = 10 offset = (l + r, 2 * r) dynamic_circles = [] static_bodies = [] for i in range(n): if i + 1 == n: position = (offset[0] + i * 2 * r + l, offset[1] + l) else: position = (offset[0] + i * 2 * r, offset[1]) circle = self.world.create_dynamic_body( position=position, fixtures=b2d.fixture_def( shape=b2d.circle_shape(radius=r * 0.90), density=1.0, restitution=1.0, friction=0.0, ), linear_damping=0.01, angular_damping=1.0, fixed_rotation=True, ) dynamic_circles.append(circle) static_body = self.world.create_static_body( position=(offset[0] + i * 2 * r, offset[1] + l) ) self.world.create_distance_joint( static_body, circle, local_anchor_a=(0, 0), local_anchor_b=(0, 0), max_length=l, stiffness=0, ) static_bodies.append(static_body) if __name__ == "__main__": ani = b2d.testbed.run(NewtonsCradle) ani .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 37.674 seconds) .. _sphx_glr_download_auto_examples_plot_newtons_cradle.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_newtons_cradle.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_newtons_cradle.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_