Note that there are some explanatory texts on larger screens.

plurals
  1. PO2D Raycasting in a grid-based environment
    primarykey
    data
    text
    <p>Hello stackoverflow community. Recently, I've been working on getting a proper raycasting system working. Currently, I've been working entirely in 2D, with a 2D map and player representation. I'm having issues however properly creating a grid-based environment. I think the issue is the way I'm casting rays isn't grid-based at all. I have an example of my issue <a href="https://dl.dropboxusercontent.com/s/pshq0tqu9krnbgv/index.html" rel="nofollow">here</a>. As you can see, the rays appear choppy and malformed. Can anyone give me some insight on how a grid-based system would work? Any help is appreciated, thanks.</p> <p>Here's the full source code(I'm using PIXI.js for my rendering):</p> <pre><code>var world = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1], [1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,1,0,0,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1], [1,0,1,1,1,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1], [1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1], [1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]; var width = world[0].length; var height = world.length; var scale = 8; var posX = 1; var posY = 1; var yaw = 0; var m = 0; var renderer = new PIXI.WebGLRenderer(width * scale,height * scale); //var renderer = new PIXI.WebGLRenderer(320,200); document.body.appendChild(renderer.view); var stage = new PIXI.Stage(0xFFFFFF); var graphics = new PIXI.Graphics(); stage.addChild(graphics); function drawMap() { for(var x = 0;x &lt; width;x++) { for(var y = 0;y &lt; height;y++) { if(world[y][x]) { graphics.beginFill(0xCCCCCC); graphics.drawRect(x * scale, y * scale, scale, scale); graphics.endFill(); } } } } function drawPlayer() { graphics.beginFill(0x000000); graphics.drawRect(posX * scale, posY * scale, 4, 4); graphics.endFill(); graphics.lineStyle(1,0x000000); graphics.moveTo(posX * scale + 2, posY * scale + 2); graphics.lineTo(posX * scale + Math.cos(yaw) * 20 + 2, posY * scale + Math.sin(yaw) * 20 + 2); } function move() { var newX = posX + Math.cos(yaw) * m * 0.3; var newY = posY + Math.sin(yaw) * m * 0.3; m = 0; if(isColliding(newX,newY)) { return; } posX = newX; posY = newY; } function isColliding(x,y) { if(world[Math.floor(y)][Math.floor(x)]) { return true; } return false; } function castRays() { var rayYaw = 0; var rayX = posX; var rayY = posY; var dist = 0; for(var x = -160;x &lt; 160;x++) { rayYaw = x * 0.1875; while(!isColliding(rayX,rayY)) { rayX += Math.cos((rayYaw) * (Math.PI / 180) + yaw); rayY += Math.sin((rayYaw) * (Math.PI / 180) + yaw); if(rayX &lt; 0 || rayX &gt;= width || rayY &lt; 0 || rayY &gt;= height) { break; } } dist = Math.sqrt(Math.pow(posX - rayX,2) + Math.pow(posY - rayY,2)); graphics.lineStyle(1,0x00FFCC); graphics.moveTo(posX * scale + 2, posY * scale + 2); graphics.lineTo(rayX * scale + Math.cos((rayYaw) * (Math.PI / 180)) + 2, rayY * scale + Math.sin((rayYaw) * (Math.PI / 180)) + 2); //drawLine(x + 160,dist); rayX = posX; rayY = posY; } } function drawLine(x,d) { var slice = (32 * d / 160); var start = (100 - (slice/2)); graphics.lineStyle(1,0xCCCCCC); graphics.moveTo(x,start); graphics.lineTo(x,slice); } function main() { drawMap(); move(); drawPlayer(); castRays(); renderer.render(stage); graphics.clear(); } document.onkeydown = checkKey; function checkKey(e) { e = e || window.event; if (e.keyCode == '38') { // up arrow m = 1; } else if (e.keyCode == '40') { // down arrow m = -1; } else if (e.keyCode == '37') { // left arrow yaw -= 0.1; } else if (e.keyCode == '39') { // right arrow yaw += 0.1; } } setInterval(main,1000/30); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload