Reworked sprite math to be simple.

This commit is contained in:
Martin Haye
2013-10-19 11:46:30 -07:00
parent e17a94bffc
commit 724e6dc39e

View File

@@ -373,19 +373,35 @@ function renderSprites() {
var dx = sprite.x + 0.5 - player.x;
var dy = sprite.y + 0.5 - player.y;
// Use rotation matrix
var sinT = Math.sin(-playerAngle());
var cosT = Math.cos(-playerAngle());
var rx = ((sprite.x + 0.5 - player.x) * cosT) - ((sprite.y + 0.5 - player.y) * sinT);
var ry = ((sprite.x + 0.5 - player.x) * sinT) + ((sprite.y + 0.5 - player.y) * cosT);
var sqDist2 = rx*rx + ry*ry;
var dist2 = Math.sqrt(sqDist2);
// distance to sprite
var dist = Math.sqrt(dx*dx + dy*dy);
// sprite angle relative to viewing angle
var spriteAngle = Math.atan2(dy, dx) - playerAngle();
dist *= Math.cos(spriteAngle);
if (options & 1)
dist = dist2;
//console.log("Sprite " + visibleSprites[i].index + ": dist=" + dist + ", dist2=" + dist2);
// size of the sprite
var size = viewDist / (Math.cos(spriteAngle) * dist);
var size = viewDist / dist;
if (size <= 0) continue;
// x-position on screen
var x = Math.tan(spriteAngle) * viewDist;
if (options & 1)
x = ry / dist2 * 252 / 0.38268343236509034;
img.style.left = (screenWidth/2 + x - size/2) + "px";
@@ -398,8 +414,12 @@ function renderSprites() {
img.style.width = size + "px";
img.style.height = size + "px";
var blockDist = dbx*dbx + dby*dby;
var blockDist = dx*dx + dy*dy;
if (options & 1)
blockDist = sqDist2;
var zIndex = -Math.floor(blockDist*1000)
if (options & 2)
zIndex = parseInt(size);
img.style.zIndex = zIndex;
console.log("visible sprite " + sprite.index + ": blockDist=" + blockDist +
@@ -741,16 +761,6 @@ function wallCalc(x, dist, bDir1, bDir2, bStep2)
}
return [lineHeight, bWallX]
/* No-log algorithm
var wInvDir = uword(65536 / bDir1);
var dist2 = umul_ww_w(dist, wInvDir);
var bWallX = umul_wb_b(dist2, bDir2);
if (bStep2 < 0)
bWallX = 255 - bWallX;
var lineHeight = Math.floor(63 * 256 / dist2);
return [lineHeight, bWallX]
*/
}
// Cast one ray from the player's position through the map until we hit a wall.
@@ -955,14 +965,17 @@ function drawStrip(stripIdx, lineData)
strip.oldStyles.clip = styleClip;
}
var dwx = lineData.xWallHit - player.x;
var dwy = lineData.yWallHit - player.y;
var dwx = lineData.xWallHit + 0.5 - player.x;
var dwy = lineData.yWallHit + 0.5 - player.y;
var wallDist = dwx*dwx + dwy*dwy;
strip.style.zIndex = -Math.floor(wallDist*1000);
var zIndex = -Math.floor(wallDist*1000)
if (options & 2)
zIndex = lineData.height;
strip.style.zIndex = zIndex;
if (stripIdx == debugRay)
console.log("wallDist=" + wallDist + ", zIndex=" + (-Math.floor(wallDist*1000)));
console.log("wallDist=" + wallDist + ", zIndex=" + zIndex);
}
function move() {
@@ -984,6 +997,10 @@ function move() {
player.x = newX; // set new position
player.y = newY;
// Turn off multi-step maneuvers.
player.speed = 0;
player.dir = 0;
}
function isBlocking(x,y) {