Skip to content

Commit

Permalink
Make bodies and constraints in the space ordered to (partially) fix p…
Browse files Browse the repository at this point in the history
…ickle/copy #181
  • Loading branch information
viblo committed Jun 25, 2020
1 parent 1abd911 commit 176a4f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions TODO.txt
Expand Up @@ -17,7 +17,8 @@ v5.x
- Type annotations?
- update example code to be less dependent on the flipy function for pygame. instead flip the gravity.
- Update docs with collision chapter. ShapeFilter, collision callbacks and such
- use env variables for pymunkoptions, like pygame https://stackoverflow.com/questions/56258942/is-there-an-environment-variable-to-disable-pymunk-chipmunk-from-printing-loadi
- use env variables for pymunkoptions, like pygame https://stackoverflow.com/questions/56258942/is-there-an-environment-variable-to-disable-pymunk-chipmunk-from-printing-loadi
- make running tests with python -m pymunk.tests, and examples with python -m pymunk.examples... somehow. See pybox2d for inspiration

v6+ (all potentially breaking changes)
---
Expand All @@ -34,4 +35,5 @@ v6+ (all potentially breaking changes)
- Make add_collision_handler(a,b) and add_collision_handler(b,a) return the
same handler. Fixes https://github.com/viblo/pymunk/issues/132
- make default of positive_y_is_up False
- use sphinx_autodoc_typehints and type hints when python 2 support is dropped
- use sphinx_autodoc_typehints and type hints when python 2 support is dropped
- drop support for python 2
12 changes: 6 additions & 6 deletions pymunk/space.py
Expand Up @@ -71,9 +71,9 @@ def __init__(self, threaded=False):
self._removed_shapes = {}

self._shapes = {}
self._bodies = set()
self._bodies = {}
self._static_body = None
self._constraints = set()
self._constraints = {}

self._in_step = False
self._add_later = set()
Expand Down Expand Up @@ -307,13 +307,13 @@ def _add_body(self, body):
"""Adds a body to the space"""
assert body not in self._bodies, "body already added to space"
body._space = weakref.proxy(self)
self._bodies.add(body)
self._bodies[body] = None
cp.cpSpaceAddBody(self._space, body._body)

def _add_constraint(self, constraint):
"""Adds a constraint to the space"""
assert constraint not in self._constraints, "constraint already added to space"
self._constraints.add(constraint)
self._constraints[constraint] = None
cp.cpSpaceAddConstraint(self._space, constraint._constraint)

def _remove_shape(self, shape):
Expand All @@ -324,11 +324,11 @@ def _remove_shape(self, shape):
def _remove_body(self, body):
"""Removes a body from the space"""
body._space = None
self._bodies.remove(body)
del self._bodies[body]
cp.cpSpaceRemoveBody(self._space, body._body)
def _remove_constraint(self, constraint):
"""Removes a constraint from the space"""
self._constraints.remove(constraint)
del self._constraints[constraint]
cp.cpSpaceRemoveConstraint(self._space, constraint._constraint)

def reindex_shape(self, shape):
Expand Down
14 changes: 9 additions & 5 deletions pymunk/tests/test_space.py
Expand Up @@ -754,10 +754,12 @@ def _testCopyMethod(self, copy_func):

b1 = p.Body(1,2)
b2 = p.Body(3,4)
b3 = p.Body(5,6)
c1 = p.Circle(b1, 7)
c2 = p.Circle(b1, 8)
c3 = p.Circle(s.static_body, 9)
s.add(b1,b2, c1, c2, c3)
c3 = p.Circle(b2, 9)
c4 = p.Circle(s.static_body, 10)
s.add(b1, b2, b3, c1, c2, c3, c4)

s.static_body.custom = "x"

Expand All @@ -779,6 +781,7 @@ def _testCopyMethod(self, copy_func):

s2 = copy_func(s)

# Assert properties
self.assertEqual(s.threaded, s2.threaded)
self.assertEqual(s.iterations, s2.iterations)
self.assertEqual(s.gravity, s2.gravity)
Expand All @@ -789,13 +792,14 @@ def _testCopyMethod(self, copy_func):
self.assertEqual(s.collision_bias, s2.collision_bias)
self.assertEqual(s.collision_persistence, s2.collision_persistence)
self.assertEqual(s.threads, s2.threads)

self.assertEqual(sorted([c.radius for c in s2.shapes]), [7,8,9])
self.assertEqual(sorted([b.mass for b in s2.bodies]), [1,3])
# Assert shapes, bodies and constriants
self.assertEqual([c.radius for c in s2.shapes], [7,8,9, 10])
self.assertEqual([b.mass for b in s2.bodies], [1, 3, 5])
self.assertEqual(s.static_body.custom, s2.static_body.custom)
ja = [j.a for j in s2.constraints]
self.assertIn(s2.static_body, ja)

# Assert collision handlers
h2 = s2.add_default_collision_handler()
self.assertIsNotNone(h2.begin)
self.assertIsNone(h2.pre_solve)
Expand Down

0 comments on commit 176a4f4

Please sign in to comment.