Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
grains
grains-web
Commits
2020a336
Commit
2020a336
authored
May 13, 2017
by
Jens-Christian Fischer
Browse files
Masters now flock with their boids
parent
a7529a77
Changes
2
Hide whitespace changes
Inline
Side-by-side
mix.exs
View file @
2020a336
...
...
@@ -3,7 +3,7 @@ defmodule Grains.Mixfile do
def
project
do
[
app:
:grains
,
version:
"0.0.5
8
"
,
version:
"0.0.5
9
"
,
elixir:
"~> 1.2"
,
elixirc_paths:
elixirc_paths
(
Mix
.
env
),
compilers:
[
:phoenix
,
:gettext
]
++
Mix
.
compilers
,
...
...
web/static/js/presencesketch.js
View file @
2020a336
...
...
@@ -17,6 +17,9 @@ function Boid(p, x, y, type) {
this
.
maxspeed
=
3
;
// Maximum speed
this
.
maxforce
=
0.05
;
// Maximum steering force
this
.
type
=
type
;
this
.
separation_f
=
2.5
;
this
.
alignment_f
=
1.0
;
this
.
cohesion_f
=
2.0
;
};
...
...
@@ -54,9 +57,9 @@ Boid.prototype.flock = function(boids) {
var
ali
=
this
.
align
(
boids
);
// Alignment
var
coh
=
this
.
cohesion
(
boids
);
// Cohesion
// Arbitrarily weight these forces
sep
.
mult
(
2.5
);
ali
.
mult
(
1.0
);
coh
.
mult
(
2.0
);
sep
.
mult
(
this
.
separation_f
);
ali
.
mult
(
this
.
alignment_f
);
coh
.
mult
(
this
.
cohesion_f
);
// Add the force vectors to acceleration
this
.
applyForce
(
sep
);
this
.
applyForce
(
ali
);
...
...
@@ -117,15 +120,17 @@ Boid.prototype.separate = function(boids) {
var
count
=
0
;
// For every boid in the system, check if it's too close
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if
((
d
>
0
)
&&
(
d
<
desiredseparation
))
{
// Calculate vector pointing away from neighbor
var
diff
=
p5
.
Vector
.
sub
(
that
.
position
,
boid
.
position
);
diff
.
normalize
();
diff
.
div
(
d
);
// Weight by distance
steer
.
add
(
diff
);
count
++
;
// Keep track of how many
if
(
that
.
type
==
boid
.
type
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if
((
d
>
0
)
&&
(
d
<
desiredseparation
))
{
// Calculate vector pointing away from neighbor
var
diff
=
p5
.
Vector
.
sub
(
that
.
position
,
boid
.
position
);
diff
.
normalize
();
diff
.
div
(
d
);
// Weight by distance
steer
.
add
(
diff
);
count
++
;
// Keep track of how many
}
}
});
// Average -- divide by how many
...
...
@@ -152,10 +157,12 @@ Boid.prototype.align = function(boids) {
var
sum
=
this
.
p
.
createVector
(
0
,
0
);
var
count
=
0
;
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
.
velocity
);
count
++
;
if
(
that
.
type
==
boid
.
type
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
.
velocity
);
count
++
;
}
}
});
if
(
count
>
0
)
{
...
...
@@ -178,10 +185,12 @@ Boid.prototype.cohesion = function(boids) {
var
sum
=
this
.
p
.
createVector
(
0
,
0
);
// Start with empty vector to accumulate all locations
var
count
=
0
;
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
.
position
);
// Add location
count
++
;
if
(
that
.
type
==
boid
.
type
)
{
var
d
=
p5
.
Vector
.
dist
(
that
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
.
position
);
// Add location
count
++
;
}
}
});
if
(
count
>
0
)
{
...
...
@@ -249,12 +258,28 @@ function Master(p, x, y, type) {
Boid
.
call
(
this
,
p
,
x
,
y
,
null
);
this
.
radius
=
32
;
this
.
type
=
type
;
this
.
cohesion_f
=
6
;
this
.
separation_f
=
1
;
this
.
alignment_f
=
2
;
}
Master
.
prototype
=
Object
.
create
(
Boid
.
prototype
);
Master
.
prototype
.
constructor
=
Master
;
Master
.
prototype
.
run
=
function
(
boids
)
{
let
that
=
this
;
let
list
=
_
.
filter
(
boids
,
function
(
boid
)
{
return
that
.
type
==
boid
.
type
;
});
this
.
flock
(
boids
);
this
.
update
();
this
.
borders
();
this
.
render
();
}
Boid
.
prototype
.
render
=
function
()
{
this
.
p
.
fill
((
this
.
radius
));
this
.
p
.
stroke
(
200
);
...
...
@@ -376,8 +401,8 @@ let PresenceSketch = {
boid
.
run
(
list
);
});
_
.
each
(
PresenceSketch
.
masters
,
function
(
boid
,
key
,
list
)
{
boid
.
render
(
);
_
.
each
(
PresenceSketch
.
masters
,
function
(
master
,
key
,
list
)
{
master
.
run
(
PresenceSketch
.
boids
);
});
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment