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 self.game.solid_blocks:
            last = self.rect.copy()
            if self.rect.colliderect(block):
                topcheck = self.rect.top < block.rect.bottom and self.rect.top > block.rect.top
                bottomcheck = self.rect.bottom > block.rect.top 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"
                   
                   
                else:
                    self.kill()
               
        for enemy in self.game.enemies_list:
            if self.rect.colliderect(enemy):
                self.kill()

        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:
https://github.com/C-Dubb/Flatformer

Sorry I have been asking so many questions!
ChristianCareaga
 
Posts: 52
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.

-Mek
User avatar
Mekire
 
Posts: 1149
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests