def decollide(self, other): ''' Shift overlapping entities apart. ''' overlap = (self.size + other.size - self.distance(other))/2 theta1 = angle_between(self.position, other.position) theta2 = angle_between(other.position, self.position) self.position += overlap * angle_position(theta2) other.position += overlap * angle_position(theta1) self.velocity *= -1 other.velocity *= -1
def draw_player(self, agent, colour): ''' Draw a player with given position and orientation. ''' size = PLAYER_CONFIG['SIZE'] self.draw_entity(agent, colour) radius_end = size*angle_position(agent.orientation) pos = vector_to_tuple(upscale(agent.position)) end = vector_to_tuple(upscale(agent.position + radius_end)) pygame.draw.line(self.window, self.black, pos, end)
def ball_angles(self, ball, angle): ''' Determines which angle to kick the ball along. ''' htc = self.homothetic_centre(ball) tangent1, tangent2 = self.tangent_points(htc) target = self.position + self.size*angle_position(angle) if norm(tangent1 - target) < norm(tangent2 - target): return angle_between(htc, tangent1) else: return angle_between(htc, tangent2)
def accelerate(self, power, theta): ''' Applies a power to the entity in direction theta. ''' rrand = uniform(-self.rand, self.rand) theta = (1 + rrand) * theta rmax = self.rand*norm(self.velocity) noise = vector(uniform(-rmax, rmax), uniform(-rmax, rmax)) rate = float(power) * self.power_rate acceleration = rate * angle_position(theta) + noise acceleration = bound_vector(acceleration, self.accel_max) self.velocity += acceleration self.velocity = bound_vector(self.velocity, self.speed_max)
def __init__(self): ''' The entities are set up and added to a space. ''' initial_player = vector(0, uniform(-PITCH_WIDTH/2, PITCH_WIDTH/2)) angle = angle_between(initial_player, vector(PITCH_LENGTH/2, 0)) self.player = Player(initial_player, angle) initial_ball = initial_player + KICKABLE * angle_position(angle) self.ball = Ball(initial_ball) initial_goalie = keeper_target(initial_ball) angle2 = angle_between(initial_goalie, initial_ball) self.goalie = Goalie(initial_goalie, angle2) self.entities = [self.player, self.goalie, self.ball] self.states = [] self.time = 0