python - 我的球/球撞击和反射有什么问题?

时间:2016-03-02 08:22:12

标签: python collision-detection collision

我写了一个突破风格的游戏,除了球棒碰撞和反射之外它都有效。它应该起作用,如果球在击中球棒时从左向右移动:

如果它撞到左端,它会从它的方向反弹回来 如果它击中右端,它会朝同一方向反弹

,反之亦然,从右到左的方向。和

如果它击中中间区域,它会以相同的角度反弹回来 如果它击中中心左/右区域,它会在角度稍有变化时反弹回来。

它甚至有时会经过蝙蝠,即使它已经超过它并且应该反弹,这是令人困惑的,但我认为可能是由于'BH == Bat.y线,因为球在一个角度移动所以可能会略微过去而继续。

代码:(BAW / H =蝙蝠宽度/高度,BW / H =球宽/高度)

# 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  

1 个答案:

答案 0 :(得分:0)

你是对的,因为

theball.y+BH == thebat.y 

在某个角度,球不太可能处于正确的高度。 试着改为添加一些不确定性。例如:

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