0

I want to check if a bullet has collided with my lines to keep the player from moving outside the screen.

Here's my lines

def create_lines():
    """Prevent objects from moving out of the screen"""
    static_lines = [
        pymunk.Segment(space.static_body, (0,0), (current_map.width,0), 0.0),
        pymunk.Segment(space.static_body, (current_map.width,0), (current_map.width,current_map.height), 0.0),
        pymunk.Segment(space.static_body, (current_map.width,current_map.height), (0,current_map.height), 0.0),
        pymunk.Segment(space.static_body, (0,current_map.height), (0,0), 0.0)]
    space.add(static_lines)

Here's my collision code

def collision_bullet_line(arb, space, data):
    bullet = arb.shapes[0]
    line   = arb.shapes[1]
    
    print('collision line!!')

handler = space.add_collision_handler(1, 5)
handler.pre_solve = collision_bullet_line

I want to avoid creating a class but I can do it like this; but it doesn't work.. maybe I can set collision type somewhere else? Any help appreciated.

class Line(GamePhysicsObject):
    """ This class extends the GamePhysicsObject to handle box objects. """

    def __init__(self, x, y, sprite, space):
        super().__init__(x , y, 0, sprite, space)
        self.shape.collision_type = 5
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Bart
  • 59
  • 5
  • A small tip is to make the segments representing the outer walls thicker (the last argument that you currently have set to 0.0). Then it will be less likely that fast objects tunnel through them. – viblo Dec 13 '22 at 08:02

1 Answers1

0

You should return a bool from the pre_solve callback function to indicate if you want pymunk to ignore the collision (if you return False) or to process the collision (if you return True).

viblo
  • 4,159
  • 4
  • 20
  • 28