Commit c1aad677 authored by Jens-Christian Fischer's avatar Jens-Christian Fischer
Browse files

Tweaked orientation & made boids work again

parent cac71ada
......@@ -3,7 +3,7 @@ defmodule Grains.Mixfile do
def project do
[app: :grains,
version: "0.0.53",
version: "0.0.54",
elixir: "~> 1.2",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
......
......@@ -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
......
......@@ -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.75) - 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) {
......
......@@ -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(this.position, boids[i].position);
_.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(this.position, boids[i].position);
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
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(this.position, boids[i].position);
_.each(boids, function(boid) {
var d = p5.Vector.dist(that.position, boid.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[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(this.position, boids[i].position);
_.each(boids, function(boid) {
var d = p5.Vector.dist(that.position, boid.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment