Skip to content

Instantly share code, notes, and snippets.

@Podshot
Last active June 18, 2019 18:46
Show Gist options
  • Save Podshot/2b79bda7a549a23a0fee45fe3d68b4c6 to your computer and use it in GitHub Desktop.
Save Podshot/2b79bda7a549a23a0fee45fe3d68b4c6 to your computer and use it in GitHub Desktop.
from operator import mul, add
import math
dims = 4
arr = [[[0 for k in range(dims)] for j in range(dims)] for i in range(dims)] # Convert to numpy array
arr[2][0][2] = 10
pos = (2,3,2)
look_vector = [0,-1,0]
def dot(v1, v2):
return sum(map(mul, v1, v2))
def vec_add(v1, v2):
return list(map(add, v1, v2))
def vec_mult(v1, sclr):
return [i * sclr for i in v1]
#print(vec_mul(pos, 2))
def raycast(pos_vec, look_vec, max_dist=10):
for i in range(max_dist):
ray_pt = vec_add(pos_vec, vec_mult(look_vec, i))
# Assumes square matrix
if ray_pt[0] < 0 or ray_pt[0] >= len(arr):
break
elif ray_pt[1] < 0 or ray_pt[1] >= len(arr[0]):
break
elif ray_pt[2] < 0 or ray_pt[2] >= len(arr[0][0]):
break;
if arr[ray_pt[0]][ray_pt[1]][ray_pt[2]] != 0:
return ray_pt
return None
print(raycast(pos, look_vector))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment