.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/joints/plot_distance_joint.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_tutorials_joints_plot_distance_joint.py: Distance Joint =========================== Uses: * :class:`b2d.DistanceJointDef` * :class:`b2d.DistanceJoint` .. GENERATED FROM PYTHON SOURCE LINES 12-13 Imports .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: default import b2d import numpy as np import b2d.plot import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 20-24 Create distance joints between a static anchor and a body directly below the anchor. We create 10 distance joints with various values for the stiffness .. GENERATED FROM PYTHON SOURCE LINES 24-47 .. code-block:: default world = b2d.world(gravity=(0, -10)) for i in range(10): # create static anchor (does not need shape/fixture) anchor = world.create_static_body(position=(i, 0)) # 5 below the anchor body = world.create_dynamic_body( position=(i, -10), fixtures=b2d.fixture_def(shape=b2d.circle_shape(radius=0.4), density=0.5), ) # distance joints of various stiffness-es world.create_distance_joint(anchor, body, length=10, stiffness=0.5 * (i + 1)) fig, ax, ani = b2d.plot.animate_world(world, world_margin=(10, 10), t=5) ani plt.title("increasing stiffness from left to right") plt.show() .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/.local/lib/python3.10/site-packages/b2d/plot.py:128: UserWarning: You passed in an explicit save_count=121 which is being ignored in favor of frames=121. ani = animation.FuncAnimation( .. GENERATED FROM PYTHON SOURCE LINES 48-49 Here we vary the damping of the distance joint .. GENERATED FROM PYTHON SOURCE LINES 49-72 .. code-block:: default world = b2d.world(gravity=(0, -10)) for i in range(10): # create static anchor (does not need shape/fixture) anchor = world.create_static_body(position=(i, 0)) # 5 below the anchor body = world.create_dynamic_body( position=(i, -10), fixtures=b2d.fixture_def(shape=b2d.circle_shape(radius=0.4), density=0.5), ) # distance joints of various stiffness-es world.create_distance_joint(anchor, body, length=10, stiffness=2, damping=0.05 * i) fig, ax, ani = b2d.plot.animate_world(world, world_margin=(10, 10), t=5) ani plt.title("increasing damping from left to right") plt.show() .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/.local/lib/python3.10/site-packages/b2d/plot.py:128: UserWarning: You passed in an explicit save_count=121 which is being ignored in favor of frames=121. ani = animation.FuncAnimation( .. GENERATED FROM PYTHON SOURCE LINES 73-74 Distance joints can be used to create wobbly structures .. GENERATED FROM PYTHON SOURCE LINES 74-104 .. code-block:: default world = b2d.world(gravity=(0, -10)) # Create a ground body edge = world.create_static_body( position=(0, 0), fixtures=b2d.fixture_def(shape=b2d.edge_shape([(-10, 0), (10, 0)])) ) # create 3 bodies a = world.create_dynamic_body( position=(-3, 4), fixtures=b2d.fixture_def(shape=b2d.circle_shape(radius=1), density=1), ) b = world.create_dynamic_body( position=(3, 4), fixtures=b2d.fixture_def(shape=b2d.circle_shape(radius=1), density=1), ) c = world.create_dynamic_body( position=(0, 8), fixtures=b2d.fixture_def(shape=b2d.circle_shape(radius=1), density=1), ) # connect bodies with distance joints distance_joint_def = dict(length=5, stiffness=100) world.create_distance_joint(a, b, **distance_joint_def) world.create_distance_joint(a, c, **distance_joint_def) world.create_distance_joint(b, c, **distance_joint_def) fig, ax, ani = b2d.plot.animate_world(world, world_margin=(10, 10)) ani plt.show() .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/.local/lib/python3.10/site-packages/b2d/plot.py:128: UserWarning: You passed in an explicit save_count=73 which is being ignored in favor of frames=73. ani = animation.FuncAnimation( .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 1 minutes 5.143 seconds) .. _sphx_glr_download_tutorials_joints_plot_distance_joint.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_distance_joint.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_distance_joint.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_