@@ -42,10 +42,10 @@ def __getitem__(self, key: Union[None, int, tuple[int, int]]) -> CollisionHandle
4242 if key in self ._handlers :
4343 return self ._handlers [key ]
4444 if key == None :
45- self ._handlers [None ] = self .space .add_global_collision_handler ( )
45+ self ._handlers [None ] = self .space .add_collision_handler ( None , None )
4646 return self ._handlers [None ]
4747 elif isinstance (key , int ):
48- self ._handlers [key ] = self .space .add_wildcard_collision_handler (key )
48+ self ._handlers [key ] = self .space .add_collision_handler (key , None )
4949 return self ._handlers [key ]
5050 elif isinstance (key , tuple ):
5151 assert isinstance (key , tuple )
@@ -626,11 +626,13 @@ def collision_handlers(
626626 return self .collision_handlers
627627
628628 def add_collision_handler (
629- self , collision_type_a : int , collision_type_b : int
629+ self , collision_type_a : Optional [ int ] , collision_type_b : Optional [ int ]
630630 ) -> CollisionHandler :
631631 """Return the :py:class:`CollisionHandler` for collisions between
632632 objects of type collision_type_a and collision_type_b.
633633
634+ Use None to indicate any collision_type.
635+
634636 Fill the desired collision callback functions, for details see the
635637 :py:class:`CollisionHandler` object.
636638
@@ -645,65 +647,34 @@ def add_collision_handler(
645647
646648 :rtype: :py:class:`CollisionHandler`
647649 """
648- key = min (collision_type_a , collision_type_b ), max (
649- collision_type_a , collision_type_b
650- )
650+ # key = min(collision_type_a, collision_type_b), max(
651+ # collision_type_a, collision_type_b
652+ # )
653+
654+ if collision_type_a == None and collision_type_b != None :
655+ collision_type_b , collision_type_a = collision_type_a , collision_type_b
656+
657+ key = collision_type_a , collision_type_b
651658 if key in self ._handlers :
652659 return self ._handlers [key ]
653660
661+ # if collision_type_a == None and collision_type_b == None:
662+ # return self.add_global_collision_handler()
663+ # CP_WILDCARD_COLLISION_TYPE
664+ wildcard = int (ffi .cast ("uintptr_t" , ~ 0 ))
665+ if collision_type_a == None :
666+ collision_type_a = wildcard
667+
668+ if collision_type_b == None :
669+ collision_type_b = wildcard
670+
654671 h = cp .cpSpaceAddCollisionHandler (
655672 self ._space , collision_type_a , collision_type_b
656673 )
657674 ch = CollisionHandler (h , self )
658675 self ._handlers [key ] = ch
659676 return ch
660677
661- def add_wildcard_collision_handler (self , collision_type_a : int ) -> CollisionHandler :
662- """Add a wildcard collision handler for given collision type.
663-
664- This handler will be used any time an object with this type collides
665- with another object, regardless of its type. A good example is a
666- projectile that should be destroyed the first time it hits anything.
667- There may be a specific collision handler and two wildcard handlers.
668- It's up to the specific handler to decide if and when to call the
669- wildcard handlers and what to do with their return values.
670-
671- When a new wildcard handler is created, the callbacks will all be
672- set to builtin callbacks that perform the default behavior. (accept
673- all collisions in :py:func:`~CollisionHandler.begin` and
674- :py:func:`~CollisionHandler.pre_solve`, or do nothing for
675- :py:func:`~CollisionHandler.post_solve` and
676- :py:func:`~CollisionHandler.separate`.
677-
678- :param int collision_type_a: Collision type
679- :rtype: :py:class:`CollisionHandler`
680- """
681-
682- if collision_type_a in self ._handlers :
683- return self ._handlers [collision_type_a ]
684-
685- h = cp .cpSpaceAddWildcardHandler (self ._space , collision_type_a )
686- ch = CollisionHandler (h , self )
687- self ._handlers [collision_type_a ] = ch
688- return ch
689-
690- def add_global_collision_handler (self ) -> CollisionHandler :
691- """Return a reference to the default collision handler or that is
692- used to process all collisions that don't have a more specific
693- handler.
694-
695- The default behavior for each of the callbacks is to call
696- the wildcard handlers, ANDing their return values together if
697- applicable.
698- """
699- if None in self ._handlers :
700- return self ._handlers [None ]
701-
702- _h = cp .cpSpaceAddGlobalCollisionHandler (self ._space )
703- h = CollisionHandler (_h , self )
704- self ._handlers [None ] = h
705- return h
706-
707678 def add_post_step_callback (
708679 self ,
709680 callback_function : Callable [
@@ -917,7 +888,7 @@ def bb_query(self, bb: "BB", shape_filter: ShapeFilter) -> list[Shape]:
917888 The filter is applied to the query and follows the same rules as the
918889 collision detection.
919890
920- Sensor shapes are included in the result
891+ Sensor shapes are included in the result
921892
922893 :param bb: Bounding box
923894 :param shape_filter: Shape filter
@@ -938,7 +909,7 @@ def bb_query(self, bb: "BB", shape_filter: ShapeFilter) -> list[Shape]:
938909 def shape_query (self , shape : Shape ) -> list [ShapeQueryInfo ]:
939910 """Query a space for any shapes overlapping the given shape
940911
941- Sensor shapes are included in the result
912+ Sensor shapes are included in the result
942913
943914 :param shape: Shape to query with
944915 :type shape: :py:class:`Circle`, :py:class:`Poly` or :py:class:`Segment`
@@ -1081,11 +1052,11 @@ def __setstate__(self, state: _State) -> None:
10811052 elif k == "_handlers" :
10821053 for k2 , hd in v :
10831054 if k2 == None :
1084- h = self .add_global_collision_handler ( )
1055+ h = self .add_collision_handler ( None , None )
10851056 elif isinstance (k2 , tuple ):
10861057 h = self .add_collision_handler (k2 [0 ], k2 [1 ])
10871058 else :
1088- h = self .add_wildcard_collision_handler (k2 )
1059+ h = self .add_collision_handler (k2 , None )
10891060 if "_begin" in hd :
10901061 h .begin = hd ["_begin" ]
10911062 if "_pre_solve" in hd :
0 commit comments