dda算法 - 光线投射

时间:2017-10-06 13:42:34

标签: javascript algorithm raycasting

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

我尝试实现daa算法,该算法不仅仅增加1 - >他通过网格并返回确切的位置。

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

1 个答案:

答案 0 :(得分:1)

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

如果你需要一个具有给定边界(或边框边)的交叉点 - 只需用光线方程和边界位置计算它。

如果你想与网格单元格交叉 - 使用像Amanatides-Woo这样的体素化算法