I began to study this question with this question , which was partially resolved in the iOS 9.2 SDK.
However, after further study, I realized that this structure still does not work properly.
Thus, a GKGraph can be constructed with nodes ( GKGraphNode and its subclasses) between which you can calculate the costs of the path and the path. A GKGraphNode2D is simply a GKGraphNode that sits and encapsulates its coordinates in a two-dimensional grid. GKGraphNode can be subclassed, and the costToNode(_:) and estimatedCostToNode(_:) methods can be overridden to provide costs between nodes. When these user nodes are added to the graph, the graph should use these methods to determine the least cost route between the linked nodes.
I create nodes, which I will call GeometricNode , where the cost between nodes is just the distance (in a two-dimensional coordinate space) between two nodes. The nodes are connected to all their neighbors in 2D space, including their diagonal neighbors. Ie, the neighboring nodes above, below, to the left and to the right of a particular node are at a distance of 1 , and the diagonally neighboring nodes are sqrt(2) ~ 1.414 . These costs are calculated in the overridden methods costToNode(_:) and estimatedCostToNode(_:) .
Unfortunately, even though these connections are properly configured and costs are properly calculated, GKGraph does not always calculate the correct path when calling findPathFromNode(_:toNode:) .
@import UIKit; @import Foundation; @import GameplayKit; @interface GeometricNode : GKGraphNode2D @end @implementation GeometricNode - (float)estimatedCostToNode:(GKGraphNode *)node { NSAssert(([node isKindOfClass:[GeometricNode class]]), @"Must Use GeometricNode"); return [self geometricDistanceToNode:(GeometricNode *)node]; } - (float)costToNode:(GKGraphNode *)node { NSAssert(([node isKindOfClass:[GeometricNode class]]), @"Must Use GeometricNode"); return [self geometricDistanceToNode:(GeometricNode *)node]; } - (CGFloat)geometricDistanceToNode:(GeometricNode *)node { return sqrtf( powf(self.position[0] - node.position[0], 2) + powf(self.position[1] - node.position[1], 2) ); } @end
And the failed test:
@import XCTest;
ios objective-c gamekit gameplay-kit
Tim camber
source share