pymunk updated shape filter usage
<p>I am trying to detect the first shape along a segment starting at my player's position, but I do not want to detect the player.</p>

<p>In a previous version of pymunk, the pymunk.Space.segment_query_first function accepted an integer as the shape_filter and it only detected shapes within a group of that integer. That worked perfectly, but now it accepts a list or dictionary instead. I have tried putting the integer into a list and that didn't work. I have no idea what it wants with a dictionary. I have tried everything that I can think of. Nothing seems to stop it from detecting my player. The documentation is not helpful at all. Thanks in advance.</p>

<p>Yes the shape filter have become more powerful in pymunk 5 (and as a result also a little bit more complicated). The shape filter is supposed to be a <code>ShapeFilter</code> object (but . See the api docs <a href="http://www.pymunk.org/en/latest/pymunk.html#pymunk.ShapeFilter" rel="nofollow">http://www.pymunk.org/en/latest/pymunk.html#pymunk.ShapeFilter</a> for details on this filter object.</p>

<p>The <code>ShapeFilter</code> has 3 properties: <code>categories</code>, <code>mask</code>, and <code>group</code>. In your case I think you want to put the player in a separate category, and mask it out from the filter query. (By default the shape filter object matches all categories and doesnt mask out anything).</p>

<p>So, you want to do something like in this example:</p>

<pre><code>&gt;&gt;&gt; import pymunk
&gt;&gt;&gt; s = pymunk.Space()
&gt;&gt;&gt; player_b = pymunk.Body(1,1)
&gt;&gt;&gt; player_c = pymunk.Circle(player_b, 10)
&gt;&gt;&gt; s.add(player_b, player_c)
&gt;&gt;&gt;
&gt;&gt;&gt; player_c.filter = pymunk.ShapeFilter(categories=0x1)
&gt;&gt;&gt; s.point_query_nearest((0,0), 0, pymunk.ShapeFilter())
PointQueryInfo(shape=&lt;pymunk.shapes.Circle object at 0x03C07F30&gt;, point=Vec2d(nan, nan), distance=-10.0, gradient=Vec2d(0.0, 1.0))
&gt;&gt;&gt; s.point_query_nearest((0,0), 0, pymunk.ShapeFilter(mask=pymunk.ShapeFilter.ALL_MASKS ^ 0x1))
&gt;&gt;&gt;
&gt;&gt;&gt; other_b = pymunk.Body(1,1)
&gt;&gt;&gt; other_c = pymunk.Circle(other_b, 10)
&gt;&gt;&gt; s.add(other_b, other_c)
&gt;&gt;&gt;
&gt;&gt;&gt; s.point_query_nearest((0,0), 0, pymunk.ShapeFilter(mask=pymunk.ShapeFilter.ALL_MASKS ^ 0x1))
PointQueryInfo(shape=&lt;pymunk.shapes.Circle object at 0x03C070F0&gt;, point=Vec2d(nan, nan), distance=-10.0, gradient=Vec2d(0.0, 1.0))
</code></pre>

<p>There are tests in the <code>test_space.py</code> file that tests different combinations of the shape filter which might help explain how they work: <a href="https://github.com/viblo/pymunk/blob/master/tests/test_space.py#L175" rel="nofollow">https://github.com/viblo/pymunk/blob/master/tests/test_space.py#L175</a></p>

python filter shape segment pymunk