fixing bug #15

This commit is contained in:
Micho 2013-05-22 22:40:06 +02:00
parent b53ce14999
commit 8361d2f1ee
1 changed files with 51 additions and 42 deletions

View File

@ -11,7 +11,7 @@ var height = 490;
var gridSize = 3; var gridSize = 3;
var nearFieldRadius = 30; var nearFieldRadius = 30;
var timeStep = 1000/1500; (250, 500, 1000) var timeStep = 1000/1500; //(250, 500, 1000)
var decay = Math.pow(0.8, 5); var decay = Math.pow(0.8, 5);
var stepSize = 100*timeStep; var stepSize = 100*timeStep;
var frameDuration = 40; var frameDuration = 40;
@ -325,15 +325,10 @@ function updatePosition() {
} }
clients.forEach(function (client) { clients.forEach(function (client) {
client.r += (client.r0 - client.r)*decay; //client.r += (client.r0 - client.r)*decay;
client.r = client.r0; client.r = client.r0;
if (!valid(client.point) && (client.r < 1e-2)) { client.valid = valid(client.point) || (client.r > 1e-2);
client.valid = false;
return;
} else {
client.valid = true;
}
if (client.settled) { if (client.settled) {
var x = Math.round(client.x/gridSize); var x = Math.round(client.x/gridSize);
@ -373,6 +368,7 @@ function updatePosition() {
if (!client.settled) { if (!client.settled) {
// Kommt gerade von wo anders her // Kommt gerade von wo anders her
// Mach mal einen Sprung nach vorn
var xn, yn; var xn, yn;
var dx = (client.x0 - client.x); var dx = (client.x0 - client.x);
var dy = (client.y0 - client.y); var dy = (client.y0 - client.y);
@ -381,20 +377,24 @@ function updatePosition() {
var rn = Math.max(r-stepSize, 0); var rn = Math.max(r-stepSize, 0);
var f = 1-rn/r; var f = 1-rn/r;
xn = client.x + (client.x0 - client.x)*f; client.x = client.x + (client.x0 - client.x)*f;
yn = client.y + (client.y0 - client.y)*f; client.y = client.y + (client.y0 - client.y)*f;
} else { } else {
xn = client.x0; client.x = client.x0;
yn = client.y0; client.y = client.y0;
} }
var dx = (client.x0 - xn); // Bin ich schon da?
var dy = (client.y0 - yn); var dx = (client.x0 - client.x);
var dy = (client.y0 - client.y);
var r = Math.sqrt(dx*dx + dy*dy); var r = Math.sqrt(dx*dx + dy*dy);
if (r < nearFieldRadius) { if (r < nearFieldRadius) {
xn = Math.round(xn/gridSize); xn = Math.round(client.x/gridSize);
yn = Math.round(yn/gridSize); yn = Math.round(client.y/gridSize);
// Ist der neue Platz noch frei?
if (grid[xn][yn]) { if (grid[xn][yn]) {
grid[xn][yn] = false;
client.x = xn*gridSize; client.x = xn*gridSize;
client.y = yn*gridSize; client.y = yn*gridSize;
} else { } else {
@ -421,11 +421,7 @@ function updatePosition() {
client.settled = true; client.settled = true;
} }
} }
} else {
client.x = xn;
client.y = yn;
} }
} }
} }
}); });
@ -439,15 +435,18 @@ function updatePosition() {
var gridx = Math.round(client.x /gridSize); var gridx = Math.round(client.x /gridSize);
var gridy = Math.round(client.y /gridSize); var gridy = Math.round(client.y /gridSize);
var dxmin = 0; grid[gridx][gridy] = true;
var dymin = 0;
do {
var dx = (gridx-gridx0); var dx = (gridx-gridx0);
var dy = (gridy-gridy0); var dy = (gridy-gridy0);
var rMin = dx*dx + dy*dy - 1e-5; var rMin = dx*dx + dy*dy - 1e-5;
var dxmin = 0;
var dymin = 0;
var foundBetter = false
for (var dxg = -1; dxg <= 1; dxg++) { for (var dxg = -1; dxg <= 1; dxg++) {
for (var dyg = -1; dyg <= 1; dyg++) { for (var dyg = -1; dyg <= 1; dyg++) {
if (grid[gridx+dxg][gridy+dyg]) { if (grid[gridx+dxg][gridy+dyg] && ((dxg != 0) || (dyg != 0))) {
var dx = (gridx+dxg-gridx0); var dx = (gridx+dxg-gridx0);
var dy = (gridy+dyg-gridy0); var dy = (gridy+dyg-gridy0);
var r = dx*dx + dy*dy; var r = dx*dx + dy*dy;
@ -455,14 +454,19 @@ function updatePosition() {
rMin = r; rMin = r;
dxmin = dxg; dxmin = dxg;
dymin = dyg; dymin = dyg;
foundBetter = true;
} }
} }
} }
} }
grid[gridx][gridy] = true; if (foundBetter) {
grid[gridx + dxmin][gridy + dymin] = false; gridx += dxmin;
client.x = (gridx + dxmin)*gridSize; gridy += dymin;
client.y = (gridy + dymin)*gridSize; }
} while (foundBetter);
grid[gridx][gridy] = false;
client.x = gridx*gridSize;
client.y = gridy*gridSize;
//nachrücken //nachrücken
} }
}); });
@ -529,10 +533,15 @@ function renderCanvas() {
} }
} }
context.fillStyle = 'rgba(0,255,0,0.5)';
debugList.forEach(function (p) { debugList.forEach(function (p) {
context.beginPath(); context.beginPath();
context.arc(p.x, p.y, 1, 0, 2*Math.PI, false); context.fillStyle = '#aaf';
context.arc(p.xo, p.yo, 2, 0, 2*Math.PI, false);
context.fill();
context.beginPath();
context.fillStyle = '#0f0';
context.arc(p.x, p.y, 2, 0, 2*Math.PI, false);
context.fill(); context.fill();
}); });
*/ */