Skip to content

Commit 44b235c

Browse files
authored
Merge pull request #74 from DerThorsten/main
added pyb2d examples to content, added ipyevents to dependencies
2 parents fe9368a + f9ae172 commit 44b235c

File tree

9 files changed

+2598
-0
lines changed

9 files changed

+2598
-0
lines changed

content/pyolite/pyb2d/0_tutorial.ipynb

Lines changed: 649 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import sys\n",
10+
"if \"pyodide\" in sys.modules:\n",
11+
" import piplite\n",
12+
" await piplite.install('pyb2d-jupyterlite-backend>=0.4.2')"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from b2d.testbed import TestbedBase\n",
22+
"import random\n",
23+
"import numpy\n",
24+
"import b2d\n",
25+
"\n",
26+
"class ColorMixing(TestbedBase):\n",
27+
"\n",
28+
" name = \"ColorMixing\"\n",
29+
"\n",
30+
" def __init__(self, settings=None):\n",
31+
" super(ColorMixing, self).__init__(settings=settings)\n",
32+
" dimensions = [30, 30]\n",
33+
"\n",
34+
" # the outer box\n",
35+
" box_shape = b2d.ChainShape()\n",
36+
" box_shape.create_loop(\n",
37+
" [\n",
38+
" (0, 0),\n",
39+
" (0, dimensions[1]),\n",
40+
" (dimensions[0], dimensions[1]),\n",
41+
" (dimensions[0], 0),\n",
42+
" ]\n",
43+
" )\n",
44+
" box = self.world.create_static_body(position=(0, 0), shape=box_shape)\n",
45+
"\n",
46+
" fixtureA = b2d.fixture_def(\n",
47+
" shape=b2d.circle_shape(1), density=2.2, friction=0.2, restitution=0.5\n",
48+
" )\n",
49+
" body = self.world.create_dynamic_body(position=(13, 10), fixtures=fixtureA)\n",
50+
"\n",
51+
" pdef = b2d.particle_system_def(\n",
52+
" viscous_strength=0.9,\n",
53+
" spring_strength=0.0,\n",
54+
" damping_strength=0.5,\n",
55+
" pressure_strength=0.5,\n",
56+
" color_mixing_strength=0.008,\n",
57+
" density=2,\n",
58+
" )\n",
59+
" psystem = self.world.create_particle_system(pdef)\n",
60+
" psystem.radius = 0.3\n",
61+
" psystem.damping = 1.0\n",
62+
"\n",
63+
" colors = [\n",
64+
" (255, 0, 0, 255),\n",
65+
" (0, 255, 0, 255),\n",
66+
" (0, 0, 255, 255),\n",
67+
" (255, 255, 0, 255),\n",
68+
" ]\n",
69+
" posiitons = [(6, 10), (20, 10), (20, 20), (6, 20)]\n",
70+
" for color, pos in zip(colors, posiitons):\n",
71+
"\n",
72+
" shape = b2d.polygon_shape(box=(5, 5), center=pos, angle=0)\n",
73+
" pgDef = b2d.particle_group_def(\n",
74+
" flags=b2d.ParticleFlag.waterParticle\n",
75+
" | b2d.ParticleFlag.colorMixingParticle,\n",
76+
" # group_flags=b2d.ParticleGroupFlag.solidParticleGroup,\n",
77+
" shape=shape,\n",
78+
" strength=1.0,\n",
79+
" color=color,\n",
80+
" )\n",
81+
" group = psystem.create_particle_group(pgDef)\n",
82+
"\n"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"from pyb2d_jupyterlite_backend.async_jupyter_gui import JupyterAsyncGui\n",
92+
"\n",
93+
"s = JupyterAsyncGui.Settings()\n",
94+
"s.resolution = [1000,500]\n",
95+
"s.scale = 8\n",
96+
"s.fps = 40\n",
97+
"\n",
98+
"tb = b2d.testbed.run(ColorMixing, backend=JupyterAsyncGui, gui_settings=s)"
99+
]
100+
}
101+
],
102+
"metadata": {
103+
"kernelspec": {
104+
"display_name": "Python 3 (ipykernel)",
105+
"language": "python",
106+
"name": "python3"
107+
},
108+
"language_info": {
109+
"codemirror_mode": {
110+
"name": "ipython",
111+
"version": 3
112+
},
113+
"file_extension": ".py",
114+
"mimetype": "text/x-python",
115+
"name": "python",
116+
"nbconvert_exporter": "python",
117+
"pygments_lexer": "ipython3",
118+
"version": "3.10.4"
119+
}
120+
},
121+
"nbformat": 4,
122+
"nbformat_minor": 4
123+
}

0 commit comments

Comments
 (0)