Exemple #1
0
 def __init__(self, x, y):
     self.acceleration = vec2(0, 0)
     angle = random.uniform(0, 2 * math.pi)
     self.velocity = vec2(math.cos(angle), math.sin(angle))
     self.position = vec2(x, y)
     self.r = 2.0
     self.max_velocity = 2
     self.max_acceleration = 0.03
Exemple #2
0
 def cohesion(self, boids):
     neighbor_dist = 50
     sum = vec2(0, 0)  # Start with empty vector to accumulate all positions
     count = 0
     for other in boids:
         d = (self.position - other.position).length()
         if 0 < d < neighbor_dist:
             sum += other.position  # Add position
             count += 1
     if count > 0:
         sum /= count
         return self.seek(sum)
     else:
         return vec2(0, 0)
Exemple #3
0
    def separate(self, boids):
        desired_separation = 25.0
        steer = vec2(0, 0)
        count = 0

        # For every boid in the system, check if it's too close
        for other in boids:
            d = (self.position - other.position).length()
            # If the distance is greater than 0 and less than an arbitrary
            # amount (0 when you are yourself)
            if 0 < d < desired_separation:
                # Calculate vector pointing away from neighbor
                diff = self.position - other.position
                diff = diff.normalized()
                steer += diff / d  # Weight by distance
                count += 1  # Keep track of how many

        # Average - divide by how many
        if count > 0:
            steer /= count

        # As long as the vector is greater than 0
        if steer.length() > 0:
            # Implement Reynolds: Steering = Desired - Velocity
            steer = steer.normalized()
            steer *= self.max_velocity
            steer -= self.velocity
            steer = steer.limited(self.max_acceleration)

        return steer
Exemple #4
0
 def update(self):
     # Update velocity
     self.velocity += self.acceleration
     # Limit speed
     self.velocity = self.velocity.limited(self.max_velocity)
     self.position += self.velocity
     # Reset acceleration to 0 each cycle
     self.acceleration = vec2(0, 0)
Exemple #5
0
    def align(self, boids):
        neighbor_dist = 50
        sum = vec2(0, 0)
        count = 0
        for other in boids:
            d = (self.position - other.position).length()
            if 0 < d < neighbor_dist:
                sum += other.velocity
                count += 1

        if count > 0:
            sum /= count
            # Implement Reynolds: Steering = Desired - Velocity
            sum = sum.normalized()
            sum *= self.max_velocity
            steer = sum - self.velocity
            steer = steer.limited(self.max_acceleration)
            return steer
        else:
            return vec2(0, 0)
Exemple #6
0
 def borders(self):
     x, y = self.position
     x = (x + self.width) % self.width
     y = (y + self.height) % self.height
     self.position = vec2(x, y)