| 1 | /** |
| 2 | * Shared camera-plane projection (FOV 90°). |
| 3 | * Rays use dir + plane*camX (camX in [-1,1]); depths are perpendicular |
| 4 | * (along forward), so walls/floor/sprites/z-buffer stay consistent and |
| 5 | * fish-eye from equal-angle rays + Euclidean depth is avoided. |
| 6 | */ |
| 7 | var Camera = |
| 8 | { |
| 9 | /** Half FOV in radians (π/4 → full FOV 90°). */ |
| 10 | HALF_FOV: Math.PI / 4, |
| 11 | |
| 12 | tanHalfFov: function () |
| 13 | { |
| 14 | return Math.tan(this.HALF_FOV); |
| 15 | }, |
| 16 | |
| 17 | forward: function (rot) |
| 18 | { |
| 19 | return { x: Math.cos(rot), y: Math.sin(rot) }; |
| 20 | }, |
| 21 | |
| 22 | /** |
| 23 | * Camera plane vector (perpendicular to forward, length = tan(halfFov)). |
| 24 | * World +Y is south; left-handed screen: +plane = screen right. |
| 25 | */ |
| 26 | plane: function (rot) |
| 27 | { |
| 28 | var t = this.tanHalfFov(); |
| 29 | var fx = Math.cos(rot); |
| 30 | var fy = Math.sin(rot); |
| 31 | // right = (-fy, fx) so that forward×right points "up" in XY… |
| 32 | // For +X east +Y south, facing +X: right should be +Y (south) = screen right when looking east? |
| 33 | // Looking east (+X): screen right = south (+Y). right=(-fy,fx)=(0,1). plane = right * tan. |
| 34 | return { x: -fy * t, y: fx * t }; |
| 35 | }, |
| 36 | |
| 37 | /** camX in [-1,1] from screen x / column index. */ |
| 38 | camXFromScreen: function (sx, screenW) |
| 39 | { |
| 40 | return (2 * sx / screenW) - 1; |
| 41 | }, |
| 42 | |
| 43 | camXFromColumn: function (col, cols) |
| 44 | { |
| 45 | return -1 + (2 * (col + 0.5) / cols); |
| 46 | }, |
| 47 | |
| 48 | /** Unnormalized ray direction for camera-plane casting. */ |
| 49 | rayDir: function (rot, camX) |
| 50 | { |
| 51 | var f = this.forward(rot); |
| 52 | var p = this.plane(rot); |
| 53 | return { x: f.x + p.x * camX, y: f.y + p.y * camX }; |
| 54 | }, |
| 55 | |
| 56 | /** Angle of camera-plane ray (for World.rayCast which takes a radian). */ |
| 57 | rayAngle: function (rot, camX) |
| 58 | { |
| 59 | var d = this.rayDir(rot, camX); |
| 60 | return Math.atan2(d.y, d.x); |
| 61 | }, |
| 62 | |
| 63 | /** |
| 64 | * Perpendicular depth of a world point relative to the camera. |
| 65 | * Positive in front; use for sprite size, z-buffer, occlusion. |
| 66 | */ |
| 67 | perpDepth: function (worldX, worldY, playerX, playerY, rot) |
| 68 | { |
| 69 | var f = this.forward(rot); |
| 70 | return (worldX - playerX) * f.x + (worldY - playerY) * f.y; |
| 71 | }, |
| 72 | |
| 73 | /** |
| 74 | * Perp depth from Euclidean ray length and the ray's camX |
| 75 | * (euclid * cos(atan(camX*tanHalf)) = euclid / length(rayDir) * …). |
| 76 | * For a hit along rayDir: perp = euclid * (forward·normalize(rayDir)). |
| 77 | */ |
| 78 | perpFromEuclid: function (euclidDist, rot, camX) |
| 79 | { |
| 80 | var d = this.rayDir(rot, camX); |
| 81 | var len = Math.sqrt(d.x * d.x + d.y * d.y); |
| 82 | if (len < 1e-8) |
| 83 | { return euclidDist; } |
| 84 | var f = this.forward(rot); |
| 85 | var cosA = (d.x * f.x + d.y * f.y) / len; |
| 86 | return euclidDist * cosA; |
| 87 | }, |
| 88 | |
| 89 | /** |
| 90 | * Project world point to screen X (pixels) and perp depth. |
| 91 | * Returns null if behind camera. |
| 92 | */ |
| 93 | projectSprite: function (worldX, worldY, playerX, playerY, rot, screenW) |
| 94 | { |
| 95 | var depth = this.perpDepth(worldX, worldY, playerX, playerY, rot); |
| 96 | if (depth <= 0.05) |
| 97 | { return null; } |
| 98 | var f = this.forward(rot); |
| 99 | var t = this.tanHalfFov(); |
| 100 | // screen-right axis (= plane / tanHalf) |
| 101 | var rx = -f.y; |
| 102 | var ry = f.x; |
| 103 | var lat = (worldX - playerX) * rx + (worldY - playerY) * ry; |
| 104 | var screenX = (screenW / 2) * (1 + (lat / depth) / t); |
| 105 | return { screenX: screenX, depth: depth, lat: lat }; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | if (typeof module !== "undefined" && module.exports) |
| 110 | { module.exports = Camera; } |