Skip to content

Commit 929f95e

Browse files
committed
New shorthand to get bodies from a Arbiter. #279
1 parent e1682c2 commit 929f95e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Changelog
1818
Added default do_nothing and always_collide callback functions to the CollisionHandler, so that its clear how to reset and align with other callbacks.
1919
If in old code you did handler.begin = None, you should now instead to handler.begin = CollisionHandler.always_collide etc.
2020
21+
New feature: Arbiter.bodies shorthand to ge the shapes' bodies in the Arbiter
2122
Changed type of PointQueryInfo.shape, SegmentQueryInfo.shape and ShapeQueryInfo.shape to not be Optional, they will always have a shape.
2223
New feature: ShapeFilter.rejects_collision()
2324
New feature: Added Vec2d.polar_tuple

pymunk/arbiter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
if TYPE_CHECKING:
77
from .space import Space
8+
from .body import Body
89

910
from ._chipmunk_cffi import ffi, lib
1011
from .contact_point_set import ContactPointSet
@@ -69,6 +70,24 @@ def contact_point_set(self, point_set: ContactPointSet) -> None:
6970

7071
lib.cpArbiterSetContactPointSet(self._arbiter, ffi.addressof(_set))
7172

73+
@property
74+
def bodies(self) -> Tuple["Body", "Body"]:
75+
"""The the bodies in the order their corresponding shapes were defined
76+
in the collision handler associated with this arbiter.
77+
78+
This is a shorthand to get the bodes::
79+
80+
arb.bodies == arb.shapes[0].body, arb.shapes[1].body .
81+
"""
82+
a, b = self.shapes
83+
assert (
84+
a.body != None
85+
), "Shape should have a body. Could be a bug in Pymunk, please report"
86+
assert (
87+
b.body != None
88+
), "Shape should have a body. Could be a bug in Pymunk, please report"
89+
return a.body, b.body
90+
7291
@property
7392
def shapes(self) -> Tuple["Shape", "Shape"]:
7493
"""Get the shapes in the order that they were defined in the

pymunk/tests/test_arbiter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def separate2(arb: p.Arbiter, space: p.Space, data: Any) -> None:
300300

301301
self.assertTrue(self.called2)
302302

303-
def testShapes(self) -> None:
303+
def testShapesAndBodies(self) -> None:
304304
s = p.Space()
305305
s.gravity = 0, -100
306306

@@ -324,6 +324,8 @@ def pre_solve(arb: p.Arbiter, space: p.Space, data: Any) -> bool:
324324
self.assertEqual(len(arb.shapes), 2)
325325
self.assertEqual(arb.shapes[0], c1)
326326
self.assertEqual(arb.shapes[1], c2)
327+
self.assertEqual(arb.bodies[0], arb.shapes[0].body)
328+
self.assertEqual(arb.bodies[1], arb.shapes[1].body)
327329
return True
328330

329331
s.add_collision_handler(1, 2).pre_solve = pre_solve

0 commit comments

Comments
 (0)