@@ -132,9 +132,9 @@ def spacefree(cp_space: ffi.CData) -> None:
132
132
) # To prevent the gc to collect the callbacks.
133
133
134
134
self ._post_step_callbacks : Dict [Any , Callable [["Space" ], None ]] = {}
135
- self ._removed_shapes : Dict [int , Shape ] = {}
135
+ self ._removed_shapes : Dict [Shape , None ] = {}
136
136
137
- self ._shapes : Dict [int , Shape ] = {}
137
+ self ._shapes : Dict [Shape , None ] = {}
138
138
self ._bodies : Dict [Body , None ] = {}
139
139
self ._static_body : Optional [Body ] = None
140
140
self ._constraints : Dict [Constraint , None ] = {}
@@ -154,7 +154,7 @@ def shapes(self) -> List[Shape]:
154
154
155
155
(includes both static and non-static)
156
156
"""
157
- return list (self ._shapes . values () )
157
+ return list (self ._shapes )
158
158
159
159
@property
160
160
def bodies (self ) -> List [Body ]:
@@ -389,8 +389,7 @@ def remove(self, *objs: _AddableObjects) -> None:
389
389
390
390
def _add_shape (self , shape : "Shape" ) -> None :
391
391
"""Adds a shape to the space"""
392
- # print("addshape", self._space, shape)
393
- assert shape ._id not in self ._shapes , "Shape already added to space."
392
+ assert shape not in self ._shapes , "Shape already added to space."
394
393
assert (
395
394
shape .space == None
396
395
), "Shape already added to another space. A shape can only be in one space at a time."
@@ -400,7 +399,7 @@ def _add_shape(self, shape: "Shape") -> None:
400
399
), "The shape's body must be added to the space before (or at the same time) as the shape."
401
400
402
401
shape ._space = weakref .proxy (self )
403
- self ._shapes [shape . _id ] = shape
402
+ self ._shapes [shape ] = None
404
403
cp .cpSpaceAddShape (self ._space , shape ._shape )
405
404
406
405
def _add_body (self , body : "Body" ) -> None :
@@ -427,13 +426,13 @@ def _add_constraint(self, constraint: "Constraint") -> None:
427
426
428
427
def _remove_shape (self , shape : "Shape" ) -> None :
429
428
"""Removes a shape from the space"""
430
- assert shape . _id in self ._shapes , "shape not in space, already removed?"
431
- self ._removed_shapes [shape . _id ] = shape
429
+ assert shape in self ._shapes , "shape not in space, already removed?"
430
+ self ._removed_shapes [shape ] = None
432
431
shape ._space = None
433
432
# During GC at program exit sometimes the shape might already be removed. Then skip this step.
434
433
if cp .cpSpaceContainsShape (self ._space , shape ._shape ):
435
434
cp .cpSpaceRemoveShape (self ._space , shape ._shape )
436
- del self ._shapes [shape . _id ]
435
+ del self ._shapes [shape ]
437
436
438
437
def _remove_body (self , body : "Body" ) -> None :
439
438
"""Removes a body from the space"""
@@ -517,7 +516,7 @@ def use_spatial_hash(self, dim: float, count: int) -> None:
517
516
the shape to be inserted into many cells, setting it too low will
518
517
cause too many objects into the same hash slot.
519
518
520
- count is the suggested minimum number of cells in the hash table. If
519
+ count is the minimum number of cells in the hash table. If
521
520
there are too few cells, the spatial hash will return many false
522
521
positives. Too many cells will be hard on the cache and waste memory.
523
522
Setting count to ~10x the number of objects in the space is probably a
@@ -563,7 +562,7 @@ def step(self, dt: float) -> None:
563
562
cp .cpHastySpaceStep (self ._space , dt )
564
563
else :
565
564
cp .cpSpaceStep (self ._space , dt )
566
- self ._removed_shapes = {}
565
+ self ._removed_shapes . clear ()
567
566
finally :
568
567
self ._locked = False
569
568
self .add (* self ._add_later )
@@ -575,7 +574,7 @@ def step(self, dt: float) -> None:
575
574
for key in self ._post_step_callbacks :
576
575
self ._post_step_callbacks [key ](self )
577
576
578
- self ._post_step_callbacks = {}
577
+ self ._post_step_callbacks . clear ()
579
578
580
579
def add_collision_handler (
581
580
self , collision_type_a : int , collision_type_b : int
@@ -745,16 +744,7 @@ def point_query(
745
744
def _get_shape (self , _shape : Any ) -> Optional [Shape ]:
746
745
if not bool (_shape ):
747
746
return None
748
-
749
- shapeid = int (ffi .cast ("int" , cp .cpShapeGetUserData (_shape )))
750
- # return self._shapes[hashid_private]
751
-
752
- if shapeid in self ._shapes :
753
- return self ._shapes [shapeid ]
754
- elif shapeid in self ._removed_shapes :
755
- return self ._removed_shapes [shapeid ]
756
- else :
757
- return None
747
+ return ffi .from_handle (cp .cpShapeGetUserData (_shape ))
758
748
759
749
def point_query_nearest (
760
750
self , point : Tuple [float , float ], max_distance : float , shape_filter : ShapeFilter
0 commit comments