开发者

How can I change this raycasting algorithm to not go diagonally?

开发者 https://www.devze.com 2023-01-08 06:14 出处:网络
// Arg0 - Map, Arg1 - X, Arg2 - Y, Arg3 - Distance, Arg4 - MaxDistance var xx,yy,dist, x1, y1, dir, maxdist, obj, res, map;
// Arg0 - Map, Arg1 - X, Arg2 - Y, Arg3 - Distance, Arg4 - MaxDistance  

var xx,yy,dist, x1, y1, dir, maxdist, obj, res, map;  
map = argument0  
x1 = argument1  
y1 = argument2  
dir = argument3  
maxdist = argument4  
dist = 0

do {  
    dist+=1  
    xx = x1+round(lengthdir_x(dist,dir))  
    yy = y1+round(lengthdir_y(dist,dir))  
} until(block_isSolid(map_get_block(map,xx,yy)) or dist>maxdist)  

if !block_isSolid(map_get_block(map,xx,yy)) {  
    return false  
} else {  
    res = ds_list_create()  
    ds_list_add(res,xx)  
    ds_list_add(res,yy)  
    return res  
}

There's the function. lengthdir_x/y is sin/cos(dir)*dist. Don't yell at me for putting the C tag on there. The languages are very very similar, to the point where I could almost copy this straight in.

Right, formalities done: This c开发者_C百科urrent algorithm will sometimes go diagonally (Where both x and y change by one in either sign), but I wish it not to do this.

EG:

Current: (Where X is the ray casted)

xoooo  
oxooo  
ooxoo  
oooxo  
oooox  

Wanted:

xxooo  
oxxoo  
ooxxo  
oooxx  
oooox  

Make sense?

Please help.


delta is a float and is the x-distance of an virtual secound "ray"(should be around 1.0f - 2.0f, just experimentate)

Delta should not be less than the size of a single pixel in the map.

do {  
    dist+=1  
    xx = x1+round(lengthdir_x(dist,dir))  
    yy = y1+round(lengthdir_y(dist,dir))  
} until(block_isSolid(map_get_block(map,xx,yy)) || block_isSolid(map_get_block(map,xx + delta,yy)) or dist>maxdist)  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号