pyb2d3 0.7.4__tar.gz → 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.pre-commit-config.yaml +3 -3
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/PKG-INFO +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/sample_base.py +4 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyproject.toml +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/ipycanvas_frontend.py +57 -12
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/render_loop.py +3 -0
- {pyb2d3-0.7.4/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter → pyb2d3-0.9.0/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas}/ui.py +88 -20
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyproject.toml +2 -2
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyproject.toml +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyproject.toml +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_pygame/pyproject.toml +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/doc_tools/__init__.py +3 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/__init__.py +6 -2
- pyb2d3-0.9.0/examples/pyb2d3_samples/angry_shapes.py +112 -0
- pyb2d3-0.7.4/examples/pyb2d3_samples/billard.py → pyb2d3-0.9.0/examples/pyb2d3_samples/billiard.py +2 -2
- pyb2d3-0.9.0/examples/pyb2d3_samples/examples_common/__init__.py +35 -0
- pyb2d3-0.9.0/examples/pyb2d3_samples/examples_common/math.py +20 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/goo_game.py +0 -8
- pyb2d3-0.9.0/examples/pyb2d3_samples/minigolf.py +125 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/ragdoll.py +2 -9
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/text.py +2 -15
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/tumbler.py +0 -10
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/pyproject.toml +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/scripts/build_emscripten_forge_pkg.sh +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/scripts/build_lite_env.sh +5 -3
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/scripts/build_lite_page.sh +1 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/scripts/examples_to_notebooks.sh +1 -1
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/tests/test_samples.py +4 -0
- pyb2d3-0.7.4/examples/pyb2d3_samples/examples_common/__init__.py +0 -2
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.clang-format +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.github/workflows/conda.yaml +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.github/workflows/docs.yaml +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.github/workflows/emscripten.yaml +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.github/workflows/publish.yaml +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/.gitignore +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/CMakeLists.txt +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/LICENSE +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/README.md +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/build_pkg.sh +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/default_frontend.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/frontend_base.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/widgets.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/debug_draw_offscreen.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/debug_draw_vanilla.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas/init.js +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/canvas_widget.css +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/canvas_widget.js +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/canvas_widget.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/exp.ipynb +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/jupyter_debug_draw.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/jupyter_frontend.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter/render_loop.py +0 -0
- {pyb2d3-0.7.4/companion_packages/pyb2d3_sandbox_ipycanvas/pyb2d3_sandbox_ipycanvas → pyb2d3-0.9.0/companion_packages/pyb2d3_sandbox_jupyter/pyb2d3_sandbox_jupyter}/ui.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/LICENSE-Box2D +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/LICENSE-box2d-py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/debug_draw_gl.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/draw.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/opengl_frontend.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shader.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/background.fs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/background.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/circle.fs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/circle.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_capsule.fs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_capsule.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_circle.fs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_circle.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_polygon.fs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_polygon.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_opengl/pyb2d3_sandbox_opengl/shaders/solid_polygon_debug.vs +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_pygame/pyb2d3_sandbox_pygame/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_pygame/pyb2d3_sandbox_pygame/pygame_frontend.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox_pygame/pyb2d3_sandbox_pygame/sample_to_video.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/dev-environment.yaml +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/.gitignore +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/Makefile +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/conf.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/index.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/src/about_the_docs.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/src/api.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/src/differences.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/src/installation.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/docs/src/samples/index.rst +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/emscripten_forge/recipe/build_pkg.sh +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/emscripten_forge/recipe/recipe.yaml.template +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/emscripten_forge/recipe/test_pyb2d3.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/cast_ray_callback.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/chain_builder.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/coupled_minigolf.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/examples_common/ragdoll.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/examples_common/text.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/joints.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/jump.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/meteor.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/mirror.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/newtons_cradle.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/raycast.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/shapes.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/examples/pyb2d3_samples/soft_bodies.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/extra_licenses/LICENSE_BOX2D +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/extra_licenses/LICENSE_NANOBIND +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/extra_licenses/README +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/batch_api/batch_exporters.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/batch_api/ndarray_traits.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/batch_api.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/debug_draw.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/numpy_utils.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/py_chain_def.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/py_converter.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/threadpool.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/world_to_canvas.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/include/pyb2d3/wrapper_structs.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/scripts/README.md +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_batch_api.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_box2d_types.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_box2d_wrapper_structs.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_collision.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_extras.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_math_functions.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_threadpool.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/export_world_to_canvas.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/main.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/module/pyb2d3/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/py_debug_draw.cpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/src/py_debug_draw.hpp +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/tests/__init__.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/tests/conftest.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/tests/headless_test_frontend.py +0 -0
- {pyb2d3-0.7.4 → pyb2d3-0.9.0}/tests/test_pyb2d3.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
repos:
|
|
2
2
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
3
|
-
rev:
|
|
3
|
+
rev: v6.0.0
|
|
4
4
|
hooks:
|
|
5
5
|
- id: check-yaml
|
|
6
6
|
- id: end-of-file-fixer
|
|
@@ -8,7 +8,7 @@ repos:
|
|
|
8
8
|
|
|
9
9
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
10
10
|
# Ruff version.
|
|
11
|
-
rev: v0.
|
|
11
|
+
rev: v0.14.7
|
|
12
12
|
hooks:
|
|
13
13
|
# Run the linter.
|
|
14
14
|
- id: ruff-check
|
|
@@ -17,7 +17,7 @@ repos:
|
|
|
17
17
|
- id: ruff-format
|
|
18
18
|
|
|
19
19
|
- repo: https://github.com/pre-commit/mirrors-clang-format
|
|
20
|
-
rev:
|
|
20
|
+
rev: v21.1.6
|
|
21
21
|
hooks:
|
|
22
22
|
- id: clang-format
|
|
23
23
|
exclude_types: [javascript, json, python, yaml]
|
{pyb2d3-0.7.4 → pyb2d3-0.9.0}/companion_packages/pyb2d3_sandbox/pyb2d3_sandbox/sample_base.py
RENAMED
|
@@ -26,7 +26,10 @@ class SampleBase(object):
|
|
|
26
26
|
sample_settings = cls.Settings()
|
|
27
27
|
if frontend_class is None:
|
|
28
28
|
frontend_class = get_default_frontend()
|
|
29
|
-
|
|
29
|
+
|
|
30
|
+
if isinstance(frontend_settings, dict):
|
|
31
|
+
frontend_settings = frontend_class.Settings(**frontend_settings)
|
|
32
|
+
elif frontend_settings is None:
|
|
30
33
|
frontend_settings = frontend_class.Settings()
|
|
31
34
|
|
|
32
35
|
frontend = frontend_class(settings=frontend_settings)
|
|
@@ -25,6 +25,12 @@ import traceback
|
|
|
25
25
|
|
|
26
26
|
from ipycanvas.compat import Canvas
|
|
27
27
|
|
|
28
|
+
# dataclass
|
|
29
|
+
from dataclasses import dataclass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
from weakref import WeakSet
|
|
33
|
+
|
|
28
34
|
|
|
29
35
|
def html_color(color):
|
|
30
36
|
"""Convert a color to a hex string"""
|
|
@@ -54,15 +60,24 @@ else:
|
|
|
54
60
|
from .debug_draw_offscreen import IpycanvasDebugDraw
|
|
55
61
|
|
|
56
62
|
|
|
57
|
-
|
|
63
|
+
all_frontends = WeakSet()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass
|
|
67
|
+
class IpycanvasFrontendSettings(FrontendBase.Settings):
|
|
68
|
+
layout_scale: float = 1.0
|
|
69
|
+
hide_controls: bool = False
|
|
70
|
+
autostart: bool = True
|
|
71
|
+
simple_ui: bool = False
|
|
58
72
|
|
|
59
73
|
|
|
60
74
|
class IpycanvasFrontend(FrontendBase):
|
|
61
|
-
Settings =
|
|
75
|
+
Settings = IpycanvasFrontendSettings
|
|
62
76
|
|
|
63
77
|
def __del__(self):
|
|
64
78
|
if self.cancel_loop is not None:
|
|
65
79
|
self.cancel_loop()
|
|
80
|
+
self.cancel_loop = None
|
|
66
81
|
|
|
67
82
|
def _handle_exception(self, e):
|
|
68
83
|
"""Handle exceptions in the frontend"""
|
|
@@ -70,13 +85,13 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
70
85
|
print(f"Error: {e}", file=sys.stderr)
|
|
71
86
|
if self.cancel_loop is not None:
|
|
72
87
|
self.cancel_loop()
|
|
88
|
+
self.cancel_loop = None
|
|
73
89
|
raise e
|
|
74
90
|
|
|
75
91
|
def __init__(self, settings):
|
|
76
|
-
global
|
|
92
|
+
global use_offscreen
|
|
77
93
|
self.output_widget = Output()
|
|
78
94
|
self.cancel_loop = None
|
|
79
|
-
|
|
80
95
|
self._ignore_these_keys = set("Control, Shift, Meta")
|
|
81
96
|
|
|
82
97
|
super().__init__(settings)
|
|
@@ -85,13 +100,15 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
85
100
|
self.canvas = Canvas(
|
|
86
101
|
width=self.settings.canvas_shape[0],
|
|
87
102
|
height=self.settings.canvas_shape[1],
|
|
88
|
-
|
|
103
|
+
layout=dict(width="100%"),
|
|
89
104
|
)
|
|
105
|
+
|
|
90
106
|
# if a cell is re-executed, we need to cancel the previous loop,
|
|
91
107
|
# otherwise we will have multiple loops running
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
|
|
109
|
+
if self.settings.autostart:
|
|
110
|
+
self.cancel_other_frontend_loops()
|
|
111
|
+
all_frontends.add(self)
|
|
95
112
|
|
|
96
113
|
self.transform = b2d.CanvasWorldTransform(
|
|
97
114
|
canvas_shape=self.settings.canvas_shape,
|
|
@@ -113,9 +130,6 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
113
130
|
|
|
114
131
|
self._last_canvas_mouse_pos = b2d.Vec2(0, 0)
|
|
115
132
|
|
|
116
|
-
# display the canvas
|
|
117
|
-
self.ui.display()
|
|
118
|
-
|
|
119
133
|
except Exception as e:
|
|
120
134
|
self._handle_exception(e)
|
|
121
135
|
|
|
@@ -304,6 +318,7 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
304
318
|
|
|
305
319
|
if self.sample.is_done():
|
|
306
320
|
self.cancel_loop()
|
|
321
|
+
self.cancel_loop = None
|
|
307
322
|
self.sample.post_run()
|
|
308
323
|
return
|
|
309
324
|
|
|
@@ -315,6 +330,7 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
315
330
|
print("Error in main loop:", file=sys.stderr)
|
|
316
331
|
self.output_widget.append_stdout(f"Error in main loop: {traceback.format_exc()}\n")
|
|
317
332
|
self.cancel_loop()
|
|
333
|
+
self.cancel_loop = None
|
|
318
334
|
|
|
319
335
|
def main_loop_vanilla(self):
|
|
320
336
|
self.ui_is_ready()
|
|
@@ -325,7 +341,29 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
325
341
|
|
|
326
342
|
self.cancel_loop = set_render_loop(self.canvas, f)
|
|
327
343
|
|
|
344
|
+
def restart(self):
|
|
345
|
+
if self.cancel_loop is None:
|
|
346
|
+
|
|
347
|
+
def f(_):
|
|
348
|
+
self._callback()
|
|
349
|
+
|
|
350
|
+
self.cancel_other_frontend_loops()
|
|
351
|
+
|
|
352
|
+
self.cancel_loop = ipycanvas_set_render_loop(self.canvas, f, fps=0)
|
|
353
|
+
|
|
354
|
+
def cancel_other_frontend_loops(self):
|
|
355
|
+
for other_frontend in all_frontends:
|
|
356
|
+
if other_frontend is not self and other_frontend.cancel_loop is not None:
|
|
357
|
+
other_frontend.ui._set_paused()
|
|
358
|
+
other_frontend.cancel_loop()
|
|
359
|
+
# pyjs.cancel_main_loop()
|
|
360
|
+
other_frontend.cancel_loop = None
|
|
361
|
+
|
|
328
362
|
async def async_main_loop(self):
|
|
363
|
+
await asyncio.sleep(0.2)
|
|
364
|
+
# display the canvas
|
|
365
|
+
self.ui.display()
|
|
366
|
+
await asyncio.sleep(0.2) # give the canvas some time to initialize
|
|
329
367
|
try:
|
|
330
368
|
await self.canvas.async_initialize()
|
|
331
369
|
self.ui_is_ready()
|
|
@@ -335,7 +373,13 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
335
373
|
self._callback()
|
|
336
374
|
|
|
337
375
|
# fps=0 means use requestAnimationFrame
|
|
338
|
-
|
|
376
|
+
if self.settings.autostart:
|
|
377
|
+
self.cancel_other_frontend_loops()
|
|
378
|
+
self.cancel_loop = ipycanvas_set_render_loop(self.canvas, f, fps=0)
|
|
379
|
+
else:
|
|
380
|
+
self._clear_canvas()
|
|
381
|
+
self.update_physics_single_step()
|
|
382
|
+
self.draw_physics()
|
|
339
383
|
|
|
340
384
|
except Exception as e:
|
|
341
385
|
self._handle_exception(e)
|
|
@@ -349,6 +393,7 @@ class IpycanvasFrontend(FrontendBase):
|
|
|
349
393
|
if is_emscripten:
|
|
350
394
|
self.main_loop_lite()
|
|
351
395
|
else:
|
|
396
|
+
self.ui.display()
|
|
352
397
|
self.main_loop_vanilla()
|
|
353
398
|
|
|
354
399
|
def _dispatch_events(self, event):
|
|
@@ -11,20 +11,60 @@ class TestbedUI:
|
|
|
11
11
|
self._canvas = frontend.canvas
|
|
12
12
|
self._output_widget = frontend.output_widget
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
if not self.frontend.settings.simple_ui:
|
|
15
|
+
self._header = self._make_header()
|
|
16
|
+
self._right_sidebar = self._make_right_sidebar()
|
|
17
|
+
self._footer = self._make_footer()
|
|
18
|
+
|
|
19
|
+
if frontend.settings.hide_controls:
|
|
20
|
+
pane_widths = [
|
|
21
|
+
0,
|
|
22
|
+
f"{100.0 * frontend.settings.layout_scale}%",
|
|
23
|
+
0,
|
|
24
|
+
]
|
|
25
|
+
right_sidebar = None
|
|
26
|
+
else:
|
|
27
|
+
pane_widths = [
|
|
28
|
+
0,
|
|
29
|
+
f"{70.0 * frontend.settings.layout_scale}%",
|
|
30
|
+
f"{30.0 * frontend.settings.layout_scale}%",
|
|
31
|
+
]
|
|
32
|
+
right_sidebar = self._right_sidebar
|
|
33
|
+
|
|
34
|
+
self.ui = ipywidgets.AppLayout(
|
|
35
|
+
header=self._header,
|
|
36
|
+
center=self._canvas,
|
|
37
|
+
right_sidebar=right_sidebar,
|
|
38
|
+
left_sidebar=None,
|
|
39
|
+
footer=self._footer,
|
|
40
|
+
pane_heights=["60px", 5, "60px"],
|
|
41
|
+
pane_widths=pane_widths,
|
|
42
|
+
)
|
|
43
|
+
else:
|
|
44
|
+
button_group = self._make_control_button_group()
|
|
45
|
+
# we want the controll in the lower **ontop** of the canvas
|
|
46
|
+
# ie some sort of floating layout
|
|
47
|
+
|
|
48
|
+
floating_layout = Layout(
|
|
49
|
+
position="absolute",
|
|
50
|
+
bottom="40px", # 10 pixels from the bottom edge
|
|
51
|
+
left="0", # 10 pixels from the left edge
|
|
52
|
+
# A small z-index ensures it renders above the canvas
|
|
53
|
+
z_index="10",
|
|
54
|
+
width="auto",
|
|
55
|
+
)
|
|
56
|
+
button_group.layout = floating_layout
|
|
57
|
+
|
|
58
|
+
# 4. Create the Parent Container (VBox)
|
|
59
|
+
# The VBox will hold both the canvas and the button.
|
|
60
|
+
# Crucially, the VBox must have position: 'relative' for the button's
|
|
61
|
+
# 'absolute' positioning to work relative to it.
|
|
62
|
+
container_layout = Layout(
|
|
63
|
+
width="100%", # or '99.9%'
|
|
64
|
+
position="relative", # <-- THIS IS THE KEY!
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
self.ui = VBox(children=[self._canvas, button_group], layout=container_layout)
|
|
28
68
|
|
|
29
69
|
def _make_header(self):
|
|
30
70
|
layout = Layout(height="60px")
|
|
@@ -86,6 +126,7 @@ class TestbedUI:
|
|
|
86
126
|
layout=Layout(height="auto", justify_content="flex-start", width="auto"),
|
|
87
127
|
)
|
|
88
128
|
accordion.set_title(0, "Drawing Settings:")
|
|
129
|
+
|
|
89
130
|
return accordion
|
|
90
131
|
|
|
91
132
|
def _make_sample_settings_accordion(self):
|
|
@@ -208,7 +249,7 @@ class TestbedUI:
|
|
|
208
249
|
|
|
209
250
|
# open this by default
|
|
210
251
|
self._simulation_settings_accordion.selected_index = 0
|
|
211
|
-
self._debug_draw_accordion.selected_index = 0
|
|
252
|
+
# self._debug_draw_accordion.selected_index = 0
|
|
212
253
|
|
|
213
254
|
return ipywidgets.VBox(
|
|
214
255
|
[
|
|
@@ -216,7 +257,7 @@ class TestbedUI:
|
|
|
216
257
|
self._debug_draw_accordion,
|
|
217
258
|
self._sample_settings_accordion,
|
|
218
259
|
],
|
|
219
|
-
layout=Layout(display="flex", justify_content="flex-start", width="
|
|
260
|
+
layout=Layout(display="flex", justify_content="flex-start", width="100%"),
|
|
220
261
|
)
|
|
221
262
|
|
|
222
263
|
def _make_left_sidebar(self):
|
|
@@ -236,8 +277,14 @@ class TestbedUI:
|
|
|
236
277
|
return ipywidgets.Label("")
|
|
237
278
|
|
|
238
279
|
def _make_control_button_group(self):
|
|
280
|
+
autostart = self.frontend.settings.autostart
|
|
281
|
+
if autostart:
|
|
282
|
+
icon = "pause"
|
|
283
|
+
else:
|
|
284
|
+
icon = "play"
|
|
285
|
+
|
|
239
286
|
self.play_pause_btn = ToggleButton(
|
|
240
|
-
value=
|
|
287
|
+
value=autostart, tooltip="Play/Pause", icon=icon, layout=Layout(width="40px")
|
|
241
288
|
)
|
|
242
289
|
self.stop_btn = Button(
|
|
243
290
|
tooltip="Stop", icon="stop", layout=Layout(width="40px"), button_style="danger"
|
|
@@ -263,10 +310,15 @@ class TestbedUI:
|
|
|
263
310
|
)
|
|
264
311
|
|
|
265
312
|
def remove_sample_ui_elements(self):
|
|
266
|
-
|
|
267
|
-
|
|
313
|
+
if not self.frontend.settings.simple_ui:
|
|
314
|
+
# remove all children from the sample settings vbox
|
|
315
|
+
self.sample_settings_vbox.children = []
|
|
268
316
|
|
|
269
317
|
def add_sample_ui_element(self, element):
|
|
318
|
+
# no ui-elements in simple mode
|
|
319
|
+
if self.frontend.settings.simple_ui:
|
|
320
|
+
return
|
|
321
|
+
|
|
270
322
|
# ensure the accordion is visible
|
|
271
323
|
if not self._sample_settings_accordion.selected_index == 0:
|
|
272
324
|
self._sample_settings_accordion.selected_index = 0
|
|
@@ -334,11 +386,25 @@ class TestbedUI:
|
|
|
334
386
|
)
|
|
335
387
|
self.sample_settings_vbox.children += (radio_buttons,)
|
|
336
388
|
|
|
389
|
+
def _set_paused(self):
|
|
390
|
+
self.play_pause_btn.icon = "play"
|
|
391
|
+
self.play_pause_btn.value = False
|
|
392
|
+
self.on_pause()
|
|
393
|
+
self.single_step_btn.disabled = False
|
|
394
|
+
|
|
395
|
+
def _set_running(self):
|
|
396
|
+
self.play_pause_btn.icon = "pause"
|
|
397
|
+
self.play_pause_btn.value = True
|
|
398
|
+
self.single_step_btn.disabled = True
|
|
399
|
+
self.on_play()
|
|
400
|
+
|
|
337
401
|
def _on_play_pause_change(self, change):
|
|
338
402
|
try:
|
|
339
403
|
if change["new"]:
|
|
340
404
|
self.play_pause_btn.icon = "pause"
|
|
341
405
|
self.single_step_btn.disabled = True
|
|
406
|
+
if self.frontend.cancel_loop is None:
|
|
407
|
+
self.frontend.restart()
|
|
342
408
|
self.on_play()
|
|
343
409
|
else:
|
|
344
410
|
self.play_pause_btn.icon = "play"
|
|
@@ -353,6 +419,8 @@ class TestbedUI:
|
|
|
353
419
|
self.play_pause_btn.value = False
|
|
354
420
|
self.single_step_btn.disabled = False
|
|
355
421
|
self.play_pause_btn.icon = "play"
|
|
422
|
+
if self.frontend.cancel_loop is not None:
|
|
423
|
+
self.frontend.restart()
|
|
356
424
|
self.on_stop(was_playing_before)
|
|
357
425
|
except Exception as e:
|
|
358
426
|
self.frontend._handle_exception(e)
|
|
@@ -392,4 +460,4 @@ class TestbedUI:
|
|
|
392
460
|
self.frontend.update_physics_single_step()
|
|
393
461
|
|
|
394
462
|
def display(self):
|
|
395
|
-
display(self.
|
|
463
|
+
display(self.ui, self._output_widget)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pyb2d3-sandbox-ipycanvas"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.9.0"
|
|
4
4
|
description = "run the pyb2d3 sandbox with ipycanvas"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Dr. Thorsten Beier", email = "derthorstenbeier@gmail.com" }
|
|
@@ -12,7 +12,7 @@ license = { text = "MIT" }
|
|
|
12
12
|
dependencies = [
|
|
13
13
|
"pyb2d3>=0.7.4",
|
|
14
14
|
"pyb2d3-sandbox>=0.7.4",
|
|
15
|
-
"ipycanvas>=0.14.
|
|
15
|
+
"ipycanvas>=0.14.2",
|
|
16
16
|
"ipyevents",
|
|
17
17
|
]
|
|
18
18
|
|
|
@@ -10,9 +10,12 @@ docs_root = this_dir.parent
|
|
|
10
10
|
html_static_path = docs_root / "_static"
|
|
11
11
|
repo_root = docs_root.parent
|
|
12
12
|
examples_dir = repo_root / "examples"
|
|
13
|
+
samples_dir = examples_dir / "pyb2d3_samples"
|
|
13
14
|
companion_packages = repo_root / "companion_packages"
|
|
14
15
|
|
|
16
|
+
|
|
15
17
|
# add to sys.path
|
|
18
|
+
sys.path.insert(0, str(samples_dir))
|
|
16
19
|
sys.path.insert(0, str(companion_packages / "pyb2d3_sandbox"))
|
|
17
20
|
sys.path.insert(0, str(companion_packages / "pyb2d3_sandbox_pygame"))
|
|
18
21
|
|
|
@@ -9,11 +9,13 @@ from .coupled_minigolf import CoupledMinigolf
|
|
|
9
9
|
from .joints import Joints
|
|
10
10
|
from .goo_game import Level1 as GooGameLevel1
|
|
11
11
|
from .chain_builder import ChainBuilder
|
|
12
|
-
from .
|
|
12
|
+
from .billiard import Billiard
|
|
13
13
|
from .meteor import Meteor
|
|
14
14
|
from .ragdoll import Ragdoll
|
|
15
15
|
from .cast_ray_callback import CastRayCallback
|
|
16
16
|
from .text import Text
|
|
17
|
+
from .minigolf import Minigolf
|
|
18
|
+
from .angry_shapes import AngryShapes
|
|
17
19
|
|
|
18
20
|
all_examples = [
|
|
19
21
|
Ragdoll,
|
|
@@ -29,7 +31,9 @@ all_examples = [
|
|
|
29
31
|
Joints,
|
|
30
32
|
GooGameLevel1,
|
|
31
33
|
ChainBuilder,
|
|
32
|
-
|
|
34
|
+
Billiard,
|
|
33
35
|
CastRayCallback,
|
|
34
36
|
Text,
|
|
37
|
+
Minigolf,
|
|
38
|
+
AngryShapes,
|
|
35
39
|
]
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# +
|
|
2
|
+
import pyb2d3 as b2d
|
|
3
|
+
from pyb2d3_sandbox import SampleBase
|
|
4
|
+
from examples_common import stacked_blocks
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AngryShapes(SampleBase):
|
|
8
|
+
def __init__(self, frontend, settings):
|
|
9
|
+
super().__init__(frontend, settings.set_gravity((0, -10)))
|
|
10
|
+
|
|
11
|
+
self.floor_size = 27.5
|
|
12
|
+
|
|
13
|
+
self.anchor_body.create_shape(
|
|
14
|
+
b2d.shape_def(material=b2d.surface_material(restitution=0.5)),
|
|
15
|
+
b2d.segment(
|
|
16
|
+
(-1, 0),
|
|
17
|
+
(28, 0),
|
|
18
|
+
),
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
stacked_blocks(self.world, num_blocks_base=10, box_shape=[0.5, 3.0], start=(0, 0))
|
|
22
|
+
|
|
23
|
+
self.projectile_radius = 1.0
|
|
24
|
+
self.projectile_anchor_pos = b2d.Vec2(-30, 2)
|
|
25
|
+
self.projectile_pos = b2d.Vec2(self.projectile_anchor_pos)
|
|
26
|
+
self.projectile_body = None
|
|
27
|
+
self.projectile_state = "wait_for_movement"
|
|
28
|
+
|
|
29
|
+
def on_mouse_down(self, event):
|
|
30
|
+
print("state:", self.projectile_state)
|
|
31
|
+
if self.projectile_state == "wait_for_movement":
|
|
32
|
+
# inside projectile ?
|
|
33
|
+
d = b2d.distance(event.world_position, self.projectile_pos)
|
|
34
|
+
if d <= self.projectile_radius:
|
|
35
|
+
self.projectile_state = "dragging"
|
|
36
|
+
|
|
37
|
+
def on_mouse_move(self, event):
|
|
38
|
+
if self.projectile_state == "dragging":
|
|
39
|
+
# move projectile position to mouse position
|
|
40
|
+
self.projectile_pos = b2d.Vec2(event.world_position)
|
|
41
|
+
|
|
42
|
+
def on_mouse_up(self, event):
|
|
43
|
+
if self.projectile_state == "dragging":
|
|
44
|
+
# launch projectile by a force proportional to the distance from anchor
|
|
45
|
+
dir_vec = self.projectile_anchor_pos - event.world_position
|
|
46
|
+
force = dir_vec * 50
|
|
47
|
+
|
|
48
|
+
# create projectile body
|
|
49
|
+
self.projectile_body = self.world.create_dynamic_body(position=self.projectile_pos)
|
|
50
|
+
|
|
51
|
+
shape = self.projectile_body.create_shape(
|
|
52
|
+
b2d.shape_def(density=1, material=b2d.surface_material(restitution=0.5)),
|
|
53
|
+
b2d.circle(radius=self.projectile_radius),
|
|
54
|
+
)
|
|
55
|
+
shape.contact_events_enabled = True
|
|
56
|
+
shape.user_data = 1
|
|
57
|
+
|
|
58
|
+
self.projectile_body.apply_linear_impulse_to_center(force)
|
|
59
|
+
self.projectile_state = "launched"
|
|
60
|
+
|
|
61
|
+
def post_debug_draw(self):
|
|
62
|
+
if self.projectile_state == "dragging":
|
|
63
|
+
# draw a line from anchor to projectile position
|
|
64
|
+
self.debug_draw.draw_segment(
|
|
65
|
+
p1=self.projectile_anchor_pos,
|
|
66
|
+
p2=self.projectile_pos,
|
|
67
|
+
color=b2d.hex_color(200, 50, 50),
|
|
68
|
+
)
|
|
69
|
+
# draw the projectile (either as a circle at the position or the body if launched)
|
|
70
|
+
if self.projectile_body is None:
|
|
71
|
+
# draw circle at position
|
|
72
|
+
self.debug_draw.draw_solid_circle(
|
|
73
|
+
transform=b2d.transform(p=self.projectile_pos),
|
|
74
|
+
radius=self.projectile_radius,
|
|
75
|
+
color=b2d.hex_color(200, 50, 50),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
def pre_update(self, dt):
|
|
79
|
+
events = self.world.get_contact_events()
|
|
80
|
+
for begin_event in events.begin_events():
|
|
81
|
+
last_projectile_pos = self.projectile_body.position
|
|
82
|
+
self.projectile_body.destroy()
|
|
83
|
+
self.projectile_body = None
|
|
84
|
+
self.projectile_pos = b2d.Vec2(self.projectile_anchor_pos)
|
|
85
|
+
self.projectile_state = "wait_for_movement"
|
|
86
|
+
# create an explosion
|
|
87
|
+
self.world.explode(position=last_projectile_pos, radius=5, impulse_per_length=5)
|
|
88
|
+
break
|
|
89
|
+
|
|
90
|
+
if self.projectile_body is not None:
|
|
91
|
+
# check if projectile is out of bounds
|
|
92
|
+
if (
|
|
93
|
+
self.projectile_body.position.y < -50
|
|
94
|
+
or self.projectile_body.position.x > self.floor_size + 100
|
|
95
|
+
):
|
|
96
|
+
self.projectile_body.destroy()
|
|
97
|
+
self.projectile_body = None
|
|
98
|
+
self.projectile_pos = b2d.Vec2(self.projectile_anchor_pos)
|
|
99
|
+
self.projectile_state = "wait_for_movement"
|
|
100
|
+
|
|
101
|
+
def post_update(self, dt):
|
|
102
|
+
pass
|
|
103
|
+
|
|
104
|
+
def aabb(self):
|
|
105
|
+
return b2d.aabb(
|
|
106
|
+
lower_bound=(-50, -10),
|
|
107
|
+
upper_bound=(35, 60),
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
if __name__ == "__main__":
|
|
112
|
+
AngryShapes.run()
|
pyb2d3-0.7.4/examples/pyb2d3_samples/billard.py → pyb2d3-0.9.0/examples/pyb2d3_samples/billiard.py
RENAMED
|
@@ -28,7 +28,7 @@ class GameState(enum.Enum):
|
|
|
28
28
|
WAITING_FOR_BALLS_TO_REST = enum.auto()
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
class
|
|
31
|
+
class Billiard(SampleBase):
|
|
32
32
|
def __init__(self, frontend, settings):
|
|
33
33
|
super().__init__(frontend, settings.set_gravity((0, 0)))
|
|
34
34
|
|
|
@@ -300,4 +300,4 @@ class Billard(SampleBase):
|
|
|
300
300
|
|
|
301
301
|
|
|
302
302
|
if __name__ == "__main__":
|
|
303
|
-
|
|
303
|
+
Billiard.run()
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from .ragdoll import Ragdoll # noqa: F401
|
|
2
|
+
from .text import create_boxes_from_text # noqa: F401
|
|
3
|
+
from .math import truncated_vector_diff, interpolate_color # noqa: F401
|
|
4
|
+
|
|
5
|
+
import pyb2d3 as b2d
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def stacked_blocks(world, num_blocks_base, box_shape, start):
|
|
9
|
+
start_x = start[0]
|
|
10
|
+
start_y = start[1]
|
|
11
|
+
distance_x = box_shape[1]
|
|
12
|
+
|
|
13
|
+
for outer in range(num_blocks_base):
|
|
14
|
+
start_y_inner = start_y + outer * (box_shape[1] + box_shape[0])
|
|
15
|
+
for i in range(num_blocks_base - outer):
|
|
16
|
+
x = start_x + (i + outer / 2) * distance_x
|
|
17
|
+
y = start_y_inner + box_shape[1] / 2
|
|
18
|
+
body = world.create_dynamic_body(position=(x, y))
|
|
19
|
+
body.create_shape(
|
|
20
|
+
b2d.shape_def(density=1, material=b2d.surface_material(restitution=0.5)),
|
|
21
|
+
b2d.box(hx=box_shape[0] / 2, hy=box_shape[1] / 2),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
if i + 1 < num_blocks_base - outer:
|
|
25
|
+
# create a second block on top
|
|
26
|
+
body = world.create_dynamic_body(
|
|
27
|
+
position=(
|
|
28
|
+
x + box_shape[1] / 2,
|
|
29
|
+
start_y_inner + box_shape[1] + box_shape[0] / 2,
|
|
30
|
+
)
|
|
31
|
+
)
|
|
32
|
+
body.create_shape(
|
|
33
|
+
b2d.shape_def(density=1, material=b2d.surface_material(restitution=0.5)),
|
|
34
|
+
b2d.box(hx=box_shape[1] / 2, hy=box_shape[0] / 2),
|
|
35
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def truncated_vector_diff(vec_a, vec_b, max_length):
|
|
5
|
+
"""Returns the vector difference vec_b - vec_a, truncated to max_length."""
|
|
6
|
+
|
|
7
|
+
diff = np.array(vec_b) - np.array(vec_a)
|
|
8
|
+
length = np.linalg.norm(diff)
|
|
9
|
+
if length > max_length:
|
|
10
|
+
diff = (diff / length) * max_length
|
|
11
|
+
return diff
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def interpolate_color(color_a, color_b, t):
|
|
15
|
+
"""Interpolate between two colors given as (r, g, b) tuples with t in [0, 1]."""
|
|
16
|
+
return (
|
|
17
|
+
int(color_a[0] + (color_b[0] - color_a[0]) * t),
|
|
18
|
+
int(color_a[1] + (color_b[1] - color_a[1]) * t),
|
|
19
|
+
int(color_a[2] + (color_b[2] - color_a[2]) * t),
|
|
20
|
+
)
|