示例#1
0
    def flock(self, boids):
        alignment = Vector(0, 0)
        cohesion = Vector(0, 0)
        separation = Vector(0, 0)

        total_in_sight = 0
        total_in_personal_space = 0
        for boid in boids:
            if boid is self:
                continue

            distance = self.position.distance(boid.position)
            if distance < self.sight_radius:
                alignment += boid.velocity
                cohesion += boid.position
                total_in_sight += 1
            if distance < self.personal_space_radius:
                difference = self.position - boid.position
                separation += difference / distance**2
                total_in_personal_space += 1

        alignment = self.align(alignment, total_in_sight)
        cohesion = self.cohere(cohesion, total_in_sight)
        separation = self.separate(separation, total_in_personal_space)

        self.acceleration = self.al_weight * alignment + self.co_weight * cohesion + self.sep_weight * separation
示例#2
0
def setup():
    global point_a
    global point_b
    size(600, 360)
    background(255)
    point_a = Vector(100, 300)
    point_b = Vector(400, 100)
示例#3
0
    def avoid_edges(self):
        if 25 >= self.location.x:
            self.desired_velocity = Vector(self.max_speed, self.velocity.y)

            self.steering_force = self.desired_velocity - self.velocity
            self.steering_force.limit(self.max_turning)
            self.acceleration += self.steering_force

        elif self.location.x >= width - 25:
            self.desired_velocity = Vector(-self.max_speed, self.velocity.y)

            self.steering_force = self.desired_velocity - self.velocity
            self.steering_force.limit(self.max_turning)
            self.acceleration += self.steering_force

        elif 25 >= self.location.y:
            self.desired_velocity = Vector(self.velocity.x, self.max_speed)

            self.steering_force = self.desired_velocity - self.velocity
            self.steering_force.limit(self.max_turning)
            self.acceleration += self.steering_force

        elif self.location.y >= height - 25:
            self.desired_velocity = Vector(self.velocity.x, -self.max_speed)

            self.steering_force = self.desired_velocity - self.velocity
            self.steering_force.limit(self.max_turning)
            self.acceleration += self.steering_force
示例#4
0
 def __init__(self, x, y, width, height):
     self.position = Vector(x, y)
     self.velocity = Vector(*np.random.rand(2) - 0.5) * 10
     self.acceleration = Vector(*np.random.rand(2) - 0.5) / 2
     self.width = width
     self.height = height
     self.maxSpeed = 5
     self.perseption = 100
示例#5
0
def attack(position, velocity, acceleration, current, ship):
    attack_direction = Vector(*ship.pos) - Vector(*current.pos)
    attack_direction = (attack_direction - Vector(*current.vel)) * MAX_SPEED

    position += velocity
    velocity += current.acceleration

    return position
示例#6
0
 def apply_drag(self, strength=0.1):
     neg_x = self.velocity.x * -1
     neg_y = self.velocity.y * -1
     drag_vector = Vector(neg_x, neg_y)
     drag_vector.normalize()
     drag_magnitude = self.velocity.magnitude_sq * strength
     drag_force = drag_vector * drag_magnitude
     self.apply_force(drag_force)
示例#7
0
 def __init__(self, x, y, width, height):
     self.position = Vector(x,y);
     self.velocity = Vector(*np.random.rand(2)-0.5)*10;
     self.acceleration = Vector(*np.random.rand(2)-0.5)/2;
     self.width = width;
     self.height = height;
     self.maxSpeed = 5;
     self.perseption = 50;    # Radius aroind boid
     self.movePos = 2;   # Used to move boid towards or away from center of mass
示例#8
0
 def __init__(self, x, y, width, height):
     self.position = Vector(x, y)
     self.velocity = Vector(*np.random.rand(2))
     self.acceleration = Vector(*np.random.rand(2))
     self.maxForce = 0.3  # to control the magnitude of cohesion and separation
     self.maxSpeed = 15
     self.perception = 200  #max distance to scan for other boids
     self.width = width
     self.height = height
示例#9
0
 def __init__(self, start_x=width / 2, start_y=height / 2):
     self.location = Vector(start_x, start_y)
     self.initial_velocity = Vector(random_gaussian(2, 3), random_gaussian(2, 3))
     self.mass = 15
     self.acceleration = Vector(0.01, 0.01)
     self.desired_velocity = Vector(0, 0)
     self.max_speed = random_gaussian(0.1, 0.05)
     self.max_turning = 0.5
     self.velocity = copy.copy(self.initial_velocity)
     self.debug = True
示例#10
0
 def __init__(self, x, y, dna: DNA = None):
     self.pos = Vector(x, y)
     self.vel = Vector(0, -2)
     self.acc = Vector(0, 0)
     self.rad = 10
     self.__name = [
         names.get_first_name(),
         names.get_last_name(),
         names.get_last_name()
     ]
     self.__dna = dna
     self.__total_evolvers += 1
示例#11
0
 def __init__(self, x, y, width, height):
     self.max_steer = 1
     self.max_speed = 5
     self.sight_radius = 100
     self.personal_space_radius = 50
     self.width = width
     self.height = height
     self.position = Vector(x, y)
     self.bucket = self.determine_bucket()
     self.velocity = Vector.random_2D() * self.max_speed
     self.acceleration = Vector(0, 0)
     self.al_weight, self.co_weight, self.sep_weight = ratio((1, 1, 1.1))
示例#12
0
文件: boid.py 项目: gitUmaru/boid
    def __init__(self, x, y, width, height):
        self.position = Vector(x, y)
        vec = (np.random.rand(2) - 0.5) * 10
        self.velocity = Vector(*vec)

        vec = (np.random.rand(2) - 0.5) / 2
        self.acceleration = Vector(*vec)
        self.max_force = 0.3
        self.max_speed = 5
        self.perception = 100

        self.width = width
        self.height = height
示例#13
0
 def __init__(self,x,y,width,height,size,sight,speeds,force_max,ks):
   self.id = Boid._id
   Boid._id += 1
   self.width = width
   self.height = height
   self.size = size
   self.sight = sight
   self.speed_min,self.speed_max = speeds
   self.force_max = force_max
   self.ks = ks
   self.pos = VecTorus(x,y,width,height)
   self.vel = Vector(*rand_vect(shift=-0.5,scale=self.speed_max))
   self.acc = Vector(*rand_vect(shift=-0.5,scale=force_max))
示例#14
0
 def __init__(self, x, y, width, height):
     self.position = Vector(x, y)
     self.velocity = Vector(*np.random.rand(2) - 0.5) * 10
     self.acceleration = Vector(*np.random.rand(2) - 0.5) / 2
     self.width = width
     self.height = height
     self.maxSpeed = 5
     self.perseption = 100
     # Radius around boid
     self.movePos = 2
     # Used to move boid towards or away from center of mass
     self.dir = Vector(1, 1)
     self.blinkCounter = np.random.randint(9)
示例#15
0
 def separate(self, vehicles):
     sum = Vector(0, 0)
     count = 0
     for other in vehicles:
         d = Vector.distance(self.location, other.location)
         repel_vector = other.location - self.location
         if d > 0 and d < self.view_range:
             sum += repel_vector
             count += 1
         if count > 0:
             sum /= count
             steering_force = sum - self.velocity
             steering_force.limit(self.max_turning)
             self.applyForce(steering_force)
示例#16
0
 def alignment(self, boids):
     steering = Vector(0, 0)
     total = 0
     average_vector = Vector(0, 0)
     for boid in boids:
         if np.linalg.norm(boid.position - self.position) < self.perception:
             average_vector += boid.velocity
             total += 1
     if total > 0:
         average_vector = Vector(*average_vector / total)
         average_vector = (average_vector /
                           np.linalg.norm(average_vector)) * self.maxSpeed
         steering = average_vector - self.velocity
     return steering
示例#17
0
def draw():
    background(250)
    global point_a
    global point_b
    stroke(0)
    mouseX = mouse_x
    mouseY = mouse_y
    mouse = Vector(mouseX, mouseY)
    mouse_point = Vector(mouse_x, mouse_y)

    line(point_a, mouse)
    line(point_a, point_b)

    norm = scalar_projection(mouse, point_a, point_b)
    ellipse((norm.x, norm.y), 10, 10)
示例#18
0
 def __init__(self, start_x=width / 2, start_y=height / 2, identifier=0):
     self.location = Vector(start_x, start_y)
     self.initial_velocity = Vector(random_gaussian(2, 3),
                                    random_gaussian(2, 3))
     self.velocity = copy.copy(self.initial_velocity)
     self.desired_velocity = Vector(0, 0)
     self.acceleration = Vector(0.01, 0.01)
     self.max_speed = 1
     self.max_turning = 1
     self.mass = 5
     self.size = 25
     self.initial_lifespan = 100
     self.lifespan = self.initial_lifespan
     self.identifier = identifier
     self.is_dead = False
示例#19
0
    def __init__(self, x, y, width, heigth):
        self.position = Vector(x, y)
        self.width = width
        self.heigth = heigth
        self.max_speed = 10
        self.max_force = 1
        self.perception = 100

        #set velocity vector
        vec = (np.random.rand(2) - 0.5) * 10
        self.velocity = Vector(*vec)

        #set acceleration vector
        vec = (np.random.rand(2) - 0.5) * 10
        self.acceleration = Vector(*vec)
示例#20
0
    def align(self, boids, p=1):
        deviation = Vector(*np.zeros(2))
        total = 0
        avg_dir = Vector(*np.zeros(2))
        #check average direction(by boid.velocity)
        for boid in boids:
            if np.linalg.norm(boid.position -
                              self.position) < self.perception * p:
                avg_dir += boid.velocity
                total += 1
        if total > 0:
            avg_dir = Vector(*(avg_dir / total))
            avg_dir = (avg_dir / np.linalg.norm(avg_dir)) * self.max_speed
            deviation = avg_dir - self.velocity

        return deviation
示例#21
0
    def follow(self, path):
        # calculate future location
        predicted_location = copy.copy(self.velocity)
        predicted_location.normalize()
        predicted_location *= 10
        predicted_location += self.location
        shortest_distance = width  # initialise to a large number
        # check whether future location is on path

        for i in range(len(path.points) - 1):
            point_a = path.points[i]
            point_b = path.points[i + 1]
            norm = sp.scalar_projection(predicted_location, point_a, point_b)
            if(norm.x < point_a.x or norm.x > point_b.x):
                continue
            distance = Vector.distance(norm, predicted_location)
            if distance < shortest_distance:
                shortest_distance = distance
                direction = point_b - point_a
                direction.normalize()
                direction *= 20
                target = norm + direction


            if shortest_distance > path.radius:
                self.steer(target)
                if self.debug:
                    line(self._tup(self.location), (self._tup(predicted_location)))
                    fill(255, 0, 0)
                    ellipse((norm.x, norm.y), 10, 10)
                    fill(0, 255, 0)
                    ellipse((target.x, target.y), 10, 10)
示例#22
0
 def update(self):
     self.position += self.velocity
     self.edges()
     self.bucket = self.determine_bucket()
     self.velocity += self.acceleration
     self.velocity.limit(upper_limit=self.max_speed)
     self.acceleration = Vector(0, 0)
示例#23
0
def draw():
    global fountains
    global gravity
    global wind
    global repeller

    background(120)

    if mouse_is_pressed:
        fountains.append(ParticleSystem(mouse_x, mouse_y, len(fountains)))
        print(f"there are {len(fountains)} fountains.")

    for fountain in reversed(fountains):

        if fountain.is_empty:
            print(f"fountain {fountain.identifier} empty")
            fountains.remove(fountain)
        fountain.update()
        fountain.apply_force(gravity)
        fountain.apply_repeller(repeller)
        repeller.display()
        if key_is_pressed:
            mouse = Vector(mouse_x, mouse_y)
            fountain.apply_force(wind.wind_force(mouse))
            wind.display(mouse)
示例#24
0
    def align(self, boids):
        steering = Vector(*np.zeros(2))
        total = 0
        avg_vector = Vector(*np.zeros(2))
        for boid in boids:
            if boid.dead == False:
                if np.linalg.norm(boid.position - self.position) < self.perception:
                    avg_vector += boid.velocity
                    total += 1
        if total > 0:
            avg_vector /= total
            avg_vector = Vector(*avg_vector)
            avg_vector = (avg_vector / np.linalg.norm(avg_vector)) * self.max_speed
            steering = avg_vector - self.velocity

        return steering
示例#25
0
 def populate(self):
     self.points = []
     self.points.append(self.first_point)
     for point in range(self.num_of_paths):
         self.points.append(
             Vector(((point + 1) * width / self.num_of_paths),
                    random_uniform(height, 0)))
示例#26
0
    def update(self):
        self.position += self.velocity
        self.velocity += self.acceleration

        if np.linalg.norm(self.velocity) > self.maxSpeed:
            self.velocity = self.velocity / np.linalg.norm(
                self.velocity) * self.maxSpeed
        self.acceleration = Vector(0, 0)
示例#27
0
 def react(self,boids): # reactive traits
   boids_nearby = self.nearby(boids)
   accs = (
     self.align(boids_nearby) * self.ks.get('k_align',0),
     self.cohere(boids_nearby) * self.ks.get('k_cohere',0),
     self.repel(boids_nearby) * self.ks.get('k_repel',0),
     )
   self.acc += sum(accs,Vector(0,0))
示例#28
0
 def align(self, vehicles, method="max"):
     sum = Vector(0, 0)
     count = 0
     for other in vehicles:
         d = Vector.distance(self.location, other.location)
         if d > 0 and d < self.view_range:
             sum += other.velocity
             count += 1
         if count > 0:
             sum /= count
             if method == "max":
                 sum.normalize()
                 sum *= self.max_speed
             if method == "average":
                 pass
             steering_force = sum - self.velocity
             steering_force.limit(self.max_turning)
             self.applyForce(steering_force)
示例#29
0
    def update(self):
        self.position += self.velocity
        self.velocity += self.acceleration
        #limit
        if np.linalg.norm(self.velocity) > self.max_speed:
            self.velocity = self.velocity / np.linalg.norm(
                self.velocity) * self.max_speed

        self.acceleration = Vector(*np.zeros(2))
示例#30
0
    def display(self, window, debug=False):
        gr = Vector(0, 255, 0)
        rd = Vector(255, 0, 0)
        col = tuple([int(i) for i in rd.lerp(gr, self.health)])
        # print(col)
        pg.draw.circle(window, col, self._pos, self.radius * 2)

        angle = self.vel.angle + pi / 2
        pg.transform.rotate(window, angle)

        if debug:
            dna2 = remap(self.dna[2], (-2, 2), (5, 100))
            dna3 = remap(self.dna[3], (-2, 2), (5, 100))
            lineg = tuple(self.pos * dna2)
            liner = tuple(self.pos * dna3)

            pg.draw.aaline(window, tuple(gr), self._pos, (lineg[:2]))
            pg.draw.aaline(window, tuple(rd), self._pos, (liner[:2]))