]> gitweb.factorcode.org Git - factor.git/blob - extra/boids/simulation/simulation.factor
b2df09f8c060ddf9cbcef1be6295209df79ee222
[factor.git] / extra / boids / simulation / simulation.factor
1 ! Copyright (C) 2008 Eduardo Cavazos.
2 ! Copyright (C) 2011 Anton Gorenko.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays combinators.short-circuit kernel math
5 math.vectors random sequences ;
6 IN: boids.simulation
7
8 CONSTANT: WIDTH 512
9 CONSTANT: HEIGHT 512
10
11 TUPLE: behaviour
12     { weight float }
13     { radius float }
14     { angle-cos float } ;
15
16 TUPLE: boid
17     { pos array }
18     { vel array } ;
19
20 C: <boid> boid
21
22 : vsum ( vecs -- v )
23     { 0.0 0.0 } [ v+ ] reduce ; inline
24
25 : vavg ( vecs -- v )
26     [ vsum ] [ length ] bi v/n ; inline
27
28 : in-radius? ( self other radius -- ? )
29     [ [ pos>> ] bi@ distance ] dip <= ; inline
30
31 : angle-between ( u v -- angle )
32     [ normalize ] bi@ vdot ; inline
33
34 : relative-position ( self other -- v )
35     swap [ pos>> ] bi@ v- ; inline
36
37 :: relative-angle ( self other -- angle )
38     self other relative-position
39     self vel>> angle-between ; inline
40
41 : in-view? ( self other angle-cos -- ? )
42     [ relative-angle ] dip >= ; inline
43
44 :: within-neighborhood? ( self other behaviour -- ? )
45     self other {
46         [ eq? not ]
47         [ behaviour radius>> in-radius? ]
48         [ behaviour angle-cos>> in-view? ]
49     } 2&& ; inline
50
51 :: neighbors ( boid boids behaviour -- neighbors )
52     boid boids [ behaviour within-neighborhood? ] with filter ;
53
54 GENERIC: force ( neighbors boid behaviour -- force )
55
56 :: (force) ( boid boids behaviour -- force )
57     boid boids behaviour neighbors
58     [ { 0.0 0.0 } ] [ boid behaviour force ] if-empty ;
59
60 : wrap-pos ( pos -- pos )
61     WIDTH HEIGHT 2array [ [ + ] keep mod ] 2map ;
62
63 :: simulate ( boids behaviours dt -- boids )
64     boids [| boid |
65         boid boids behaviours
66         [ [ (force) ] keep weight>> v*n ] 2with map vsum :> a
67
68         boid vel>> a dt v*n v+ normalize :> vel
69         boid pos>> vel dt v*n v+ wrap-pos :> pos
70
71         pos vel <boid>
72     ] map ;
73
74 : random-boids ( count -- boids )
75     [
76         WIDTH HEIGHT [ random ] bi@ 2array
77         2 [ 0 1 normal-random-float ] replicate
78         <boid>
79     ] replicate ;
80
81 TUPLE: cohesion < behaviour ;
82 TUPLE: alignment < behaviour ;
83 TUPLE: separation < behaviour ;
84
85 C: <cohesion> cohesion
86 C: <alignment> alignment
87 C: <separation> separation
88
89 M: cohesion force ( neighbors boid behaviour -- force )
90     drop [ [ pos>> ] map vavg ] [ pos>> ] bi* v- normalize ;
91
92 M: alignment force ( neighbors boid behaviour -- force )
93     2drop [ vel>> ] map vsum normalize ;
94
95 M:: separation force ( neighbors boid behaviour -- force )
96     behaviour radius>> :> r
97     boid pos>> neighbors
98     [ pos>> v- [ normalize ] [ r v/n ] bi v- ] with map vsum ;