Hello there, I currently have a tightly packed grid of entities as shown below
Here is a picture (add a .png to the link) http://puu.sh/iTQ5P/b9b7caf84a
Each block is an entity with type “node”. Each entity also has a boolean field called “traversable”. Every entity is marked traversable except the grey blocks.
I want to check if there is a straight line between the two red nodes that doesn’t intersect a grey block. My idea for doing this was to create an entity in the world, rapidly move it from the first red block to the second red block, and checking for collisions with "node"s. If a “node” is collided with, then a second check will be performed to see if that node is traversable. If a non-traversable node is detected, then there must be a grey block between the two red blocks.
function detectStraightLine(inputNodeA:Node, inputNodeB:Node):Boolean {
// create two temporary nodes
var nodeA:Node = new Node(inputNodeA.x, inputNodeA.y, 0, 0);
var nodeB:Node = new Node(inputNodeB.x, inputNodeB.y, 0, 0);
// move A towards B and check for collision with nodes
while ((nodeA.x != nodeB.x) || (nodeA.y != nodeB.y)) {
nodeA.moveTowards(nodeB.x, nodeB.y, 5);
if (nodeA.collide("node", nodeA.x, nodeA.y)) {
trace("collision detected");
// check if collided node is traversable
return false;
}
}
trace("straight line found");
return true;
}
I have done tests and the function successfully moves nodeA every iteration of the while loop by 5 units downwards. However I am not detecting any collisions at all with this method. Am I doing something fundamentally wrong?