JavaScript中的线交点?
我正在做一个测试,我需要检测光线是否与墙段相交 我有每个顶点的坐标,有人知道这样做的方法吗 我已经查过了,但是我还没有找到任何可以帮助我的东西 所以我在这里问。以下是您需要知道的代码(使用wasd进行测试):JavaScript中的线交点?,javascript,html,canvas,geometry,raycasting,Javascript,Html,Canvas,Geometry,Raycasting,我正在做一个测试,我需要检测光线是否与墙段相交 我有每个顶点的坐标,有人知道这样做的方法吗 我已经查过了,但是我还没有找到任何可以帮助我的东西 所以我在这里问。以下是您需要知道的代码(使用wasd进行测试): 页面标题 c=document.getElementById(“c”); w=c.宽度; h=c.高度; ctx=c.getContext(“2d”); var tilesize=50; var墙壁=[]; var射线=[]; 射线角=1; 低射线角度=-1; 变量播放器={ x:80
页面标题
c=document.getElementById(“c”);
w=c.宽度;
h=c.高度;
ctx=c.getContext(“2d”);
var tilesize=50;
var墙壁=[];
var射线=[];
射线角=1;
低射线角度=-1;
变量播放器={
x:80,
y:80,
尺寸:10,
速度:1,
目录:0,
rot:0,
旋转速度:0.05,
rotDir:0,
}
变量映射=[
[1,1,1,1,1,1,1,1,1,1,],
[1,0,0,0,0,0,0,0,0,1,],
[1,0,1,1,0,0,0,0,0,1,],
[1,0,1,1,0,0,0,0,0,1,],
[1,0,0,0,0,0,0,0,0,1,],
[1,0,0,0,0,0,0,0,0,1,],
[1,0,0,0,0,0,1,0,0,1,],
[1,0,0,0,0,0,1,0,0,1,],
[1,0,0,0,0,0,1,0,0,1,],
[1,1,1,1,1,1,1,1,1,1,]
]
对于(y=0;y for(i=lowrayangle;ifor(i=lowrayangle;Isom)相当高级的数学,但是:将您重定向到此处接受的答案
for(i=lowrayangle;i<rayangle;i+= 0.1){
var distantCheck = 1000;
var detail = 5;
var memoryX;
var memoryY;
var xLength;
var yLength;
for(var j =0;j<distantCheck;j+=detail){
xLength = player.y - Math.cos(i) * j;
yLength= player.x - Math.sin(i) * j;
memoryY = xLength;
memoryX = yLength;
xLength = xLength -(xLength%tilesize);
yLength = yLength -(yLength%tilesize);
if(map[yLength/tilesize][xLength/tilesize]==1){
break;
}
}
var ray = {
rayAngle : i,
newY : memoryY,
newX : memoryX,
x : player.x,
y : player.y
}
rays.push(ray)
}