Search code examples
pythonpygamegame-physicsphysicspymunk

How to make a Static Rectangle shape in pymunk?


My question is pretty straightforward I just wanna know a way to make a static body, which is a rectangle using pymunk. I tried reading the docs and found out about Segments, but couldn't really understand it. So any kind of help is very much appriciated !


Solution

  • Either you can use the static body already attached to the Space, or you make a new one. To create a rectangle, either you provide the corners to the Poly constructor (as shown below), or you use the shorthand Poly.create_box method.

    space = pymunk.Space()
    rectangle = pymunk.Poly(space.static_body, [(10,10),(20,10),(20,15),(10,15)])
    space.add(rectangle)
    

    Note that static bodies are not supposed to be moved after they have been added to space. So if you need to move the rectangle, you can use a kinematic body instead.