@@ -133,64 +133,6 @@ def testAddRemove(self) -> None:
133
133
self .assertEqual (s .bodies , [b ])
134
134
self .assertEqual (s .shapes , [c2 ])
135
135
136
- def testAddRemoveInStep (self ) -> None :
137
- s = p .Space ()
138
-
139
- b1 = p .Body (1 , 2 )
140
- c1 = p .Circle (b1 , 2 )
141
-
142
- b2 = p .Body (1 , 2 )
143
- c2 = p .Circle (b2 , 2 )
144
-
145
- s .add (b1 , b2 , c1 , c2 )
146
-
147
- b = p .Body (1 , 2 )
148
- c = p .Circle (b , 2 )
149
-
150
- def pre_solve_add (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
151
- space .add (b , c )
152
- space .add (c , b )
153
- self .assertTrue (b not in s .bodies )
154
- self .assertTrue (c not in s .shapes )
155
- return True
156
-
157
- def pre_solve_remove (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
158
- space .remove (b , c )
159
- space .remove (c , b )
160
- self .assertTrue (b in s .bodies )
161
- self .assertTrue (c in s .shapes )
162
- return True
163
-
164
- s .add_collision_handler (0 , 0 ).pre_solve = pre_solve_add
165
-
166
- s .step (0.1 )
167
- return
168
- self .assertTrue (b in s .bodies )
169
- self .assertTrue (c in s .shapes )
170
-
171
- s .add_collision_handler (0 , 0 ).pre_solve = pre_solve_remove
172
-
173
- s .step (0.1 )
174
-
175
- self .assertTrue (b not in s .bodies )
176
- self .assertTrue (c not in s .shapes )
177
-
178
- def testRemoveInStep (self ) -> None :
179
- self ._setUp ()
180
- s = self .s
181
-
182
- def pre_solve (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
183
- space .remove (* arb .shapes )
184
- return True
185
-
186
- s .add_collision_handler (0 , 0 ).pre_solve = pre_solve
187
-
188
- s .step (0.1 )
189
-
190
- self .assertTrue (self .s1 not in s .shapes )
191
- self .assertTrue (self .s2 not in s .shapes )
192
- self ._tearDown ()
193
-
194
136
def testAddShapeAsserts (self ) -> None :
195
137
s1 = p .Space ()
196
138
s2 = p .Space ()
@@ -705,6 +647,112 @@ def separate(*_: Any) -> None:
705
647
s .step (1 )
706
648
s .remove (c1 )
707
649
650
+ def testCollisionHandlerRemoveAfterSeparate (self ) -> None :
651
+ # In this test the separate must happen before post_solve in the same step()
652
+ print ()
653
+ space = p .Space ()
654
+
655
+ shape1 = p .Circle (space .static_body , 1 )
656
+ shape1 .collision_type = 1
657
+
658
+ body2 = p .Body ()
659
+ shape2 = p .Circle (body2 , 1 )
660
+ shape2 .density = 1
661
+ shape2 .collision_type = 2
662
+
663
+ body3 = p .Body (body_type = p .Body .KINEMATIC )
664
+ shape3 = p .Circle (body3 , 1 )
665
+ shape3 .collision_type = 3
666
+
667
+ space .add (shape1 , body2 , shape2 , shape3 , body3 )
668
+ print ("START" , shape1 , shape2 , shape3 )
669
+
670
+ def separate (arbiter : p .Arbiter , space : p .Space , data ):
671
+ print ("SEP" , arbiter .shapes )
672
+ self .separate_occurred = True
673
+
674
+ def post_solve (arbiter : p .Arbiter , space : p .Space , data ):
675
+ print ("POST" , arbiter .shapes )
676
+ if self .separate_occurred :
677
+ print ("POST REMOVE" , arbiter .shapes )
678
+ space .remove (* arbiter .shapes )
679
+
680
+ space .add_collision_handler (1 , 2 ).post_solve = post_solve
681
+ space .add_collision_handler (3 , 2 ).separate = separate
682
+
683
+ print (1 )
684
+ self .separate_occurred = False
685
+ space .step (1 )
686
+ print (2 )
687
+ body3 .position = 10 , 0
688
+ # self.assertEqual(len(space.shapes), 3)
689
+
690
+ self .separate_occurred = False
691
+ space .step (1 )
692
+ print (3 )
693
+ space .remove (shape3 )
694
+ # self.assertEqual(len(space.shapes), 1)
695
+ print (4 )
696
+ # space.remove(shape3)
697
+
698
+ def testCollisionHandlerAddRemoveInStep (self ) -> None :
699
+ s = p .Space ()
700
+
701
+ b1 = p .Body (1 , 2 )
702
+ c1 = p .Circle (b1 , 2 )
703
+
704
+ b2 = p .Body (1 , 2 )
705
+ c2 = p .Circle (b2 , 2 )
706
+
707
+ s .add (b1 , b2 , c1 , c2 )
708
+
709
+ b = p .Body (1 , 2 )
710
+ c = p .Circle (b , 2 )
711
+
712
+ def pre_solve_add (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
713
+ space .add (b , c )
714
+ space .add (c , b )
715
+ self .assertTrue (b not in s .bodies )
716
+ self .assertTrue (c not in s .shapes )
717
+ return True
718
+
719
+ def pre_solve_remove (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
720
+ space .remove (b , c )
721
+ space .remove (c , b )
722
+ self .assertTrue (b in s .bodies )
723
+ self .assertTrue (c in s .shapes )
724
+ return True
725
+
726
+ s .add_collision_handler (0 , 0 ).pre_solve = pre_solve_add
727
+
728
+ s .step (0.1 )
729
+ return
730
+ self .assertTrue (b in s .bodies )
731
+ self .assertTrue (c in s .shapes )
732
+
733
+ s .add_collision_handler (0 , 0 ).pre_solve = pre_solve_remove
734
+
735
+ s .step (0.1 )
736
+
737
+ self .assertTrue (b not in s .bodies )
738
+ self .assertTrue (c not in s .shapes )
739
+
740
+ def testCollisionHandlerRemoveInStep (self ) -> None :
741
+ self ._setUp ()
742
+ s = self .s
743
+
744
+ def pre_solve (arb : p .Arbiter , space : p .Space , data : Any ) -> bool :
745
+ space .remove (* arb .shapes )
746
+ return True
747
+
748
+ s .add_collision_handler (0 , 0 ).pre_solve = pre_solve
749
+
750
+ s .step (0.1 )
751
+
752
+ self .assertTrue (self .s1 not in s .shapes )
753
+ self .assertTrue (self .s2 not in s .shapes )
754
+ self ._tearDown ()
755
+
708
756
def testCollisionHandlerKeyOrder (self ) -> None :
709
757
s = p .Space ()
710
758
h1 = s .add_collision_handler (1 , 2 )
@@ -960,7 +1008,7 @@ def _testCopyMethod(self, copy_func: Callable[[Space], Space]) -> None:
960
1008
961
1009
def testPickleCachedArbiters (self ) -> None :
962
1010
s = p .Space ()
963
-
1011
+
964
1012
b1 = p .Body ()
965
1013
b2 = p .Body ()
966
1014
@@ -970,32 +1018,31 @@ def testPickleCachedArbiters(self) -> None:
970
1018
c1 .mass = 1
971
1019
c2 .mass = 1
972
1020
973
- b2 .position = 1 ,2
974
- s .add (c1 ,c2 , b1 , b2 )
975
-
1021
+ b2 .position = 1 , 2
1022
+ s .add (c1 , c2 , b1 , b2 )
1023
+
976
1024
s .step (0.1 )
977
1025
# print("\nOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO")
978
1026
s_copy = s .copy ()
979
-
1027
+
980
1028
# a1 = [p.arbiter._arbiter_to_dict(_arb, s) for _arb in s._get_arbiters()]
981
1029
# a2 = [p.arbiter._arbiter_to_dict(_arb, s_copy) for _arb in s_copy._get_arbiters()]
982
-
1030
+
983
1031
# print("a1", a1)
984
1032
# print("a2", a2)
985
1033
# print("XXXX")
986
-
1034
+
987
1035
# print("s.bodies.position:")
988
1036
# print([b.position for b in s.bodies])
989
1037
# print("s_copy.bodies.position:")
990
1038
# print([b.position for b in s_copy.bodies])
991
1039
992
-
993
1040
s .step (0.1 )
994
1041
s_copy .step (0.1 )
995
-
1042
+
996
1043
# a1 = [p.arbiter._arbiter_to_dict(_arb, s) for _arb in s._get_arbiters()]
997
1044
# a2 = [p.arbiter._arbiter_to_dict(_arb, s_copy) for _arb in s_copy._get_arbiters()]
998
-
1045
+
999
1046
# print("a1", a1)
1000
1047
# print("a2", a2)
1001
1048
# print("XXXX")
@@ -1009,7 +1056,7 @@ def testPickleCachedArbiters(self) -> None:
1009
1056
1010
1057
# a1 = [p.arbiter._arbiter_to_dict(_arb, s) for _arb in s._get_arbiters()]
1011
1058
# a2 = [p.arbiter._arbiter_to_dict(_arb, s_copy) for _arb in s_copy._get_arbiters()]
1012
-
1059
+
1013
1060
# print("a1", a1)
1014
1061
# print("a2", a2)
1015
1062
# print("XXXX")
@@ -1019,14 +1066,12 @@ def testPickleCachedArbiters(self) -> None:
1019
1066
# print("s_copy.bodies.position:")
1020
1067
# print([b.position for b in s_copy.bodies])
1021
1068
1022
- # TODO: to assert that everything is working as it should all
1069
+ # TODO: to assert that everything is working as it should all
1023
1070
# properties on the cached the arbiters should be asserted.
1024
1071
1025
-
1026
1072
self .assertAlmostEqual (s .bodies [0 ].position .x , s_copy .bodies [0 ].position .x )
1027
1073
self .assertAlmostEqual (s .bodies [0 ].position .y , s_copy .bodies [0 ].position .y )
1028
1074
1029
1075
1030
-
1031
1076
def f1 (* args : Any , ** kwargs : Any ) -> None :
1032
1077
pass
0 commit comments