Reputation: 13641
I'm using a numpy array of 0s and 1s to represent collision in a game map, co-ords set as 1 represent a wall.
What I need to do is a line of sight test from point A to B. So, given the values startX
, startY
, endX
and endY
, I need to get all values from my numpy array in a straight line, and if any of those are a wall (1) then there is no line of sight.
Is there a way to get a line of values from numpy? Or is this probably the wrong way to go about it?
Any pointers?
Upvotes: 1
Views: 2500
Reputation: 41
As said Daniel F, this can be an implementation. I improved, completed and fixed its implementation.
import numpy as np
a = np.ones([7, 7])
a[3, 3] = 1
a[1, :] = 2
a[4, :] = 2
b = 1 - a
b = b.astype(bool)
startX = 3
startY = 0
endX = 3
endY = 5
n = 4 # Steps per unit distance
dxy = (np.sqrt((endX - startX) ** 2 + (endY - startY) ** 2)) * n
i = np.rint(np.linspace(startX, endX, dxy)).astype(int)
j = np.rint(np.linspace(startY, endY, dxy)).astype(int)
has_collision = np.any(b[i, j])
print("Has collision", has_collision)
Upvotes: 2
Reputation: 14399
A simple implementation
coll = np.random.randint(0, 2, (100,100)) # collision matrix
n = 4 # bigger for more accurate, less speed
dxy = (startX - endX + startY - endY) * n
i = np.rint(np.linspace(startX, endX, dxy))
j = np.rint(np.linspace(startY, endY, dxy))
collision = np.any(coll[i, j])
Upvotes: -1