ballmaze: also bounce off corners
parent
66264ea61c
commit
615aeef4cd
|
|
@ -99,9 +99,8 @@
|
||||||
y = rH/2;
|
y = rH/2;
|
||||||
vx = vy = 0;
|
vx = vy = 0;
|
||||||
ppx = px = x;
|
ppx = px = x;
|
||||||
ppy = py = y
|
ppy = py = y;
|
||||||
|
|
||||||
clearWatch();
|
|
||||||
generateMaze(); // this shows unbuffered progress messages
|
generateMaze(); // this shows unbuffered progress messages
|
||||||
if (settings.cheat && r>1) findRoute(); // not enough memory for r==1 :-(
|
if (settings.cheat && r>1) findRoute(); // not enough memory for r==1 :-(
|
||||||
|
|
||||||
|
|
@ -395,7 +394,7 @@
|
||||||
y += vy/s;
|
y += vy/s;
|
||||||
bounce();
|
bounce();
|
||||||
}
|
}
|
||||||
if (x>sW-cW+r && y>sH-rH+r) win();
|
if (x>sW-cW && y>sH-rH) win();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -407,40 +406,79 @@
|
||||||
function bounce() {
|
function bounce() {
|
||||||
const row = Math.floor(y/sH*rows), col = Math.floor(x/sW*cols),
|
const row = Math.floor(y/sH*rows), col = Math.floor(x/sW*cols),
|
||||||
i = row*cols+col, walls = rooms[i];
|
i = row*cols+col, walls = rooms[i];
|
||||||
|
const left = col*cW,
|
||||||
|
right = (col+1)*cW,
|
||||||
|
top = row*rH,
|
||||||
|
bottom = (row+1)*rH;
|
||||||
|
let bounced = false;
|
||||||
if (vx<0) {
|
if (vx<0) {
|
||||||
const left = col*cW+r;
|
if ((walls&LEFT) && x<=left+r) {
|
||||||
if ((walls&LEFT) && x<=left) {
|
x += (1+e)*(left+r-x);
|
||||||
x += (1+e)*(left-x);
|
|
||||||
const fy = sign(vy)*d*Math.abs(vx);
|
const fy = sign(vy)*d*Math.abs(vx);
|
||||||
vy -= fy/m;
|
vy -= fy/m;
|
||||||
vx = -vx*e;
|
vx = -vx*e;
|
||||||
|
bounced = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const right = (col+1)*cW-r;
|
if ((walls&RIGHT) && x>=right-r) {
|
||||||
if ((walls&RIGHT) && x>=right) {
|
x -= (1+e)*(x+r-right);
|
||||||
x -= (1+e)*(x-right);
|
|
||||||
const fy = sign(vy)*d*Math.abs(vx);
|
const fy = sign(vy)*d*Math.abs(vx);
|
||||||
vy -= fy/m;
|
vy -= fy/m;
|
||||||
vx = -vx*e;
|
vx = -vx*e;
|
||||||
|
bounced = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (vy<0) {
|
if (vy<0) {
|
||||||
const top = row*rH+r;
|
if ((walls&TOP) && y<=top+r) {
|
||||||
if ((walls&TOP) && y<=top) {
|
y += (1+e)*(top+r-y);
|
||||||
y += (1+e)*(top-y);
|
|
||||||
const fx = sign(vx)*d*Math.abs(vy);
|
const fx = sign(vx)*d*Math.abs(vy);
|
||||||
vx -= fx/m;
|
vx -= fx/m;
|
||||||
vy = -vy*e;
|
vy = -vy*e;
|
||||||
|
bounced = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const bottom = (row+1)*rH-r;
|
if ((walls&BOTTOM) && y>=bottom-r) {
|
||||||
if ((walls&BOTTOM) && y>=bottom) {
|
y -= (1+e)*(y+r-bottom);
|
||||||
y -= (1+e)*(y-bottom);
|
|
||||||
const fx = sign(vx)*d*Math.abs(vy);
|
const fx = sign(vx)*d*Math.abs(vy);
|
||||||
vx -= fx/m;
|
vx -= fx/m;
|
||||||
vy = -vy*e;
|
vy = -vy*e;
|
||||||
|
bounced = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (bounced) return;
|
||||||
|
let cx, cy;
|
||||||
|
if ((rooms[i-1]&TOP) || rooms[i-cols]&LEFT) {
|
||||||
|
if ((x-left)*(x-left)+(y-top)*(y-top)<=r*r) {
|
||||||
|
cx = left;
|
||||||
|
cy = top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((rooms[i-1]&BOTTOM) || rooms[i+cols]&LEFT) {
|
||||||
|
if ((x-left)*(x-left)+(bottom-y)*(bottom-y)<=r*r) {
|
||||||
|
cx = left;
|
||||||
|
cy = bottom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((rooms[i+1]&TOP) || rooms[i-cols]&RIGHT) {
|
||||||
|
if ((right-x)*(right-x)+(y-top)*(y-top)<=r*r) {
|
||||||
|
cx = right;
|
||||||
|
cy = top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((rooms[i+1]&BOTTOM) || rooms[i+cols]&RIGHT) {
|
||||||
|
if ((right-x)*(right-x)+(bottom-y)*(bottom-y)<=r*r) {
|
||||||
|
cx = right;
|
||||||
|
cy = bottom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!cx) return;
|
||||||
|
let nx = x-cx, ny = y-cy;
|
||||||
|
const l = Math.sqrt(nx*nx+ny*ny);
|
||||||
|
nx /= l;
|
||||||
|
ny /= l;
|
||||||
|
const p = vx*nx+vy*ny;
|
||||||
|
vx -= 2*p*nx*e;
|
||||||
|
vy -= 2*p*ny*e;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue