Making a bot for the game HaxBall in python using OpenCV, need help in emulating the key presses according to its physics -
i have been working on haxball bot. don't know haxball is: simplistic football game (or soccer) in control circle , try shoot ball in goal, can play here. making bot simulate human using opencv image processing detect player , ball. program quite nicely.
the next step move player, have used pyautogui move player around. want player move 1 coordinate , here problem lies.
i able move object in bumpy way alternating between keydown , keyups, want smooth motion decided use kinematics , found player velocity , hence found time key down. player still overshoots , needs move back. physics of haxball such objects (player , ball) slide on field , acceleration of objects not constant.
would recommend better approach movement problem. code can found here.
def emulate(): epsilon = 1e-5 starttime = time.time() # x,y = coords final position # buffer = acceptable error x,y,buffer = 276,132,40 # dict time how long press key presskeys = {"w": 0.0, "a": 0.0, "s": 0.0, "d": 0.0} while true: if len(player_center)>0 , len(ball_center)>0 , len(player_speed)>0: px,py,pr = player_center[-1][0], player_center[-1][1], player_radius pvx,pvy = abs(player_speed[-1][0]),abs(player_speed[-1][1]) bx,by,br = ball_center[-1][0], ball_center[-1][1], ball_radius if px-x>buffer: print("a") presskeys["a"] = (px-x)/(pvx+epsilon) elif x-px>buffer: print("d") presskeys["d"] = (x - px) / (pvx + epsilon) else: presskeys["a"] = 0.0 presskeys["d"] = 0.0 if py-y>buffer: print("w") presskeys["w"] = (py - y) / (pvy + epsilon) elif y-py>buffer: print("s") presskeys["s"] = (y - py) / (pvy + epsilon) else: presskeys["w"] = 0.0 presskeys["s"] = 0.0 currtime = time.time() if currtime-starttime < presskeys["w"]: pyautogui.keydown("w") else: pyautogui.keyup("w") if currtime-starttime < presskeys["s"]: pyautogui.keydown("s") else: pyautogui.keyup("s") if currtime-starttime < presskeys["a"]: pyautogui.keydown("a") else: pyautogui.keyup("a") if currtime-starttime < presskeys["d"]: pyautogui.keydown("d") else: pyautogui.keyup("d")
i apologized if couldnt explain (i have been bogged down problem), love make edits make more understandable. if wants work me on love work together.
Comments
Post a Comment