我在屏幕上有两个sks节点。如何最好地计算它们之间的距离(即直线距离,不需要向量等)?
我已经用谷歌和这里的搜索了一下,但找不到相关内容(stackoverflow上关于sprite kit的主题并不多)。
我在屏幕上有两个sks节点。如何最好地计算它们之间的距离(即直线距离,不需要向量等)?
我已经用谷歌和这里的搜索了一下,但找不到相关内容(stackoverflow上关于sprite kit的主题并不多)。
这是一个可以为您完成该操作的函数,代码示例来自苹果公司的Adventure游戏:
CGFloat SDistanceBetweenPoints(CGPoint first, CGPoint second) {
return hypotf(second.x - first.x, second.y - first.y);
}
要从您的代码中调用此函数:
CGFloat distance = SDistanceBetweenPoints(nodeA.position, nodeB.position);
另一种快速的方法,由于我们处理的是距离,我添加了abs()函数以使结果始终为正数。
extension CGPoint {
func distance(point: CGPoint) -> CGFloat {
return abs(CGFloat(hypotf(Float(point.x - x), Float(point.y - y))))
}
}
joshd 和 Andrey Gordeev 都是正确的,其中 Gordeev 的解决方案阐述了 hypotf 函数的作用。
但是平方根函数是一个很耗费资源的函数。如果你只需要相对距离,可以跳过它;只有当你需要知道实际距离时才需要使用它。你可能想知道哪个精灵最靠近、最远,或者是否有任何精灵在半径范围内。在这些情况下,只需比较距离的平方。
- (float)getDistanceSquared:(CGPoint)p1 and:(CGPoint)p2 {
return pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2);
}
要在SKScene子类的update:方法中使用此功能来计算任何精灵是否在视图中心的半径范围内:
-(void)update:(CFTimeInterval)currentTime {
CGFloat radiusSquared = pow (self.closeDistance, 2);
CGPoint center = self.view.center;
for (SKNode *node in self.children) {
if (radiusSquared > [self getDistanceSquared:center and:node.position]) {
// This node is close to the center.
};
}
}
(p2.x-p1.y)*(p2.x-p1.y)
可以大幅提高性能,而不是使用pow
,因为pow
是用于接收两个双精度浮点数的。 - Timpowf
,它接受两个浮点数。 - AustinSwift:
extension CGPoint {
/**
Calculates a distance to the given point.
:param: point - the point to calculate a distance to
:returns: distance between current and the given points
*/
func distance(point: CGPoint) -> CGFloat {
let dx = self.x - point.x
let dy = self.y - point.y
return sqrt(dx * dx + dy * dy);
}
}
var theDistance = point1.distance(point2)
。 - bluegreen勾股定理:
- (float)getDistanceBetween:(CGPoint)p1 and:(CGPoint)p2 {
return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}