Bouncing bullets

Bouncing bullets

Postby ChristianCareaga » Wed Dec 25, 2013 10:22 pm

Alright so im working on making my bullets bounce of walls and floors, Right now the way i have it I think there is a better way to do it, If the bullet hits the top or bottom of a block then it's x vector is multiplied by one, if it hits the left or right of a block the y speed should be multiplied by one, that part works but im having a problem detecting what part of the block it hits

here is my bullet update:

Code: Select all
    def update(self, dt):
        if self.can_bounce:
            speed = -10.
            range = 200
            distance = [self.mouse_x - self.player[0], self.mouse_y - self.player[1]]
            norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
            direction = [distance[0] / norm, distance[1 ] / norm]
            bullet_vector = [direction[0] * speed, direction[1] * speed]

            self.dx = bullet_vector[0]
            self.dy = bullet_vector[1]
        for block in
            last = self.rect.copy()
            if self.rect.colliderect(block):
                topcheck = < block.rect.bottom and >
                bottomcheck = self.rect.bottom > and self.rect.bottom < block.rect.bottom
                rightcheck = self.rect.right > block.rect.left and self.rect.right < block.rect.right
                leftcheck = self.rect.left < block.rect.right and self.rect.left > block.rect.left
                if self.can_bounce:                     
                    if bottomcheck:
                        self.rect = last
                        self.dy  *= -1 #Bottom check
                        self.can_bounce = False
                        print "bottom"
                    if rightcheck:
                        self.rect = last
                        self.dx  *= -1 #right check
                        self.can_bounce = False
                        print "right"
                    if leftcheck:
                        self.rect = last
                        self.dx  *= -1 #left check
                        self.can_bounce = False
                        print "left"
                    if topcheck:
                        self.rect = last
                        self.dy  *= -1 #Top check
                        self.can_bounce = False
                        print "top"
        for enemy in
            if self.rect.colliderect(enemy):

        self.rect.x -= self.dx
        self.rect.y -= self.dy

it works sometimes but sometimes it doesnt bounce it just goes into the block and is killed, i think this happens when it hits the boundary of 2 blocks

I think there is a much more efficient way of doing this but dont know how!

You can test out the whole game from here:

Sorry I have been asking so many questions!
Posts: 54
Joined: Sat Jun 22, 2013 9:54 am

Re: Bouncing bullets

Postby Mekire » Thu Dec 26, 2013 1:32 pm

Haven't looked in detail yet, but as far as I can tell you never actually reset the bullet's position on collision; you flip the velocity but leave it inside the wall.

What I like to do in this situation is check for collisions normally, but then if you find a collision use a sort of swept collision technique to figure out exactly how far the bullet can move. You can do this by calculating a unit vector for your bullet velocity and on collision decrement by this unit vector until the collision is cleared. After you have cleared the collision you should be able to decide exactly what direction the bullet should bounce.

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Return to Game Development

Who is online

Users browsing this forum: Bing [Bot] and 1 guest