【问题标题】:how to change a physics body shape on contact/collision如何在接触/碰撞时改变物理体形
【发布时间】:2015-09-24 22:46:33
【问题描述】:

我创建了一个圆形物理体的精灵。我想在与另一个物体接触/碰撞时将圆形变为矩形物理体。我相信这应该在didBeginContact 中完成。这是我到目前为止所做的事情

ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
self.addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width/2)
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1.2
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.applyImpulse(CGVectorMake(2, -4))

func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody
    var secondBody : SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask  {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }
    else    {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == enemyCategory  {

        //change ball physics shape here
    }
}

【问题讨论】:

    标签: swift sprite-kit


    【解决方案1】:

    请记住,物理体是属于节点的东西。它只是一个属性,它依赖于该节点的存在。您所要做的就是在节点的 PhysicsBody 属性中将一个物体换成另一个物体:

    //inside of didBeginContact, say you want to change firstBody's body to rectangle
    firstBody.node.physicsBody = SKPhysicsBody(rectangleOfSize:...)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2017-03-27
    • 2013-01-28
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多