.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/17_pybullet/viz_ball_collide.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_17_pybullet_viz_ball_collide.py: ========================= Ball Collision Simulation ========================= This example simulation shows how to use pybullet to render physics simulations in fury. In this example we render the collision between a blue ball and red ball and also display a message by confirming the collision. First some imports. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python import itertools import numpy as np import pybullet as p from fury import actor, ui, window client = p.connect(p.DIRECT) .. GENERATED FROM PYTHON SOURCE LINES 22-23 Parameters and definition of red and blue balls. .. GENERATED FROM PYTHON SOURCE LINES 23-56 .. code-block:: Python red_radius = 0.5 blue_radius = 0.5 duration = 50 # Red Ball red_ball_actor = actor.sphere( centers=np.array([[0, 0, 0]]), colors=np.array([[1, 0, 0]]), radii=red_radius ) red_ball_coll = p.createCollisionShape(p.GEOM_SPHERE, radius=red_radius) red_ball = p.createMultiBody( baseMass=0.5, baseCollisionShapeIndex=red_ball_coll, basePosition=[10, 0, 0], baseOrientation=[0, 0, 0, 1], ) # Blue ball blue_ball_actor = actor.sphere( centers=np.array([[0, 0, 0]]), colors=np.array([[0, 0, 1]]), radii=blue_radius ) blue_ball_coll = p.createCollisionShape(p.GEOM_SPHERE, radius=blue_radius) blue_ball = p.createMultiBody( baseMass=0.5, baseCollisionShapeIndex=blue_ball_coll, basePosition=[-10, 0, 0], baseOrientation=[0, 0, 0, 1], ) .. GENERATED FROM PYTHON SOURCE LINES 57-58 We set the coefficient of restitution of both the balls to `0.6`. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python p.changeDynamics(red_ball, -1, restitution=0.6) p.changeDynamics(blue_ball, -1, restitution=0.6) .. GENERATED FROM PYTHON SOURCE LINES 63-64 We add all the actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 64-77 .. code-block:: Python scene = window.Scene() scene.add(actor.axes()) scene.add(red_ball_actor) scene.add(blue_ball_actor) showm = window.ShowManager( scene, size=(900, 700), reset_camera=False, order_transparent=True ) counter = itertools.count() .. GENERATED FROM PYTHON SOURCE LINES 78-79 Method to sync objects. .. GENERATED FROM PYTHON SOURCE LINES 79-96 .. code-block:: Python def sync_actor(actor, multibody): pos, orn = p.getBasePositionAndOrientation(multibody) actor.SetPosition(*pos) orn_deg = np.degrees(p.getEulerFromQuaternion(orn)) actor.SetOrientation(*orn_deg) apply_force = True tb = ui.TextBlock2D(position=(0, 600), font_size=30, color=(1, 0.5, 0), text='') scene.add(tb) scene.set_camera( position=(0.30, -18.78, 0.89), focal_point=(0.15, 0.25, 0.40), view_up=(0, 0, 1.00) ) .. GENERATED FROM PYTHON SOURCE LINES 97-98 Timer callback to sync and step simulation every second. .. GENERATED FROM PYTHON SOURCE LINES 98-141 .. code-block:: Python def timer_callback(_obj, _event): global apply_force cnt = next(counter) showm.render() red_pos, red_orn = p.getBasePositionAndOrientation(red_ball) blue_pos, blue_orn = p.getBasePositionAndOrientation(blue_ball) # Apply force for the first step of the simulation. if apply_force: p.applyExternalForce( red_ball, -1, forceObj=[-40000, 0, 0], posObj=red_pos, flags=p.WORLD_FRAME ) p.applyExternalForce( blue_ball, -1, forceObj=[40000, 0, 0], posObj=blue_pos, flags=p.WORLD_FRAME ) apply_force = 0 sync_actor(blue_ball_actor, blue_ball) sync_actor(red_ball_actor, red_ball) # Get various collision information using `p.getContactPoints`. contact = p.getContactPoints(red_ball, blue_ball, -1, -1) if len(contact) != 0: tb.message = 'Collision!!' p.stepSimulation() if cnt == 50: showm.exit() showm.add_timer_callback(True, duration, timer_callback) interactive = False if interactive: showm.start() window.record(scene, size=(900, 700), out_path='viz_ball_collide.png') .. image-sg:: /auto_examples/17_pybullet/images/sphx_glr_viz_ball_collide_001.png :alt: viz ball collide :srcset: /auto_examples/17_pybullet/images/sphx_glr_viz_ball_collide_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.558 seconds) .. _sphx_glr_download_auto_examples_17_pybullet_viz_ball_collide.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_ball_collide.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_ball_collide.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_