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,30 +435,38 @@ 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;
var dx = (gridx-gridx0); do {
var dy = (gridy-gridy0); var dx = (gridx-gridx0);
var rMin = dx*dx + dy*dy - 1e-5; var dy = (gridy-gridy0);
for (var dxg = -1; dxg <= 1; dxg++) { var rMin = dx*dx + dy*dy - 1e-5;
for (var dyg = -1; dyg <= 1; dyg++) { var dxmin = 0;
if (grid[gridx+dxg][gridy+dyg]) { var dymin = 0;
var dx = (gridx+dxg-gridx0); var foundBetter = false
var dy = (gridy+dyg-gridy0); for (var dxg = -1; dxg <= 1; dxg++) {
var r = dx*dx + dy*dy; for (var dyg = -1; dyg <= 1; dyg++) {
if (r < rMin) { if (grid[gridx+dxg][gridy+dyg] && ((dxg != 0) || (dyg != 0))) {
rMin = r; var dx = (gridx+dxg-gridx0);
dxmin = dxg; var dy = (gridy+dyg-gridy0);
dymin = dyg; var r = dx*dx + dy*dy;
if (r < rMin) {
rMin = r;
dxmin = dxg;
dymin = dyg;
foundBetter = true;
}
} }
} }
} }
} if (foundBetter) {
grid[gridx][gridy] = true; gridx += dxmin;
grid[gridx + dxmin][gridy + dymin] = false; gridy += dymin;
client.x = (gridx + dxmin)*gridSize; }
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();
}); });
*/ */