Skip to content

Commit e1682c2

Browse files
committed
Change ContactPointSet.points to tuple #279
1 parent cbe41d9 commit e1682c2

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Changelog
1212
Changed body.constraints to return a KeysView of the Constraints attached to the body. Note that its still weak references to the Constraints.
1313
Reversed the dependency between bodies and shapes. Now the Body owns the connection, and the Shape only keeps a weak ref to the Body. That means that if you remove a Body, then any shapes not referenced anywhere else will also be removed.
1414
Changed body.shapes to return a KeysView instead of a set of the shapes.
15-
Changed Space.segment_query to return None in case the query did not hit the shape.
15+
Changed Shape.segment_query to return None in case the query did not hit the shape.
16+
Changed ContactPointSet.points to be a tuple and not list to make it clear its length is fixed.
1617
1718
Added default do_nothing and always_collide callback functions to the CollisionHandler, so that its clear how to reset and align with other callbacks.
1819
If in old code you did handler.begin = None, you should now instead to handler.begin = CollisionHandler.always_collide etc.

pymunk/contact_point_set.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__docformat__ = "reStructuredText"
22

3-
from typing import TYPE_CHECKING, List
3+
from typing import TYPE_CHECKING, Tuple
44

55
if TYPE_CHECKING:
66
from ._chipmunk_cffi import ffi
@@ -31,8 +31,6 @@ def __init__(
3131
point_b: Vec2d,
3232
distance: float,
3333
) -> None:
34-
assert len(point_a) == 2
35-
assert len(point_b) == 2
3634
self.point_a = point_a
3735
self.point_b = point_b
3836
self.distance = distance
@@ -52,12 +50,11 @@ class ContactPointSet(object):
5250
"""
5351

5452
normal: Vec2d
55-
points: List[ContactPoint]
53+
points: Tuple[ContactPoint, ...]
5654

5755
__slots__ = ("normal", "points")
5856

59-
def __init__(self, normal: Vec2d, points: List[ContactPoint]) -> None:
60-
assert len(normal) == 2
57+
def __init__(self, normal: Vec2d, points: Tuple[ContactPoint, ...]) -> None:
6158
self.normal = normal
6259
self.points = points
6360

@@ -68,14 +65,21 @@ def __repr__(self) -> str:
6865
def _from_cp(cls, _points: "ffi.CData") -> "ContactPointSet":
6966
normal = Vec2d(_points.normal.x, _points.normal.y)
7067

71-
points = []
72-
for i in range(_points.count):
73-
_p = _points.points[i]
74-
p = ContactPoint(
75-
Vec2d(_p.pointA.x, _p.pointA.y),
76-
Vec2d(_p.pointB.x, _p.pointB.y),
77-
_p.distance,
78-
)
79-
points.append(p)
80-
81-
return cls(normal, points)
68+
assert _points.count in (1, 2), "This is likely a bug in Pymunk, please report."
69+
70+
_p1 = _points.points[0]
71+
p1 = ContactPoint(
72+
Vec2d(_p1.pointA.x, _p1.pointA.y),
73+
Vec2d(_p1.pointB.x, _p1.pointB.y),
74+
_p1.distance,
75+
)
76+
if _points.count == 1:
77+
return cls(normal, (p1,))
78+
79+
_p2 = _points.points[1]
80+
p2 = ContactPoint(
81+
Vec2d(_p2.pointA.x, _p2.pointA.y),
82+
Vec2d(_p2.pointB.x, _p2.pointB.y),
83+
_p2.distance,
84+
)
85+
return cls(normal, (p1, p2))

pymunk/tests/test_arbiter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def pre_solve(arb: p.Arbiter, space: p.Space, data: Any) -> bool:
137137
self.assertAlmostEqual(p1.distance, -11)
138138

139139
# check for length of points
140-
ps2.points = []
140+
ps2.points = ()
141141

142142
def f() -> None:
143143
arb.contact_point_set = ps2

pymunk/tests/test_space.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,10 +1045,10 @@ def _testCopyMethod(self, copy_func: Callable[[Space], Space]) -> None:
10451045
s.add(j1, j2)
10461046

10471047
h = s.add_default_collision_handler()
1048-
h.begin = f1
1048+
h.begin = f2
10491049

10501050
h = s.add_wildcard_collision_handler(1)
1051-
h.pre_solve = f1
1051+
h.pre_solve = f2
10521052

10531053
h = s.add_collision_handler(1, 2)
10541054
h.post_solve = f1
@@ -1192,3 +1192,7 @@ def testDeleteSpaceWithObjects(self) -> None:
11921192

11931193
def f1(*args: Any, **kwargs: Any) -> None:
11941194
pass
1195+
1196+
1197+
def f2(*args: Any, **kwargs: Any) -> bool:
1198+
return True

0 commit comments

Comments
 (0)