From 37867735631102b892e4f72b632c6f68509dcfa3 Mon Sep 17 00:00:00 2001 From: JustinOros Date: Mon, 15 Sep 2025 09:17:48 -0700 Subject: [PATCH] Fixed bug where enemy tanks did not point/aim forward while moving/shooting. --- tankwars-game/tankwars-game.py | 70 ++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/tankwars-game/tankwars-game.py b/tankwars-game/tankwars-game.py index 9356e58..64f5d03 100644 --- a/tankwars-game/tankwars-game.py +++ b/tankwars-game/tankwars-game.py @@ -291,34 +291,32 @@ def draw_bullet(x,y,z,color): glPopMatrix() -def draw_player_tank(x, z, direction=0, turret_target=None): +def draw_player_tank(x, z, direction=0, turret_angle=0, turret_pitch=0): glPushMatrix() glTranslatef(x, 0, z) glRotatef(direction, 0, 1, 0) glColor3f(0,1,1) if NPY_AVAILABLE and tank_display_list: + glPushMatrix() + glRotatef(-90, 0, 1, 0) + glRotatef(turret_angle - direction, 0, 1, 0) + glRotatef(-turret_pitch, 1, 0, 0) glCallList(tank_display_list) + glPopMatrix() else: draw_cube(2) - if turret_target: - dx = turret_target[0]-x - dz = turret_target[2]-z - turret_yaw = math.degrees(math.atan2(dx,-dz)) - dy = turret_target[1] - 1 - horizontal_dist = math.sqrt(dx*dx + dz*dz) - turret_pitch = math.degrees(math.atan2(dy, horizontal_dist)) if horizontal_dist>0 else 0 glPushMatrix() - glRotatef(turret_yaw - direction, 0, 1, 0) - glTranslatef(0, 1, 1.5) - glRotatef(-turret_pitch, 1, 0, 0) + glTranslatef(0, 1, 0) + glRotatef(turret_angle - direction, 0, 1, 0) + glRotatef(-turret_pitch, 1, 0, 0) + glTranslatef(0, 0, 1.5) glColor3f(0.8,0.8,0) - if not NPY_AVAILABLE: - glScalef(0.2,0.2,2) - draw_cube(1) + glScalef(0.2,0.2,2) + draw_cube(1) glPopMatrix() glPopMatrix() -def draw_enemy_tank(x, z, body_direction=0, turret_target=None): +def draw_enemy_tank(x, z, body_direction=0, turret_angle=0): distance = math.hypot(x - player_pos[0], z - player_pos[2]) if distance >= 100: return @@ -330,22 +328,22 @@ def draw_enemy_tank(x, z, body_direction=0, turret_target=None): glColor4f(1,0,0,alpha) glPushMatrix() glTranslatef(x, 0, z) - glRotatef(body_direction + 90, 0, 1, 0) + glRotatef(body_direction, 0, 1, 0) if NPY_AVAILABLE and tank_display_list: + glPushMatrix() + glRotatef(-90, 0, 1, 0) + glRotatef(turret_angle - body_direction, 0, 1, 0) glCallList(tank_display_list) + glPopMatrix() else: draw_cube(2) - if turret_target: - dx = turret_target[0]-x - dz = turret_target[2]-z - turret_angle = math.degrees(math.atan2(dx,-dz)) glPushMatrix() - glRotatef(turret_angle - (body_direction + 90), 0,1,0) - glTranslatef(0,1,1.5) + glTranslatef(0, 1, 0) + glRotatef(turret_angle - body_direction, 0, 1, 0) + glTranslatef(0, 0, 1.5) glColor4f(1,1,0,alpha) - if not NPY_AVAILABLE: - glScalef(0.2,0.2,2) - draw_cube(1) + glScalef(0.2,0.2,2) + draw_cube(1) glPopMatrix() glPopMatrix() glDisable(GL_BLEND) @@ -377,7 +375,7 @@ def move_enemies(dt): e['state'] = 'hunting' target_dx = player_pos[0] - e['pos'][0] target_dz = player_pos[2] - e['pos'][2] - e['target_angle'] = math.degrees(math.atan2(target_dx, -target_dz)) + e['target_angle'] = math.degrees(math.atan2(target_dx, target_dz)) if e['state']=='rotating': angle_diff = (e['target_angle'] - e['angle'] + 540)%360 - 180 max_rot = 120*dt @@ -393,7 +391,7 @@ def move_enemies(dt): e['angle']%=360 elif e['state']=='moving': dx = math.sin(math.radians(e['angle'])) - dz = -math.cos(math.radians(e['angle'])) + dz = math.cos(math.radians(e['angle'])) new_pos = [e['pos'][0]+dx*enemy_speed*dt,0,e['pos'][2]+dz*enemy_speed*dt] if not check_tank_collision(new_pos,ignore_enemy=e): e['pos'][0]=new_pos[0] @@ -411,7 +409,7 @@ def move_enemies(dt): elif e['state']=='hunting': target_dx = player_pos[0]-e['pos'][0] target_dz = player_pos[2]-e['pos'][2] - target_angle = math.degrees(math.atan2(target_dx, -target_dz)) + target_angle = math.degrees(math.atan2(target_dx, target_dz)) angle_diff = (target_angle - e['angle'] + 540)%360 - 180 max_rot = 90*dt if abs(angle_diff)