dda算法——光线投射

dda algorithm - raycasting

我开始了一个使用光线投射技术的项目GitHub Project 为了找到射线的长度(从玩家 pos 到墙的距离),我只增加了一个。但是这有几个问题,它耗时,不准确并且很难进行纹理化。

我尝试实现 daa 算法,它不只是递增 1 -> 他遍历网格和 returns 精确位置。

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

有没有人有这方面的经验或任何提示?

无算法方式:

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

您不需要 DDA 或 Bresenham 算法来找到光线与墙壁的交点。

如果您需要与给定边界(或框边缘)的交点 - 只需使用射线方程和边界位置进行计算即可。

如果你想获得与网格单元的交点 - 使用像

这样的体素化算法