python - what's wrong with my ball/bat collision and reflection? -
i have written breakout style game, , works apart ball bat collision , reflection. supposed work if ball moving left right hits bat:
if hits left end bounces in direction came if hits right end bounces in same direction
and vice versa right left direction. and:
if hits middle area bounces @ same angle if hits centre left/right areas bounces slight change in angle.
it goes through bat when on , should rebounding, confusing, think might due `bh == bat.y line, ball moving @ angle , might go on , carry on.
code: (baw/h = bat width/height, bw/h = ball width/height)
# check bat if theball.y+bh == thebat.y , (theball.x >= thebat.x-5 , theball.x <= thebat.x+batw): # collision in centre - rebounds angle reflection == angle incidence if theball.cx>= thebat.x+40 , theball.cx<=thebat.x+60: theball.dy = -theball.dy return # else find ball direction, areas each direction if theball.oldx < theball.x: # ball moving left right, find area: ends, mid lef, mid right, centre # collision left end if theball.cx<= thebat.x+10: # ball rebounds in direction came theball.dx = - theball.dx theball.dy = - theball.dy return # collision 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 , right # mid left if (theball.cx > thebat.x+10 , 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 , 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 left if theball.oldx > theball.x: # ball moving right left, find area: ends, mid lef, mid right, centre # collision right end if theball.cx>= thebat.x+90: # ball rebounds in direction came theball.dx = - theball.dx theball.dy = - theball.dy return # collision 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 , right # mid left if (theball.cx > thebat.x+10 , 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 , 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 it's because of
theball.y+bh == thebat.y
at angle, ball less @ right height. try instead add uncertainty. example:
uncertainty = 10 if theball.y+bh - uncertainty <= thebat.y <= theball.y+bh + uncertainty , ...
Comments
Post a Comment