public bool fillNodeWithVehicle(VehiclePos vehicle, TreeNode node)// should MARK that node as full and as a vehicle { node.setDirection(vehicle.direction); node.setIsWall(false); //node.setLength(Board.vehicleLength); node.setPlayerId(vehicle.playerId); node.setPosition(new Position(vehicle.pos.x, vehicle.pos.y)); //node.setWidth(Board.vehicleWidth); node.isEmpty = false; node.setRadius(vehicle.radius); return(false); }
public bool detectCollision2(VehiclePos vehicle, TreeNode node) { // TODO the math and collision tolerance checks double distance = Math.Sqrt(Math.Pow(node.getPosition().y - vehicle.pos.y, 2) + Math.Pow(node.getPosition().x - vehicle.pos.x, 2)); if (!(distance > (vehicle.radius + node.getRadius()))) { Debug.WriteLine("COLLISION"); return(true); } double x1, x2, x3, x4, y1, y2, y3, y4; x1 = vehicle.pos.x - 25 * Math.Cos(vehicle.direction); x2 = vehicle.pos.x + 25 * Math.Cos(vehicle.direction); y1 = vehicle.pos.y - 25 * Math.Sin(vehicle.direction); y2 = vehicle.pos.y + 25 * Math.Sin(vehicle.direction); x3 = node.getPosition().x - 25 * Math.Cos(node.getDirection()); x4 = node.getPosition().x + 25 * Math.Cos(node.getDirection()); y3 = node.getPosition().y - 25 * Math.Sin(node.getDirection()); y4 = node.getPosition().y + 25 * Math.Sin(node.getDirection()); if (((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)) == 0 || ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)) == 0) { return(false); } double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); double y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); if (x1 >= x2) { if (!(x2 <= x && x <= x1)) { return(false); } } else { if (!(x1 <= x && x <= x2)) { return(false); } } if (y1 >= y2) { if (!(y2 <= y && y <= y1)) { return(false); } } else { if (!(y1 <= y && y <= y2)) { return(false); } } if (x3 >= x4) { if (!(x4 <= x && x <= x3)) { return(false); } } else { if (!(x3 <= x && x <= x4)) { return(false); } } if (y3 >= y4) { if (!(y4 <= y && y <= y3)) { return(false); } } else { if (!(y3 <= y && y <= y4)) { return(false); } } Debug.WriteLine("COLLISION"); return(true); }
/// <summary> /// Let's update the QuadTree data structure with our new vehicle data. /// </summary> /// <param name="vehicle"></param> /// <param name="startNode"></param> /// <returns></returns> public bool insertVehicle(VehiclePos vehicle, TreeNode startNode) { //FIRST must remove vehicle's previous position from the QuadTree if (vehicle.prevPos != null) { TreeNode tempNode = null; tempNode = findNode(vehicle.prevPos, root); emptyNode(tempNode); if (tempNode.hasChildren()) { cleanupLeaves(tempNode.getParent()); } } // Next, insert the vehicle into the tree if (vehicle.pos.x < 0 || vehicle.pos.x > 5000 || vehicle.pos.y < 0 || vehicle.pos.y > 5000) { destroyVehicle(vehicle.playerId); } else { startNode = findNode(vehicle.pos, startNode); if (startNode.isEmpty == false) // check for collision { if (detectCollision2(vehicle, startNode) == true) // TODO check to see if collision is within tolerance { if (startNode.getIsWall() == true) // we need to know if it's a vehicle collision or not { Debug.WriteLine("Wall destroys playerrr!"); destroyVehicle(vehicle.playerId); } else { destroyVehicle(vehicle.playerId); destroyVehicle(startNode.getPlayerId()); emptyNode(startNode); cleanupLeaves(startNode.getParent()); // vehicle collision for both players! how unlucky for the other player :P } } else // no collision, create more leaves { TreeNode newstartNode = new TreeNode(); int child1 = 0, child2 = 0; while (child1 == child2) { startNode.createChildren(); child1 = chooseChild(vehicle.pos, startNode); child2 = chooseChild(startNode.getPosition(), startNode); switch (child2) { case 1: copyNodeToChild(startNode, startNode.getChild1()); newstartNode = startNode.getChild1(); break; case 2: copyNodeToChild(startNode, startNode.getChild2()); newstartNode = startNode.getChild2(); break; case 3: copyNodeToChild(startNode, startNode.getChild3()); newstartNode = startNode.getChild3(); break; case 4: copyNodeToChild(startNode, startNode.getChild4()); newstartNode = startNode.getChild4(); break; } if (child1 != child2) //If the new wall and existing wall are not getting the same child number { switch (child1) { case 1: fillNodeWithVehicle(vehicle, startNode.getChild1()); break; case 2: fillNodeWithVehicle(vehicle, startNode.getChild2()); break; case 3: fillNodeWithVehicle(vehicle, startNode.getChild3()); break; case 4: fillNodeWithVehicle(vehicle, startNode.getChild1()); break; } emptyNode(startNode); // only leaves can have data!!! } else { emptyNode(startNode); // only leaves can have data!!! startNode = newstartNode; } } } } else { fillNodeWithVehicle(vehicle, startNode); // should MARK that node as full and as a vehicle } } return(false); }