I want to get the distance to the closest node (if there is a one) from another node that is "looking" at a specific direction.
In the image, the "P" is a node for the player, and the line is the direction to look at. The circe is another node, crossing the direction line.
Getting the distance between 2 nodes is not a problem, but how do I access the closest node exactly crossing my line?
I assume that your sprite has some radius, and crossing my line
denotes that distance from line to sprite center is smaller than radius.
Let playur point is P, direction angle is Fi, so unit direction vector is
dx = cos(fi)
dy = sin(fi)
and line equation is
- x * dy + y * dx + q = 0
where
q = dy * p.x - dx * p.y
To find distance from sprite center to this line, substitute x and y with center.x and center.y
dist = - center.x * dy + center.y * dx + q
So walk through sprite list, check whether sprite is intersected (dist < spriteRadius
) and choose the closest from intersected sprites