Oct-26-2017, 05:52 AM
Trying to update this older code --
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 ^
This is the physics.py file where this portion of the code is contained
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.2This 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)