【问题标题】:SKPhysicsBodies not colliding after addition of SKPhysicsJointLimit添加 SKPhysicsJointLimit 后 SKPhysicsBodies 不发生碰撞
【发布时间】:2016-07-17 15:24:04
【问题描述】:

我目前有两个 SKSpriteNode,我已将 SKPhysicsBodies 添加到其中。当它们没有附加 SKJoint 时,它们会按预期发生碰撞。一旦我添加了 SKPhysicsJoint,它们就会直接通过对方。我正确添加功能的任何关节,但 SKPhysicsJointLimit 仅限制节点可以彼此分开的程度,而不是它们可以接近的程度。我该如何解决这个问题?

这是我用于关节的代码:

let joint = SKPhysicsJointLimit.joint(withBodyA: object1.physicsBody!, bodyB: object2.physicsBody!, anchorA: CGPoint(x: object1.position.x + iconController.position.x, y: object1.position.y + iconController.position.y), anchorB: CGPoint(x: object2.position.x + iconController.position.x, y: object2.position.y + iconController.position.y))
    joint.maxLength = screen.height * 0.4

physicsWorld.add(joint)

两个节点的PhysicsBody:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.allowsRotation = false
self.physicsBody?.friction = 0
self.physicsBody?.mass = 0.1

我已经对 SKPhysicsBody 的上述修改使用不同的值对其进行了测试,它的性能相同。

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    SKPhysicsJoint 对象连接两个物理实体,以便物理世界一起模拟它们。 你也可以使用SKPhysicJointPin:

    销关节允许两个主体独立地围绕 锚点,就像固定在一起一样。

    如果您的对象在使用物理引擎添加SKPhysicsJoint 之前运行良好,因此他们按照您的意愿和设置触发了didBeginContact,我认为您的问题只是一个错误的锚点。尝试添加:

    let skView = self.view as! SKView skView.showsPhysics = true

    到您的场景初始化代码:您将看到物理主体的轮廓,也许您会立即看到问题。

    为了帮助您,我将尝试制作一个配置为相互碰撞的元素示例:

    enum CollisionTypes: UInt32 {
        case Boundaries = 1
        case Element = 2
    }
    
    class GameScene: SKScene,SKPhysicsContactDelegate {
        private var elements = [SKNode]()
        override func didMoveToView(view: SKView) {
            physicsWorld.gravity = CGVector(dx: 0, dy: 0)
            self.physicsWorld.contactDelegate = self
            let boundariesFrame = CGRectMake(20, 20, 200, 400)
            let boundaries = SKShapeNode.init(rect: boundariesFrame)
            boundaries.position = CGPointMake(350,150)
            let boundariesBody = SKPhysicsBody.init(edgeLoopFromRect: boundariesFrame)
            boundariesBody.dynamic = false
            boundariesBody.categoryBitMask = CollisionTypes.Boundaries.rawValue
            boundariesBody.contactTestBitMask = CollisionTypes.Element.rawValue
            boundaries.physicsBody = boundariesBody
            addChild(boundaries)
            for index in 0..<5 {
                let element = SKShapeNode(circleOfRadius: 10)
                let body = SKPhysicsBody(circleOfRadius: 10)
                body.linearDamping = 0
                // body.mass = 0
                body.dynamic = true
                body.categoryBitMask = CollisionTypes.Element.rawValue
                body.contactTestBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
                body.collisionBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
                element.physicsBody = body
                element.position = CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index))
                elements.append(element)
                addChild(element)
            }
        }
    }
    

    希望它可以帮助您找到您的问题。

    【讨论】:

    • 我之前已经按照您的建议通过展示物理来调查可能发生的情况;然而,展示物理只展示了我认为它应该展示的东西。我的两个锚都正确定位在每个节点的中间,用一条线表示它们之间的连接。同样,关节按我想要的方向向外工作,但是当节点向内相互移动时,它们根本不会发生碰撞。
    • 所以你的对象在添加 SKPhysicsJoint 之前就已经发生了碰撞?我问它是因为在您的问题中您没有报告任何代码。
    • 是的,它们相撞得很好。事实上,即使添加了关节,它们也会与除彼此之外的所有节点正常碰撞。我将在我的问题中添加一些代码。
    • 好的,我会阅读它,之后我会尝试更新我的答案。同时,我添加了一个示例来帮助您了解对象之间的碰撞。
    • @AlessandroOrnano 你能帮我解决这个问题吗stackoverflow.com/questions/44241484/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多