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
c1aad677
Commit
c1aad677
authored
May 09, 2017
by
Jens-Christian Fischer
Browse files
Tweaked orientation & made boids work again
parent
cac71ada
Changes
4
Hide whitespace changes
Inline
Side-by-side
mix.exs
View file @
c1aad677
...
...
@@ -3,7 +3,7 @@ defmodule Grains.Mixfile do
def
project
do
[
app:
:grains
,
version:
"0.0.5
3
"
,
version:
"0.0.5
4
"
,
elixir:
"~> 1.2"
,
elixirc_paths:
elixirc_paths
(
Mix
.
env
),
compilers:
[
:phoenix
,
:gettext
]
++
Mix
.
compilers
,
...
...
web/static/js/app.js
View file @
c1aad677
...
...
@@ -101,24 +101,26 @@ room.join().receive("ok", resp => {
})
.
receive
(
"
error
"
,
reason
=>
console
.
log
(
"
presence join failed
"
,
reason
))
let
visuals
=
socket
.
channel
(
"
visuals:data
"
,
{});
if
(
document
.
getElementById
(
'
sketch
'
))
{
visuals
.
on
(
"
velocity
"
,
state
=>
{
console
.
log
(
"
velocity:
"
,
state
);
PresenceSketch
.
set_velocity
(
state
.
user
,
state
.
dx
,
state
.
dy
);
});
let
visuals
=
socket
.
channel
(
"
visuals:data
"
,
{});
visuals
.
on
(
"
acceleration
"
,
state
=>
{
console
.
log
(
"
acceleration
:
"
,
state
);
PresenceSketch
.
set_
acceleration
(
state
.
user
,
state
.
force
);
});
visuals
.
on
(
"
velocity
"
,
state
=>
{
console
.
log
(
"
velocity
:
"
,
state
);
PresenceSketch
.
set_
velocity
(
state
.
user
,
state
.
dy
,
state
.
dx
);
});
visuals
.
join
().
receive
(
"
ok
"
,
resp
=>
{
console
.
log
(
"
visual joined
"
);
})
.
receive
(
"
error
"
,
reason
=>
console
.
log
(
"
visual join failed
"
,
reason
))
visuals
.
on
(
"
acceleration
"
,
state
=>
{
console
.
log
(
"
acceleration:
"
,
state
);
PresenceSketch
.
set_acceleration
(
state
.
user
,
state
.
force
);
});
visuals
.
join
().
receive
(
"
ok
"
,
resp
=>
{
console
.
log
(
"
visual joined
"
);
})
.
receive
(
"
error
"
,
reason
=>
console
.
log
(
"
visual join failed
"
,
reason
))
}
// Import local files
...
...
web/static/js/orientation.js
View file @
c1aad677
...
...
@@ -65,7 +65,7 @@ let OrientationPanel = {
},
send_value
(
values
)
{
values
[
0
]
=
((
values
[
0
]
-
0.
3
)
/
0.7
)
-
0.5
;
// scale to normal movement range
values
[
0
]
=
((
(
values
[
0
]
-
0.
25
)
/
0.7
5
)
-
0.5
)
*
2
;
// scale to normal movement range
values
[
1
]
=
Math
.
pow
((
values
[
1
]
-
0.5
),
3
)
*
8
;
// prefer larger amounts
let
scaled
=
values
.
map
(
function
(
i
)
{
...
...
web/static/js/presencesketch.js
View file @
c1aad677
...
...
@@ -17,7 +17,9 @@ function Boid(p, x, y) {
};
Boid
.
prototype
.
set_velocity
=
function
(
dx
,
dy
)
{
this
.
velocity
=
this
.
p
.
createVector
(
dx
,
dy
);
// this.velocity = this.p.createVector(dx, dy);
let
acc
=
this
.
p
.
createVector
(
dx
,
dy
);
this
.
applyForce
(
acc
);
};
...
...
@@ -47,12 +49,12 @@ Boid.prototype.flock = function(boids) {
var
ali
=
this
.
align
(
boids
);
// Alignment
var
coh
=
this
.
cohesion
(
boids
);
// Cohesion
// Arbitrarily weight these forces
sep
.
mult
(
4
.5
);
sep
.
mult
(
2
.5
);
ali
.
mult
(
1.0
);
coh
.
mult
(
1
.0
);
coh
.
mult
(
2
.0
);
// Add the force vectors to acceleration
this
.
applyForce
(
sep
);
//
this.applyForce(ali);
this
.
applyForce
(
ali
);
this
.
applyForce
(
coh
);
};
...
...
@@ -98,22 +100,23 @@ Boid.prototype.borders = function() {
// Separation
// Method checks for nearby boids and steers away
Boid
.
prototype
.
separate
=
function
(
boids
)
{
let
that
=
this
;
var
desiredseparation
=
25.0
;
var
steer
=
this
.
p
.
createVector
(
0
,
0
);
var
count
=
0
;
// For every boid in the system, check if it's too close
for
(
var
i
=
0
;
i
<
boids
.
length
;
i
++
)
{
var
d
=
p5
.
Vector
.
dist
(
th
is
.
position
,
boid
s
[
i
]
.
position
);
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
th
at
.
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
(
th
is
.
position
,
boid
s
[
i
]
.
position
);
var
diff
=
p5
.
Vector
.
sub
(
th
at
.
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
if
(
count
>
0
)
{
steer
.
div
(
count
);
...
...
@@ -133,16 +136,17 @@ Boid.prototype.separate = function(boids) {
// Alignment
// For every nearby boid in the system, calculate the average velocity
Boid
.
prototype
.
align
=
function
(
boids
)
{
let
that
=
this
;
var
neighbordist
=
50
;
var
sum
=
this
.
p
.
createVector
(
0
,
0
);
var
count
=
0
;
for
(
var
i
=
0
;
i
<
boids
.
length
;
i
++
)
{
var
d
=
p5
.
Vector
.
dist
(
th
is
.
position
,
boid
s
[
i
]
.
position
);
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
th
at
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
s
[
i
]
.
velocity
);
sum
.
add
(
boid
.
velocity
);
count
++
;
}
}
}
);
if
(
count
>
0
)
{
sum
.
div
(
count
);
sum
.
normalize
();
...
...
@@ -158,16 +162,17 @@ Boid.prototype.align = function(boids) {
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
Boid
.
prototype
.
cohesion
=
function
(
boids
)
{
let
that
=
this
;
var
neighbordist
=
50
;
var
sum
=
this
.
p
.
createVector
(
0
,
0
);
// Start with empty vector to accumulate all locations
var
count
=
0
;
for
(
var
i
=
0
;
i
<
boids
.
length
;
i
++
)
{
var
d
=
p5
.
Vector
.
dist
(
th
is
.
position
,
boid
s
[
i
]
.
position
);
_
.
each
(
boids
,
function
(
boid
)
{
var
d
=
p5
.
Vector
.
dist
(
th
at
.
position
,
boid
.
position
);
if
((
d
>
0
)
&&
(
d
<
neighbordist
))
{
sum
.
add
(
boid
s
[
i
]
.
position
);
// Add location
sum
.
add
(
boid
.
position
);
// Add location
count
++
;
}
}
}
);
if
(
count
>
0
)
{
sum
.
div
(
count
);
return
this
.
seek
(
sum
);
// Steer towards the location
...
...
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