2007. 6. 25. 17:54
Code/NodeBox
###steering
###http://www.red3d.com/cwr/steer/
size(300,300)
speed(100)
def setup():
global x
global y
global x1
global y1
global vx1
global vy1
global x2
global y2
global vx2
global vy2
x1=WIDTH/2-10
y1=HEIGHT/2-10
x2=WIDTH/2+10
y2=HEIGHT/2+10
vx1=random(5)
vy1=random(5)
vx2=random(5)
vy2=random(5)
def draw():
global x
global y
global x1
global y1
global vx1
global vy1
global x2
global y2
global vx2
global vy2
#target
x=MOUSEX
y=MOUSEY
ovalc(x,y,30,30)
maxs=10
# maxf=0.1
r=10
#reset
ax1=0
ay1=0
ax2=0
ay2=0
#direction d=t-p
dx1=x-x1
dy1=y-y1
dx2=x-x2
dy2=y-y2
if (dx1==0) & (dy1==0):
vx1=0
vy1=0
if (dx2==0) & (dy2==0):
vx2=maxs
vy2=maxs
if (dx1>-r) & (dx1<r):
vx1/=2
if (dy1>-r) & (dy1<r):
vy1/=2
#if(dx2>-r) & (dx2<r):
# vx2=maxs/10
#if(dy2>-r) & (dy2<r):
# vy2=maxs/10
# maximum speed limitation
if (vx1>maxs):
vx1=maxs
if (vx1<-maxs):
vx1=-maxs
if (vy1>maxs):
vy1=maxs
if(vy1<-maxs):
vy1=-maxs
if (vx2>maxs):
vx2=maxs
if (vx2<-maxs):
vx2=-maxs
if (vy2>maxs):
vy2=maxs
if (vy2<-maxs):
vy2=-maxs
# steering force: s=d-v=(t-p)-v
sx1=dx1-vx1
sy1=dy1-vy1
sx2=dx2-vx2
sy2=dy2-vy2
# seek
ax1+=sx1/100
ay1+=sy1/100
ax2+=sx2/100
ay2+=sy2/100
vx1+=ax1
vy1+=ay1
vx2+=ax2
vy2+=ay2
x1+=vx1
y1+=vy1
x2+=vx2
y2+=vy2
# at boarders
if (x2<-r):
x2=WIDTH+r
if (y2<-r):
y2=HEIGHT+r
if (x2>WIDTH+r):
x2=-r
if (y2>HEIGHT+r):
y2=-r
stroke(0)
fill(1)
ovalc(x1, y1, r, r)
# line(x1, y1, x1+sx1, y1+sy1)
fill(0.5)
ovalc(x2, y2, r, r)
# line(x2, y2, x2+sx2, y2+sy2)
def ovalc(x,y,w,h):
x=x-w/2
y=y-h/2
oval(x,y,w,h)
###http://www.red3d.com/cwr/steer/
size(300,300)
speed(100)
def setup():
global x
global y
global x1
global y1
global vx1
global vy1
global x2
global y2
global vx2
global vy2
x1=WIDTH/2-10
y1=HEIGHT/2-10
x2=WIDTH/2+10
y2=HEIGHT/2+10
vx1=random(5)
vy1=random(5)
vx2=random(5)
vy2=random(5)
def draw():
global x
global y
global x1
global y1
global vx1
global vy1
global x2
global y2
global vx2
global vy2
#target
x=MOUSEX
y=MOUSEY
ovalc(x,y,30,30)
maxs=10
# maxf=0.1
r=10
#reset
ax1=0
ay1=0
ax2=0
ay2=0
#direction d=t-p
dx1=x-x1
dy1=y-y1
dx2=x-x2
dy2=y-y2
if (dx1==0) & (dy1==0):
vx1=0
vy1=0
if (dx2==0) & (dy2==0):
vx2=maxs
vy2=maxs
if (dx1>-r) & (dx1<r):
vx1/=2
if (dy1>-r) & (dy1<r):
vy1/=2
#if(dx2>-r) & (dx2<r):
# vx2=maxs/10
#if(dy2>-r) & (dy2<r):
# vy2=maxs/10
# maximum speed limitation
if (vx1>maxs):
vx1=maxs
if (vx1<-maxs):
vx1=-maxs
if (vy1>maxs):
vy1=maxs
if(vy1<-maxs):
vy1=-maxs
if (vx2>maxs):
vx2=maxs
if (vx2<-maxs):
vx2=-maxs
if (vy2>maxs):
vy2=maxs
if (vy2<-maxs):
vy2=-maxs
# steering force: s=d-v=(t-p)-v
sx1=dx1-vx1
sy1=dy1-vy1
sx2=dx2-vx2
sy2=dy2-vy2
# seek
ax1+=sx1/100
ay1+=sy1/100
ax2+=sx2/100
ay2+=sy2/100
vx1+=ax1
vy1+=ay1
vx2+=ax2
vy2+=ay2
x1+=vx1
y1+=vy1
x2+=vx2
y2+=vy2
# at boarders
if (x2<-r):
x2=WIDTH+r
if (y2<-r):
y2=HEIGHT+r
if (x2>WIDTH+r):
x2=-r
if (y2>HEIGHT+r):
y2=-r
stroke(0)
fill(1)
ovalc(x1, y1, r, r)
# line(x1, y1, x1+sx1, y1+sy1)
fill(0.5)
ovalc(x2, y2, r, r)
# line(x2, y2, x2+sx2, y2+sy2)
def ovalc(x,y,w,h):
x=x-w/2
y=y-h/2
oval(x,y,w,h)
'Code > NodeBox' 카테고리의 다른 글
matrix (0) | 2007.06.27 |
---|---|
arrive (0) | 2007.06.25 |
ping_orbit (0) | 2007.06.20 |
orbit (0) | 2007.06.20 |
particle(rect+oval)_class (0) | 2007.06.20 |