블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
  • total
  • today
  • yesterday

Category

2007. 6. 25. 18:50 Code/NodeBox
사용자 삽입 이미지
flock

class Boid:
    def __init__(self,x,y,vx,vy):
        self.x=x
        self.y=y
        self.vx=vx
        self.vy=vy
        self.tx=0
        self.ty=0
        
    def target(self,tx,ty):
        self.tx=tx
        self.ty=ty
#        oval(tx,ty,30,30)
        
    def chase(self):
        dx=self.tx-self.x
        dy=self.ty-self.y
        sx=dx-self.vx
        sy=dy-self.vy   
        ax=0
        ay=0
        ax+=sx/random(10,100)
        ay+=sy/random(10,100)
        self.vx+=ax
        self.vy+=ay
        self.x+=self.vx
        self.y+=self.vy
        
        r=10
        maxs=10

        if (dx==0) & (dy==0):
            self.vx=0
            self.vy=0
            
        if (dx>-r) & (dy<r):
            self.vx/=2
            self.vy/=2

        if (self.vx>maxs):
            self.vx=maxs
        if (self.vx<-maxs):
            self.vx=-maxs
        if (self.vy>maxs):
            self.vy=maxs
        if (self.vy<-maxs):
            self.vy=-maxs
            
    def render(self):
        fill(0.5)
        ovalc(self.x, self.y, 10, 10)
            
    #def update(self,ax,ay):
    #    self.ax=0
    #    self.ay=0    
    def boarders(self):
        r=10
        if (self.x<-r):
            self.x=WIDTH+r
        if (self.y<-r):
            self.y=HEIGHT+r
        if (self.x>WIDTH+r):
            self.x=-r
        if (self.y>HEIGHT+r):
            self.y=-r
            
def ovalc(x,y,w,h):
    x=x-w/2
    y=y-h/2
    oval(x,y,w,h)            
        
size(300,300)
speed(100)

def setup():
    global b
    b=range(20)
    
    for i in range(20):
        b[i]=Boid(i*random(100), i*random(100), i*random(10), i*random(10))
        
def draw():
    global b

    fill(0)
    ovalc(MOUSEX,MOUSEY,30,30)
    
    for i in b:
        i.target(MOUSEX,MOUSEY)
        i.chase()
        i.boarders()
        i.render()

'Code > NodeBox' 카테고리의 다른 글

matrix-color change  (0) 2007.06.27
matrix  (0) 2007.06.27
steering  (0) 2007.06.25
ping_orbit  (0) 2007.06.20
orbit  (0) 2007.06.20
posted by maetel