def run(self): car = Car(0, 0) ppu = 32 while not self.exit: dt = self.clock.get_time() / 1000 # Event queue for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True # User input pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: if car.velocity.x < 0: car.acceleration = car.brake_deceleration else: car.acceleration += 1 * dt elif pressed[pygame.K_DOWN]: if car.velocity.x > 0: car.acceleration = -car.brake_deceleration else: car.acceleration -= 1 * dt elif pressed[pygame.K_SPACE]: if abs(car.velocity.x) > dt * car.brake_deceleration: car.acceleration = -copysign(car.brake_deceleration, car.velocity.x) else: car.acceleration = -car.velocity.x / dt else: if abs(car.velocity.x) > dt * car.free_deceleration: car.acceleration = -copysign(car.free_deceleration, car.velocity.x) else: if dt != 0: car.acceleration = -car.velocity.x / dt car.acceleration = max(-car.max_acceleration, min(car.acceleration, car.max_acceleration)) if pressed[pygame.K_RIGHT]: car.steering -= 30 * dt elif pressed[pygame.K_LEFT]: car.steering += 30 * dt else: car.steering = 0 car.steering = max(-car.max_steering, min(car.steering, car.max_steering)) # Logic car.update(dt) # Drawing self.screen.fill((0, 0, 0)) rotated = pygame.transform.rotate(car_image, car.angle) rect = rotated.get_rect() self.screen.blit( rotated, car.position * ppu - (rect.width / 2, rect.height / 2)) pygame.display.flip() self.clock.tick(self.ticks) pygame.quit()
def run(self): # place car on road car = Car(5, 27) # initialize traffic self.init_traffic_cars() # sensor checkboxes on top right corner cbox_front_sensor = Checkbox(self.screen_width - 200, 10, 'Enable front sensor', True) cbox_rear_sensor = Checkbox(self.screen_width - 200, 35, 'Enable rear sensor', True) # reset position list -> to be updated rs_pos_list = [[650, 258, 90.0], [650, 258, 270.0], [0, 0, 180.0], [0, 0, 0.0], [302, 200, 45.0], [40, 997, 0.0], [40, 997, 180.0], [100, 997, 0.0], [100, 997, 180.0], [400, 998, 0.0], [400, 998, 180.0], [385, 315, 135.0]] # boolean variable needed to check for single-click press mouse_button_pressed = False # initialize object mask object_mask = pygame.Surface((self.screen_width, self.screen_height)) if self.record_data is True: index_image = 0 while not self.exit: # VARIABLE_UPDATE if self.traffic is True: collision_list = [False] * len(self.traffic_list) dt = self.clock.get_time() / 1000 self.event_handler(cbox_front_sensor, cbox_rear_sensor, mouse_button_pressed) # LOGIC self.key_handler(car, dt, rs_pos_list) car.acceleration = max(-car.max_acceleration, min(car.acceleration, car.max_acceleration)) car.steering = max(-car.max_steering, min(car.steering, car.max_steering)) # DRAWING stagePos = self.draw_sim_environment(car, object_mask, cbox_front_sensor, cbox_rear_sensor, print_coords=True) relPos = (stagePos[2], stagePos[3]) stagePos = (stagePos[0], stagePos[1]) # UPDATE # ------------------------ traffic car ----------------------------------------------- if self.traffic is True: self.check_collisions(collision_list) self.traffic_movement(collision_list, object_mask, stagePos) # ------------------------------------------------------------------------------------------- car.update(dt) act_mask = pygame.Surface((self.screen_width, self.screen_height)) if cbox_front_sensor.isChecked(): self.optimized_front_sensor(car, object_mask, act_mask, display_obstacle_on_sensor=True) if cbox_rear_sensor.isChecked(): self.optimized_rear_sensor(car, object_mask, act_mask, display_obstacle_on_sensor=True) if self.record_data is True: image_name = 'image_' + str(index_image) + '.png' print(index_image) index_image += 1 if self.record_data is True: # RECORD TAB # Save replay # Write reference trajectory write_data( os.path.join(os.path.dirname(__file__), "recorded_data", "reference.csv"), car.position, car.angle) write_data( os.path.join(os.path.dirname(__file__), "recorded_data", "obstacles.csv"), self.traffic_obstacle_points) # reset obstacle point list self.traffic_obstacle_points = list() pygame.display.update() self.clock.tick(self.ticks) pygame.quit()
max_torque_handle = car.get_weight_on_wheel( ) * mu_k * wheel.radius total_force += max_torque_handle / wheel.radius wheel.spinning = True else: # TODO wheel lock on ice and other things # TODO wheel is pushed and will rotate only if friction is high enough wheel.vis_torque.append(0) wheel.spinning = False # apply air resistance just one time total_resistance_force += car.get_air_resistance() # these forces can't move the car if it is still total_force -= total_resistance_force # newton second law car.acceleration = total_force / car.get_total_mass() # euler numeric integration car.speed += car.acceleration * timestep # iterate again over wheels to get acc speed for wheel in car.wheels: if not wheel.spinning: # wheel is connected to the car # if wheel speed is different from the correct one move toward it angular_acc = (car.speed / wheel.radius - wheel.angular_speed) / (2) # get car speed else: # wheel is not fully connected max_torque_handle = car.get_weight_on_wheel() * mu_s * wheel.radius angular_acc = car.acceleration / wheel.radius + ( wheel.torque - max_torque_handle) / (wheel.get_inertia() * 2)
p2 = (int(racetrack.checkpoints[i + 1][0]), int(racetrack.checkpoints[i + 1][1])) pygame.draw.line(screen, (0, 255 - colorOffset, 0 + colorOffset), p1, p2, 1) colorOffset = colorOffset + 5 for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True # User input pressed = pygame.key.get_pressed() sensors = car.radar.calculate_distance(racetrack) if pressed[pygame.K_UP]: if car.velocity.x < 0: car.acceleration = car.brake_deceleration else: car.acceleration += 1 * dt elif pressed[pygame.K_DOWN]: if car.velocity.x > 0: car.acceleration = -car.brake_deceleration else: car.acceleration -= 1 * dt elif pressed[pygame.K_SPACE]: if abs(car.velocity.x) > dt * car.brake_deceleration: car.acceleration = -copysign(car.brake_deceleration, car.velocity.x) else: car.acceleration = -car.velocity.x / dt else:
def run(self): car = Car(100 / 32, 75 / 32) image = pygame.image.load('car4.png').convert_alpha() wheel_rpm = car.rpm / (car.current_gear * car.differential_gear * 60 / 2 * 3.14) print(wheel_rpm) while not self.exit: dt = self.clock.get_time() / 1000 # Event queue for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True # User input pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: if car.velocity.x < 0: car.acceleration = car.brake_deceleration else: car.acceleration += 1 * dt elif pressed[pygame.K_DOWN]: if car.velocity.x > 0: car.acceleration = -car.brake_deceleration else: car.acceleration -= 1 * dt elif pressed[pygame.K_SPACE]: if car.velocity.x != 0: car.acceleration = copysign(car.max_acceleration, -car.velocity.x) else: car.acceleration = 0 car.acceleration = max(-car.max_acceleration, min(car.acceleration, car.max_acceleration)) if pressed[pygame.K_RIGHT]: car.steering -= 45 * dt elif pressed[pygame.K_LEFT]: car.steering += 45 * dt else: car.steering = 0 car.steering = max(-car.max_steering, min(car.steering, car.max_steering)) # Logic car.update(dt) # Drawing self.screen.fill((0, 0, 0)) rotate = pygame.transform.rotate(image, car.angle) rect = rotate.get_rect() ppu = 32 self.screen.blit( rotate, car.position * ppu - (rect.width / 2, rect.height / 2)) pygame.display.flip() self.clock.tick(self.ticks) pygame.quit()