Exemplo n.º 1
0
# Add the upper directory (where the nodebox module is) to the search path.
import os, sys
sys.path.insert(0, os.path.join("..", ".."))

from nodebox.graphics import *
from nodebox.graphics.physics import Vector, Boid, Flock, Obstacle

# Flocking can be used to simulate birds, herds, or school of fish.
# Each "boid" in the flock adheres to a simple set of rules:
# - separation: steer to avoid crowding local flockmates,
# - alignment: steer towards the average heading of local flockmates,
# - cohesion: steer to move toward the average position of local flockmates.

# Create a new flock.
flock = Flock(30, x=50, y=50, width=500, height=500, depth=100.0)
flock.space(
    30)  # Set the space for each boid (influences boid separation forces).
flock.sight(70)  # Set the line-of-sight for each boid
# (influences boid cohesion and alignment forces).

# Add some obstacles that the flock will need to evade.
for i in range(3):
    o = Obstacle(x=random(50, 500), y=random(50, 500), radius=random(10, 70))
    flock.obstacles.append(o)


def draw(canvas):

    # Clear the previous frame.
    # For fun, comment this out and observe the interesting flocking patterns.
    canvas.clear()
Exemplo n.º 2
0
# Add the upper directory (where the nodebox module is) to the search path.
import os, sys; sys.path.insert(0, os.path.join("..",".."))

from nodebox.graphics import *
from nodebox.graphics.physics import Vector, Boid, Flock, Obstacle

# Flocking can be used to simulate birds, herds, or school of fish.
# Each "boid" in the flock adheres to a simple set of rules:
# - separation: steer to avoid crowding local flockmates,
# - alignment: steer towards the average heading of local flockmates,
# - cohesion: steer to move toward the average position of local flockmates.

# Create a new flock.
flock = Flock(30, x=50, y=50, width=500, height=500, depth=100.0)
flock.space(30) # Set the space for each boid (influences boid separation forces).
flock.sight(70) # Set the line-of-sight for each boid 
                # (influences boid cohesion and alignment forces).

# Add some obstacles that the flock will need to evade.
for i in range(3):
    o = Obstacle(x=random(50,500), y=random(50,500), radius=random(10,70))
    flock.obstacles.append(o)

def draw(canvas):

    # Clear the previous frame.
    # For fun, comment this out and observe the interesting flocking patterns.
    canvas.clear()
    
    # If the mouse is moved inside the flocking area, 
    # set it as a target for the boids.