boids: Use math.physics

db4
Eduardo Cavazos 2008-07-11 19:03:15 -05:00
parent 24a063ea76
commit b7b17ed879
2 changed files with 18 additions and 14 deletions

View File

@ -6,14 +6,17 @@ USING: combinators.short-circuit kernel namespaces
math.order
math.vectors
math.trig
math.physics.pos
math.physics.vel
combinators arrays sequences random vars
combinators.lib ;
combinators.lib
accessors ;
IN: boids
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TUPLE: boid pos vel ;
TUPLE: boid < vel ;
C: <boid> boid
@ -70,7 +73,7 @@ VAR: separation-radius
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: distance ( boid boid -- n ) [ boid-pos ] [ boid-pos ] bi* v- norm ;
: distance ( boid boid -- n ) [ pos>> ] [ pos>> ] bi* v- norm ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -81,10 +84,10 @@ VAR: separation-radius
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: relative-position ( self other -- v ) swap [ boid-pos ] bi@ v- ;
: relative-position ( self other -- v ) swap [ pos>> ] bi@ v- ;
: relative-angle ( self other -- angle )
over boid-vel -rot relative-position angle-between ;
over vel>> -rot relative-position angle-between ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -92,9 +95,9 @@ over boid-vel -rot relative-position angle-between ;
: vaverage ( seq-of-vectors -- seq ) [ vsum ] [ length ] bi v/n ;
: average-position ( boids -- pos ) [ boid-pos ] map vaverage ;
: average-position ( boids -- pos ) [ pos>> ] map vaverage ;
: average-velocity ( boids -- vel ) [ boid-vel ] map vaverage ;
: average-velocity ( boids -- vel ) [ vel>> ] map vaverage ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -123,7 +126,7 @@ over boid-vel -rot relative-position angle-between ;
dup cohesion-neighborhood
dup empty?
[ 2drop { 0 0 } ]
[ average-position swap boid-pos v- normalize* cohesion-weight> v*n ]
[ average-position swap pos>> v- normalize* cohesion-weight> v*n ]
if ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -143,7 +146,7 @@ over boid-vel -rot relative-position angle-between ;
dup separation-neighborhood
dup empty?
[ 2drop { 0 0 } ]
[ average-position swap boid-pos swap v- normalize* separation-weight> v*n ]
[ average-position swap pos>> swap v- normalize* separation-weight> v*n ]
if ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@ -206,10 +209,10 @@ cond ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: new-pos ( boid -- pos ) [ boid-pos ] [ boid-vel time-slice> v*n ] bi v+ ;
: new-pos ( boid -- pos ) [ pos>> ] [ vel>> time-slice> v*n ] bi v+ ;
: new-vel ( boid -- vel )
[ boid-vel ] [ acceleration time-slice> v*n ] bi v+ normalize* ;
[ vel>> ] [ acceleration time-slice> v*n ] bi v+ normalize* ;
: wrap-pos ( pos -- pos ) { [ wrap-x ] [ wrap-y ] } parallel-call ;

View File

@ -19,7 +19,8 @@ USING: combinators.short-circuit kernel namespaces
ui.gadgets.packs
ui.gadgets.grids
ui.gestures
assocs.lib vars rewrite-closures boids ;
assocs.lib vars rewrite-closures boids accessors
math.geometry.rect ;
IN: boids.ui
@ -27,9 +28,9 @@ IN: boids.ui
! draw-boid
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: point-a ( boid -- a ) boid-pos ;
: point-a ( boid -- a ) pos>> ;
: point-b ( boid -- b ) [ boid-pos ] [ boid-vel normalize* 20 v*n ] bi v+ ;
: point-b ( boid -- b ) [ pos>> ] [ vel>> normalize* 20 v*n ] bi v+ ;
: boid-points ( boid -- point-a point-b ) [ point-a ] [ point-b ] bi ;