2

I started a project using the raycasting technique GitHub Project To find the length of the ray (distance from players pos to wall) I just increment by one. But there are several problems with that, its time consuming, inaccurate & will be difficult for texturing.

I tried to implement the daa algorithm, which doesnt just increments by 1 -> he goes through the grids and returns exact positions.

http://www.geeksforgeeks.org/dda-line-generation-algorithm-computer-graphics/

Has anyone experience with that or any tips?

No algorithm way:

for(let resolution = 0; resolution < display.width / 2; resolution++){ //every 2nd px gets scanned
        let ray = this.pov + (-this.fov / 2 + this.fov / (display.width / 2) * resolution);
        let distance = 0, hit = false;

        /*ugly way of raycasting!*/
        do{
            let x = this.x + distance * Math.cos(ray * (Math.PI / 180));
            let y = this.y + distance * Math.sin(ray * (Math.PI / 180));
            if(map[Math.floor(x / block)][Math.floor(y / block)]){
                distance = Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
                hit = true
            }
            distance += 1;
        }while(!hit);
        distance = convert / distance;
        canvas.fillStyle = "#fff";
        canvas.fillRect(resolution * 2, display.height / 2 - distance / 2, 2, distance);
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
nichtgian
  • 113
  • 1
  • 4
  • 13
  • ray line/plane/triangle/quad intersection is `O(1)` operation derive the equation (or google it) and just use it no loops are needed for this. – Spektre Oct 06 '17 at 13:54
  • how is this related to brainfuck? – Aemyl Apr 05 '18 at 07:11
  • @Aemyl in any way, I am still trying to fix that – nichtgian Apr 08 '18 at 17:05
  • the brainfuck tag is for questions related to the programming language brainfuck which consists of `+-<>.,[]` – Aemyl Apr 08 '18 at 17:24
  • @Aemyl lul you are totally wrong. Total disaster. Brainfuck is what you get when youre dump, as I am. Of course. What the hell is a programming language? Do you mean computer? +- Is not possible by the way. Total nonsense. Just write - or open a question on Stackoverflow, so maybe someone can help you. Like me. Thanks – nichtgian Apr 08 '18 at 17:34
  • 1
    @crackhead420 don't know if you are trolling but if you don't believe me, check https://en.wikipedia.org/wiki/Brainfuck or just hover over the brainfuck tag to see the tag description – Aemyl Apr 08 '18 at 17:38
  • 1
    I've removed the BrainFuck tag. The tag is about the programming language, not what you think @crackhead420. – Zoe Apr 08 '18 at 17:40

1 Answers1

1

You don't need DDA or Bresenham algorithm to find intersections of the ray with walls.

If you need one intersection with given border (or box edges) - just calculate it with ray equation and border position.

If you want to get intersections with grid cells - use voxelization algorithm like Amanatides-Woo

MBo
  • 77,366
  • 5
  • 53
  • 86
  • What is the *Ray Equation*? I'm currently planning to use a DDA generator that yields coordinates, then check each one is in empty space before moving to the next. Is there a better way (I'm implementing a laser range finder for a robot game). – Carl Smith Aug 13 '18 at 18:09
  • 1
    Parametric ray equation: `x=x0+dx*t, y=y0+dy*t` where (x0,y0) is base point, (dx,dy) is direction vector. DDA is intended to generate pixels describing line, not for intersection seeking. I described better way - find intersection solving equation. – MBo Aug 13 '18 at 18:15
  • @MBo I skimmed through the paper you referenced, and the proposed technique matched my view on a DDA solution. However, my reference for this is not academical and I might be wrong. Could you please enlighten me with the difference between voxelization and DDA techniques for ray tracing problems? – Smartskaft2 Feb 08 '22 at 21:10