Python Forum
updating collision handler for pymunk 5.3.2
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
updating collision handler for pymunk 5.3.2
#1
Trying to update this older code --
space.set_default_collision_handler(coll_handler)
Error:
Traceback (most recent call last): File "/code/pyplatformer/game.py", line 2, in <module> from engine import * File "\code\pyplatformer\engine\__init__.py", line 12, in <module> from .physics import BoxCollider, SphereCollider, Physics, Rigidbody File "\code\pyplatformer\engine\physics.py", line 20, in <module> space.set_default_collision_handler(coll_handler) AttributeError: 'Space' object has no attribute 'set_default_collision_handler' Process finished with exit code 1
with an equivalent that works with pymunk 5.3.2

This is the first step in our assignment for updating this pymunk platformer game, but I haven't quite been able to figure out what to do. Since there are a few points in the code to update, I'm not sure if a change in error message makes this step resolved or not.

I have been looking into examples and documentation:
pymunk 5.3.2 documentation for add_collision_handler http://www.pymunk.org/en/latest/pymunk.h...munk.Space
pymunk 5.3.2 platformer game example with add_collision_handler https://github.com/viblo/pymunk/blob/mas...tformer.py
pymunk 5.3.2 breakout game example with add_collision_handler https://github.com/viblo/pymunk/blob/mas...reakout.py

I attempted replacing the old code with this, from the platformer game example ^
space.add_collision_handler(1, 2).begin = coll_handler
Error:
Traceback (most recent call last): File "/code/pyplatformer/game.py", line 154, in <module> game.mainloop() File "\code\pyplatformer\engine\__init__.py", line 118, in mainloop self.update(dt) File "\code\pyplatformer\engine\__init__.py", line 142, in update gameobject.update(dt) File "\code\pyplatformer\engine\__init__.py", line 89, in update component.update(dt) File "/code/pyplatformer/game.py", line 112, in update self.gameobject.move(d * 5 * dt, 0) File "\code\pyplatformer\engine\__init__.py", line 59, in move self._body.apply_impulse((x, y)) AttributeError: 'Body' object has no attribute 'apply_impulse' Process finished with exit code 1
Does this look correct, or is there another way to solve this?



This is the physics.py file where this portion of the code is contained
import pymunk

from .components import Component
 
 
__all__ = ['BoxCollider', 'SphereCollider', 'Physics', 'Rigidbody']
 
 
def coll_handler(_, arbiter):
   if len(arbiter.shapes) == 2:
       obj1 = arbiter.shapes[0].gameobject
       obj2 = arbiter.shapes[1].gameobject
 
       obj1.collide(obj2, arbiter.contacts)
       obj2.collide(obj1, arbiter.contacts)
       print(obj1)
       print(obj2)
   return True
 
space = pymunk.Space()
space.gravity = 0, -10
space.set_default_collision_handler(coll_handler) ##### << OLD CODE TO CHANGE
 
##########################################################################
 
class Rigidbody(Component):
    __slots__ = ['mass', 'is_static']
 
    def __init__(self, mass=1, is_static=True):
        self.mass = mass
        self.is_static = is_static
 
    def start(self):
        if not self.is_static:
            # Replace the static body
            pos = self.gameobject._body.position
            body = pymunk.Body(self.mass, 1666)
            body.position = pos
            self.gameobject._body = body
 
    def add_shape_to_space(self, shape):
        shape.collision_type = 1
        self.gameobject._shape = shape
        shape.gameobject = self.gameobject
        if self.is_static:
            space.add(shape)
        else:
            space.add(self.gameobject._body, shape)
 
 
class BoxCollider(Rigidbody):
    __slots__ = ['size']
 
    def __init__(self, width, height, mass=1, is_static=True):
        super(BoxCollider, self).__init__(mass, is_static)
        self.size = width, height
 
    def start(self):
        super(BoxCollider, self).start()
        body = self.gameobject._body
        shape = pymunk.Poly.create_box(body, self.size)
        self.add_shape_to_space(shape)
 
 
class SphereCollider(Rigidbody):
    __slots__ = ['radius']
 
    def __init__(self, radius, mass=1, is_static=True):
        super(SphereCollider, self).__init__(mass, is_static)
        self.radius = radius
 
    def start(self):
        super(SphereCollider, self).start()
        body = self.gameobject._body
        shape = pymunk.Circle(body, self.radius)
        self.add_shape_to_space(shape)
 
 
class Physics(object):
    @classmethod
    def step(cls, dt):
        space.step(dt)
 
    @classmethod
    def remove(cls, body):
        space.remove(body)
Reply
#2
I could find add_default_collision_handler, but not set_default_collision_handler
Reply
#3
My instructor's version had either add_collision_handler or add_default_collision_handler, so I believe these will work in its place.

set_default_collision_handler, and add_collision_handler are also on the older documentation
http://www.pymunk.org/en/pymunk-4.0.0/pymunk.html

The set_default_collision_handler from 4.0.0 documentation seems to accept quite a few parameters versus the add_default_collision_handler from the 5.3.2 documentation however..
Reply
#4
Went to class today, and my instructor said that this version I came up with is correct, but something else needs to be changed regarding parameters -- possibly in the coll_handler, or in the def coll_handler? She hinted at something about 2 parameters (pymunk 4.0.0) versus 3 parameters (pymunk 5.3.2), and the order of them. I'm looking through documentation and examples right now..

correct code to replace set_default_collision_handler:
h = space.add_default_collision_handler()
h.begin = coll_handler
Error:
From cffi callback <function CollisionHandler._set_begin.<locals>.cf at 0x056F8228>: Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\site-packages\pymunk\collision_handler.py", line 64, in cf x = func(Arbiter(_arb, self._space), self._space, self._data) TypeError: coll_handler() takes 2 positional arguments but 3 were given From cffi callback <function CollisionHandler._set_begin.<locals>.cf at 0x056F8228>: Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\site-packages\pymunk\collision_handler.py", line 64, in cf x = func(Arbiter(_arb, self._space), self._space, self._data) TypeError: coll_handler() takes 2 positional arguments but 3 were given Traceback (most recent call last): File "/Chapter 6/code/pyplatformer/game.py", line 154, in <module> game.mainloop() File "\Chapter 6\code\pyplatformer\engine\__init__.py", line 118, in mainloop self.update(dt) File "\Chapter 6\code\pyplatformer\engine\__init__.py", line 142, in update gameobject.update(dt) File "\Chapter 6\code\pyplatformer\engine\__init__.py", line 89, in update component.update(dt) File "/Chapter 6/code/pyplatformer/game.py", line 112, in update self.gameobject.move(d * 5 * dt, 0) File "\Chapter 6\code\pyplatformer\engine\__init__.py", line 59, in move self._body.apply_impulse((x, y)) AttributeError: 'Body' object has no attribute 'apply_impulse'
4.0.0 set_default_collision_handler documentation http://www.pymunk.org/en/pymunk-4.0.0/pymunk.html
5.3.2 add_default_collision_handler documentation http://www.pymunk.org/en/latest/pymunk.h...on_handler
5.3.2 add_default_collision_handler example http://www.pymunk.org/en/latest/_modules...on_handler
Reply
#5
You may find some examples here: http://nullege.com
Reply
#6
Thank you - I have resolved this problem. The solution was to change to match 5.3.2
def coll_handler(_, arbiter):


To do this, I looked in the Traceback to collision_handler.py. There, it shows the collision handler requiring input of 3 parameters:
def coll_handler(arbiter, space, _):
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exception handler problem part 2 Leo 3 1,557 Feb-27-2022, 12:33 AM
Last Post: Larz60+
  Exception handler problem Leo 10 2,872 Feb-24-2022, 08:21 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020