我开始了一个项目,使用光线投射技术GitHub项目来寻找射线的长度(从球员到墙壁的距离),我只是增加了一个。但是有几个问题,它的时间消耗,不准确&将是很难纹理。
我尝试实现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);
}发布于 2017-10-06 14:06:25
您不需要DDA或Bresenham算法来找到光线与墙壁的交点。
如果你需要一个与给定边框(或框边)的交点,就用射线方程和边框位置来计算它。
如果您想获得与网格单元格的交叉点--使用像阿马马提德斯这样的体素化算法
https://stackoverflow.com/questions/46607152
复制相似问题