.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_color_mixing.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_color_mixing.py: Color Mixing =========================== This example show how to create particles which mix colors when they touch .. GENERATED FROM PYTHON SOURCE LINES 7-76 .. 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 random import numpy import b2d class ColorMixing(TestbedBase): name = "ColorMixing" def __init__(self, settings=None): super(ColorMixing, self).__init__(settings=settings) dimensions = [30, 30] # the outer box box_shape = b2d.ChainShape() box_shape.create_loop( [ (0, 0), (0, dimensions[1]), (dimensions[0], dimensions[1]), (dimensions[0], 0), ] ) box = self.world.create_static_body(position=(0, 0), shape=box_shape) fixtureA = b2d.fixture_def( shape=b2d.circle_shape(1), density=2.2, friction=0.2, restitution=0.5 ) body = self.world.create_dynamic_body(position=(13, 10), fixtures=fixtureA) pdef = b2d.particle_system_def( viscous_strength=0.9, spring_strength=0.0, damping_strength=0.5, pressure_strength=0.5, color_mixing_strength=0.008, density=2, ) psystem = self.world.create_particle_system(pdef) psystem.radius = 0.3 psystem.damping = 1.0 colors = [ (255, 0, 0, 255), (0, 255, 0, 255), (0, 0, 255, 255), (255, 255, 0, 255), ] posiitons = [(6, 10), (20, 10), (20, 20), (6, 20)] for color, pos in zip(colors, posiitons): shape = b2d.polygon_shape(box=(5, 5), center=pos, angle=0) pgDef = b2d.particle_group_def( flags=b2d.ParticleFlag.waterParticle | b2d.ParticleFlag.colorMixingParticle, # group_flags=b2d.ParticleGroupFlag.solidParticleGroup, shape=shape, strength=1.0, color=color, ) group = psystem.create_particle_group(pgDef) if __name__ == "__main__": ani = b2d.testbed.run(ColorMixing) ani .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 48.855 seconds) .. _sphx_glr_download_auto_examples_plot_color_mixing.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_color_mixing.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_color_mixing.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_