nichtgian
nichtgian

Reputation: 113

dda algorithm - raycasting

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);
    }

Upvotes: 2

Views: 4740

Answers (1)

MBo
MBo

Reputation: 80287

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

Upvotes: 1

Related Questions