简体   繁体   中英

python - what's wrong with my ball/bat collision and reflection?

I have written a breakout style game, and it all works apart from the ball bat collision and reflection. It is supposed to work so that if the ball is moving left to right as it hits the bat:

if it hits the left end it bounces back in the direction it came from if it hits the right end it bounces back in the same direction

and vice versa for the right to left direction. And:

if it hits the middle area it bounces back at the same angle if it hits the centre left/right areas it bounces back with a slight change in the angle.

It also sometimes goes through the bat even when it is over it and should be rebounding, which is confusing, but I think might be due to the `BH == Bat.y line, as the ball is moving at an angle and so might go slightly over and just carry on.

Code: (BAW/H = Bat width/height, BW/H = ball width/height)

# check with bat

if theball.y+BH == thebat.y and (theball.x >= thebat.x-5 and theball.x <= thebat.x+BATW): 

# collision in centre - rebounds with angle reflection == angle incidence

    if theball.cx>= thebat.x+40 and theball.cx<=thebat.x+60:
        theball.dy = -theball.dy
        return

    # else find ball direction, do all areas for each direction

    if theball.oldx < theball.x: # ball moving left to right, find which area: ends, mid lef, mid right, centre

        # collision with left end

        if theball.cx<= thebat.x+10:

        # ball rebounds back in direction it came from

            theball.dx = - theball.dx
            theball.dy = - theball.dy
            return

        # collision with right end

        if theball.cx >= thebat.x+90:

            angle -= 10
            if angle < MIN_ANGLE:
                angle = MIN_ANGLE

            theball.dx, theball.dy = calc_dxdy(angle)
            return  

        # middle left and right 

        # mid left

        if (theball.cx > thebat.x+10 and theball.cx < thebat.x+40):

            angle -=5
            if angle <= MIN_ANGLE:
                angle = MIN_ANGLE
                theball.dx, theball.dy = calc_dxdy(angle)
                return

        # mid right

        if  (theball.cx > thebat.x+60 and theball.cx < thebat.x+90):

            angle +=5
            if angle >= MAX_ANGLE:
                angle = MAX_ANGLE
                theball.dx, theball.dy = calc_dxdy(angle)
                return  

    # ball moving right to left

    if theball.oldx > theball.x: # ball moving right to left, find which area: ends, mid lef, mid right, centre

        # collision with right end

        if theball.cx>= thebat.x+90: 

        # ball rebounds back in direction it came from

            theball.dx = - theball.dx
            theball.dy = - theball.dy
            return

        # collision with right end

        if theball.cx <= thebat.x+10:

            angle += 10
            if angle > MAX_ANGLE:
                angle = MAX_ANGLE

            theball.dx, theball.dy = calc_dxdy(angle)
            return  

        # middle left and right 

        # mid left

        if (theball.cx > thebat.x+10 and theball.cx < thebat.x+40):

            angle +=5
            if angle <= MAX_ANGLE:
                angle = MAX_ANGLE
                theball.dx, theball.dy = calc_dxdy(angle)
                return

        # mid right

        if  (theball.cx > thebat.x+60 and theball.cx < thebat.x+90):

            angle -=5
            if angle >= MIN_ANGLE:
                angle = MIN_ANGLE
                theball.dx, theball.dy = calc_dxdy(angle)
                return  

You're right that it's because of the

theball.y+BH == thebat.y 

At an angle, the ball is less likely to be at exactly the right height. Try to instead add some uncertainty. For example:

UNCERTAINTY = 10
if theball.y+BH - UNCERTAINTY <= thebat.y <= theball.y+BH + UNCERTAINTY and ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM