因此,对于我正在开发的应用程序,我有两个碰撞的多维数据集。我以标准方式对此进行检查。该应用程序会告诉我它们何时碰撞到我的“didBeginContact”方法中。

-(void)didBeginContact:(SKPhysicsContact *)contact {
    if (contact.bodyA.categoryBitMask == WALL_CATEGORY && contact.bodyB.categoryBitMask == CHARACTER_CATEGORY) {
        CGPoint point = contact.contactPoint;
    }
}

所以我知道发生碰撞的地方,但是因为它是两个正方形,所以它可以在包括角的侧面的任何点上。那么,如何检查左侧/右侧/顶部/底部的碰撞是否排他呢?

编辑:正确答案:可能不是最干净的方法,但它可以工作。希望它会在将来对某人有所帮助。
m_lNode = [SKNode node];
m_lNode.position = CGPointMake(-(CHARACTER_SIZE / 2), 0);
m_lNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)];
m_lNode.physicsBody.allowsRotation = NO;
m_lNode.physicsBody.usesPreciseCollisionDetection = YES;
m_lNode.physicsBody.categoryBitMask = CHARACTER_L_CATEGORY;

m_rNode = [SKNode node];
m_rNode.position = CGPointMake((CHARACTER_SIZE / 2), 0);
m_rNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, m_character.size.height)];
m_rNode.physicsBody.allowsRotation = NO;
m_rNode.physicsBody.usesPreciseCollisionDetection = YES;
m_rNode.physicsBody.categoryBitMask = CHARACTER_R_CATEGORY;

m_tNode = [SKNode node];
m_tNode.position = CGPointMake(0, (CHARACTER_SIZE / 2));
m_tNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width , 1)];
m_tNode.physicsBody.allowsRotation = NO;
m_tNode.physicsBody.usesPreciseCollisionDetection = YES;
m_tNode.physicsBody.categoryBitMask = CHARACTER_T_CATEGORY;

m_bNode = [SKNode node];
m_bNode.position = CGPointMake(0, -(CHARACTER_SIZE / 2));
m_bNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(m_character.size.width, 1)];
m_bNode.physicsBody.allowsRotation = NO;
m_bNode.physicsBody.usesPreciseCollisionDetection = YES;
m_bNode.physicsBody.categoryBitMask = CHARACTER_B_CATEGORY;

[m_character addChild:m_tNode];
[m_character addChild:m_bNode];
[m_character addChild:m_lNode];
[m_character addChild:m_rNode];

-(void)didBeginContact:(SKPhysicsContact *)contact {
    if (contact.bodyA.categoryBitMask == WALL_CATEGORY) {
        switch (contact.bodyB.categoryBitMask) {
            case CHARACTER_T_CATEGORY:
                NSLog(@"Top");
                m_isHitTop = true;
                break;
            case CHARACTER_B_CATEGORY:
                NSLog(@"Bottom");
                m_isHitBottom = true;
                break;
            case CHARACTER_L_CATEGORY:
                NSLog(@"Left");
                m_isHitLeft = true;
                break;
            case CHARACTER_R_CATEGORY:
                NSLog(@"Right");
                m_isHitRight = true;
                break;
        }
    }
}

添加了一些相关代码。这是我的代码,因此除其他外还有变量,但是您应该能够弄清楚。

最佳答案

我认为,确定联系所涉及的哪一方(上,左,下,右,右)的最佳方法是:

  • 计算上,下,左和右两侧(例如,如果您有一个方形的 Sprite )的居中十字所留下的点。

    ios - SpriteKit-确定与正方形相撞的一面-LMLPHP
    let halfWidth = self.frame.width/2
    let halfHeight = self.frame.height/2
    let down = CGPointMake(self.frame.origin.x+halfWidth,self.frame.origin.y)
    let up = CGPointMake(self.frame.origin.x+halfWidth,self.frame.origin.y+self.frame.size.height)
    let left = CGPointMake(self.frame.origin.x,self.frame.origin.y+halfHeight)
    let right = CGPointMake(self.frame.origin.x+self.frame.size.width,self.frame.origin.y+halfHeight)
    
  • 计算每个点两侧的contactPoint和之间的距离(上下左右)

  • 这个小功能可以完成此步骤:
    func getDistance(p1:CGPoint,p2:CGPoint)->CGFloat {
        let xDist = (p2.x - p1.x)
        let yDist = (p2.y - p1.y)
        return CGFloat(sqrt((xDist * xDist) + (yDist * yDist)))
    }
    

    之后,与所计算出的最小距离有关的点是最靠近contactPoint的边点。

    这种方法有什么优点:
  • 您无需绘制僵尸或幽灵节点以及相关的物理物体
  • 此方法在动态可变CGPath中也有效,不仅适用于
    已知的矩形边界
  • 是快速的几行代码,如果添加其他几行,则可以
    能够确定对角线并使您的精度更高
    算法。
  • 关于ios - SpriteKit-确定与正方形相撞的一面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27830093/

    10-13 04:04