ts-raycasting
Version:
Raycasting in typescript
1 lines • 1.98 kB
JavaScript
var raycast=function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="/dist/web/",e(e.s=0)}([function(t,e,r){t.exports=r(1)},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(2),o=r(3);e.castRay=function(t,e,r,o,a){for(var u=Math.sin(a),i=Math.cos(a),c=n.getQuadrant(a),s=Math.floor(e),f=Math.floor(r),h=u/i,l=i/u,M=2&c?1:-1,p=M*h,d=1&c?-1:1,v=d*l,y=2&c?Math.ceil(e):s,w=r+(y-e)*h,P=1&c?f:Math.ceil(r),b=e+(P-r)*l,g=Math.sqrt(Math.pow(y-e,2)+Math.pow(w-r,2)),_=Math.sqrt(Math.pow(b-e,2)+Math.pow(P-r,2)),m=Math.sqrt(Math.pow(M,2)+Math.pow(p,2)),x=Math.sqrt(Math.pow(v,2)+Math.pow(d,2)),O=g<_?0:1,j=g<_?g:_,C=0;o(f,s,t[f][s],j,C);)g<_?(g+=m,y+=M,w+=p,s+=M,j=g,O=0):(_+=x,b+=v,P+=d,f+=d,j=_,O=1),C++;return{dist:0===O?g-m:_-x,side:O,x:1===O?b-v:y-M,y:1===O?P-d:w-p,rot:a,row:f,column:s}},e.castRays=function(t,r,a,u,i,c){void 0===c&&(c=o.defaultConfig);for(var s=function(o){return e.castRay(t,r,a,i,n.normalizeAngle(o))},f=c.fisheye?s:function(t){return n.removeFisheye(s(t),u)},h=(c.center,u-c.fov/2),l=c.fov/c.rayCount,M=[],p=0;p<c.rayCount;)M.push(f(p*l+h)),p++;return M}},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=2*Math.PI,o=.5*Math.PI,a=.75*n;e.getQuadrant=function(t){var e=t<0||t>Math.PI?1:4;return t>a||t<o?2|e:8|e},e.normalizeAngle=function(t){var e=t%n;return e<0?n+e:e},e.removeFisheye=function(t,e){return t.dist=t.dist*Math.cos(e-t.rot),t}},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.defaultConfig={rayCount:256,fov:Math.PI/2,fisheye:!1,center:!0}}]);