.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/plot_tutorial_0.0.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_plot_tutorial_0.0.py: Tutorial 0.0: Hello World =========================== The first steps .. GENERATED FROM PYTHON SOURCE LINES 10-14 Imports ------------------------ pyb2d is imported as `b2d` .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: default import b2d import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 19-21 These imports are only needed for plotting. `b2d.plot` requires OpenCV to be installed! .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: default import b2d.plot import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 27-30 The first step with Box2D is the creation of the world. The world is parametrized by a gravity vector. .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: default # the world gravity = (0, -10) world = b2d.World(gravity) .. GENERATED FROM PYTHON SOURCE LINES 36-37 Create a circle-shaped body .. GENERATED FROM PYTHON SOURCE LINES 37-58 .. code-block:: default # the body def body_def = b2d.BodyDef() body_def.type = b2d.BodyType.dynamic body_def.position = (0, 0) # the body body = world.create_body(body_def) # shape circle_shape = b2d.CircleShape() circle_shape.radius = 1.0 # the fixture fixture_def = b2d.FixtureDef() fixture_def.shape = circle_shape fixture_def.density = 1.0 # create and add the fixture to the body fixture = body.create_fixture(fixture_def) .. GENERATED FROM PYTHON SOURCE LINES 59-63 We can now have a look at the world: We render the world st. each meter in the Box2D world will be 100 pixels in the image: .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: default pixels_per_meter = 100 b2d.plot.plot_world(world, ppm=pixels_per_meter) plt.show() .. image-sg:: /tutorials/images/sphx_glr_plot_tutorial_0.0_001.png :alt: plot tutorial 0.0 :srcset: /tutorials/images/sphx_glr_plot_tutorial_0.0_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-73 Lets run the world for a total of 5 seconds. Usually one wants to run the world at a certain frame rate. With the frame rate and the total time we can compute the delta for each iteration and how many steps we need .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: default t = 5 fps = 40 dt = 1.0 / fps n_steps = int(t / dt + 0.5) print(f"t={t} fps={fps} dt={dt} n_steps={n_steps}") .. rst-class:: sphx-glr-script-out .. code-block:: none t=5 fps=40 dt=0.025 n_steps=200 .. GENERATED FROM PYTHON SOURCE LINES 80-82 in each step we query the bodies position and velocity and store then for later plotting .. GENERATED FROM PYTHON SOURCE LINES 82-87 .. code-block:: default positions = np.zeros([n_steps, 2]) velocites = np.zeros([n_steps, 2]) timepoints = np.zeros([n_steps]) .. GENERATED FROM PYTHON SOURCE LINES 88-89 do it .. GENERATED FROM PYTHON SOURCE LINES 89-103 .. code-block:: default t_elapsed = 0.0 for i in range(n_steps): # get the bodies center of mass positions[i, :] = body.world_center # get the bodies velocity velocites[i, :] = body.linear_velocity timepoints[i] = t_elapsed world.step(time_step=dt, velocity_iterations=1, position_iterations=1) t_elapsed += dt .. GENERATED FROM PYTHON SOURCE LINES 104-107 plot the y-position against the time. We can see that the body is falling down in an accelerating way: .. GENERATED FROM PYTHON SOURCE LINES 107-111 .. code-block:: default plt.plot(timepoints, positions[:, 1]) plt.show() .. image-sg:: /tutorials/images/sphx_glr_plot_tutorial_0.0_002.png :alt: plot tutorial 0.0 :srcset: /tutorials/images/sphx_glr_plot_tutorial_0.0_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 112-114 as expected the x position is not changing since the gravity vector is non-zero only in the x direction .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: default plt.plot(timepoints, positions[:, 0]) plt.show() .. image-sg:: /tutorials/images/sphx_glr_plot_tutorial_0.0_003.png :alt: plot tutorial 0.0 :srcset: /tutorials/images/sphx_glr_plot_tutorial_0.0_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.213 seconds) .. _sphx_glr_download_tutorials_plot_tutorial_0.0.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_tutorial_0.0.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_tutorial_0.0.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_