Home > Software engineering >  AssertionError: The shape's body must be added to the space before (or at the same time) as the
AssertionError: The shape's body must be added to the space before (or at the same time) as the

Time:11-08

I am trying to make a game, which includes polygons in pymunk, and to be able to do that in pymunk I decided to use many segments and draw it as a solid polygon. When I implement the code im getting an error

My code

class StaticPoly:
    def __init__(self, space, pos, elasticity=1, collision_type=None):
        self.bodies = []
        self.shapes = []
        self.pos = pos
        for i in list(range(0, len(self.pos) - 1)) [-1]:
            print(i)
            self.bodies.append(pymunk.Body(body_type=pymunk.Body.STATIC))
            self.shapes.append(pymunk.Segment(self.bodies[i], self.pos[i], self.pos[i   1], 1))
            self.shapes[i].elasticity = elasticity
            space.add(self.shapes[i])
            if collision_type:
                self.shapes[i].collision_type = collision_type

ERROR:

Traceback (most recent call last):
  File *path*, line 33, in <module>
    catch = StaticPoly(space, ((0, 100), (100, 101), (0, 0)))
  File *path again*, line 37, in __init__
    space.add(self.shapes[i])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymunk/space.py", line 401, in add
    self._add_shape(o)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymunk/space.py", line 441, in _add_shape
    assert (
AssertionError: The shape's body must be added to the space before (or at the same time) as the shape.

CodePudding user response:

StaticPoly.bodies contains pymunk.Bodys. These bodies must also be added to the space.

Reed the error message:

The shape's body must be added to the space before (or at the same time) as the shape.

Either before

space.add(self.bodies[i])
space.add(self.shapes[i])

or at the same time

space.add(self.bodies[i], self.shapes[i])
  • Related