def steer(self, desired): # steer = vector((0, 1)) if norm(desired) > self.maxspeed: desired = normalize(desired) * self.maxspeed self.desired = desired steer = desired - self.velocity steer = normalize(steer) * self.maxforce # steer.normalize() self.apply(steer)
def separate(self, vehicles): radius = self.size * 2 sum_vel = vector((0.0, 0.0)) n = 0 for other in vehicles: d = dist(self.position, other.position) if 0 < d < radius: n += 1 diff = normalize(self.position - other.position) / d sum_vel += diff if n > 0: sum_vel /= n sum_vel = normalize(sum_vel) * self.maxspeed self.steer(sum_vel)
def wander(self, d=50, r=25, change=0.5): center = normalize(self.velocity) * d + self.position self.theta += random() * 2 * change - change randrad = vector((sin(self.theta), cos(self.theta))) * r desired = center + randrad self.steer(desired - self.position)
def __init__(self, path, resolution=None): resolution = resolution or path.raduis / 1.5 super().__init__(resolution=resolution) for i in range(self.cols): for j in range(self.rows): center = vector((int((i + 0.5) * self.resolution), int((j + 0.5) * self.resolution))) self.field[i][j] = normalize(path.get_normal(center) - center) # * 0.2
def align(self, vehicles): sum_vel = vector((0.0, 0.0)) n = 0 for other in vehicles: if other is not self: sum_vel += other.velocity n += 1 if n > 0: sum_vel /= n sum_vel = normalize(sum_vel) * self.maxspeed # print(self.velocity, sum_vel) self.steer(sum_vel)
def track(self, path, scr=None, debug=False): prediction_rate = 25 future_loc = self.position + normalize(self.velocity) * prediction_rate # future_loc = vector((0, 1)) normal_loc = path.get_normal(future_loc) if debug: pygame.draw.circle(scr, (255, 128, 255), (int(normal_loc[0]), int(normal_loc[1])), 10) pygame.draw.circle(scr, (255, 230, 255), (int(future_loc[0]), int(future_loc[1])), 10) if dist(future_loc, normal_loc) > path.raduis: self.seek(normal_loc)
def follow(self, field): # desired = field.lookup(self.position + self.velocity) desired = normalize(field.lookup(self.position)) * self.maxspeed self.steer(desired)