Sprite Kit的bodyAtPoint和bodyInRect返回的值不正确?

5
我创建了一个非常简单的示例代码,只有一个场景,一个20x20像素的精灵节点,在屏幕上的点0.0。当我调用scene.physicsWorld bodyAtPoint时,它会返回这个节点,即使在34x34这样的点。但是在35x35的点处它会返回null。所以基本上从0px到34px在两个轴上的所有点都会返回这个节点,从35px开始,它就不再返回了。如果精灵可见结束在20px 20px,有什么可能的原因吗?bodyInRect的行为也相同。
以下是示例代码:
-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    self.physicsWorld.gravity = CGVectorMake(0, 0);

    SKSpriteNode *node = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20, 20)];
    node.position = CGPointMake(10, 10);

    node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];
    [self addChild:node];

}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    if([self.physicsWorld bodyAtPoint:location])
    {
        NSLog(@"Detected at point: %f %f", location.x, location.y);
    }else{
        NSLog(@"No node at point: %f %f", location.x, location.y);
    }
}
}

以下是控制台日志:

2013-11-09 15:05:52.822 SKTest[43143:70b] Detected at point: 2.000000 1.000000
2013-11-09 15:05:56.274 SKTest[43143:70b] Detected at point: 3.000000 7.000000
2013-11-09 15:05:57.006 SKTest[43143:70b] Detected at point: 3.000000 11.000000
2013-11-09 15:05:58.199 SKTest[43143:70b] Detected at point: 3.000000 12.000000
2013-11-09 15:05:58.918 SKTest[43143:70b] Detected at point: 3.000000 14.000000
2013-11-09 15:05:59.785 SKTest[43143:70b] Detected at point: 3.000000 17.000000
2013-11-09 15:06:00.685 SKTest[43143:70b] Detected at point: 3.000000 20.000000
2013-11-09 15:06:01.565 SKTest[43143:70b] Detected at point: 3.000000 22.000000
2013-11-09 15:06:02.915 SKTest[43143:70b] Detected at point: 3.000000 25.000000
2013-11-09 15:06:04.285 SKTest[43143:70b] Detected at point: 3.000000 30.000000
2013-11-09 15:06:05.387 SKTest[43143:70b] Detected at point: 3.000000 34.000000
2013-11-09 15:06:08.492 SKTest[43143:70b] No node at point: 4.000000 38.000000
2013-11-09 15:06:12.499 SKTest[43143:70b] Detected at point: 4.000000 5.000000
2013-11-09 15:06:13.240 SKTest[43143:70b] Detected at point: 6.000000 5.000000
2013-11-09 15:06:13.881 SKTest[43143:70b] Detected at point: 10.000000 5.000000
2013-11-09 15:06:15.064 SKTest[43143:70b] Detected at point: 12.000000 5.000000
2013-11-09 15:06:16.120 SKTest[43143:70b] Detected at point: 14.000000 5.000000
2013-11-09 15:06:16.873 SKTest[43143:70b] Detected at point: 16.000000 5.000000
2013-11-09 15:06:17.582 SKTest[43143:70b] Detected at point: 18.000000 5.000000
2013-11-09 15:06:18.066 SKTest[43143:70b] Detected at point: 21.000000 5.000000
2013-11-09 15:06:18.966 SKTest[43143:70b] Detected at point: 24.000000 5.000000
2013-11-09 15:06:19.585 SKTest[43143:70b] Detected at point: 31.000000 5.000000
2013-11-09 15:06:20.531 SKTest[43143:70b] Detected at point: 35.000000 5.000000
2013-11-09 15:06:22.581 SKTest[43143:70b] No node at point: 36.000000 4.000000
2013-11-09 15:06:26.560 SKTest[43143:70b] Detected at point: 19.000000 16.000000
2013-11-09 15:06:27.933 SKTest[43143:70b] Detected at point: 20.000000 17.000000
2013-11-09 15:06:29.856 SKTest[43143:70b] Detected at point: 25.000000 19.000000
2013-11-09 15:06:31.487 SKTest[43143:70b] Detected at point: 26.000000 22.000000
2013-11-09 15:06:33.850 SKTest[43143:70b] Detected at point: 29.000000 27.000000
2013-11-09 15:06:35.492 SKTest[43143:70b] Detected at point: 31.000000 29.000000
2013-11-09 15:06:36.854 SKTest[43143:70b] Detected at point: 35.000000 32.000000
2013-11-09 15:06:40.004 SKTest[43143:70b] No node at point: 40.000000 34.000000

是否有可能是苹果的错误,即在nodeAtPointnodeInRect中都存在问题?很难相信。但代码如此简短,我无法看出任何错误可能会在这里发生。 非常感谢您的帮助。


我的解决方案https://dev59.com/D33aa4cB1Zd3GeqPjeYq#27150398 - Roman Bambura
1个回答

0
我对此进行了一些实验。当我放大精灵时,我注意到身体区域似乎比精灵大。我不得不将精灵大小除以1.15,才能使身体矩形与所有4个边上显示的精灵大致匹配。不知道为什么。虽然我从未在游戏中遇到过问题,但也许nodeAtPoint有一个阈值范围?
SKSpriteNode *node = [SKSpriteNode spriteNodetWithColor:[SKColor whiteColor]
                                                   size:CGSizeMake(200, 200)];
node.position = CGPointMake(200, 200);

CGRect bodySize = CGSizeMake(node.size.width / 1.15, node.size.height / 1.15);
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bodySize];
[self addChild:node];

我在Retina 4英寸和非Retina iPad模拟器上进行了测试。


我在另一个论坛上也得到了答案,看起来你是正确的,但不完全是1.15分频器。(对我来说,你的代码也检测到了203px处的节点)。在其他论坛上的回复是,物理体在所有方向上都比实际尺寸大约15.5像素。我再次检查了一下,这个15.5像素似乎是确切的大小,无论我将节点大小设置为20像素还是200像素,它始终比实际尺寸大15.5像素。因此,在200像素的示例中,单击215-检测,单击216未检测到。但是当我将物理体设置为200-15.5时,仍然不正确。(在203px处检测)还有其他想法吗? - DamianD
它似乎与节点缩放或场景缩放有关?为什么点击在215处检测到,而不是在216处,但当我将body设置为185px时,它也在203px处检测到。 - DamianD
BodyAtPoint太麻烦了,我会使用经典方法。https://dev59.com/BXA85IYBdhLWcg3wHv3B - Yusuf Ismail Oktay
使用enumerateBodiesAlongRayStart方法和CGPoint参数可以得到正确的连接点。 - Yusuf Ismail Oktay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接