如何将 SCNAction 和冲量力应用于 SCNNode

How to apply a SCNAction & An Impulse Force to a SCNNode

嘿,我有一个通过施加力而移动的球。我想让它做的基本上是在它通过空气到达目的地时,重力作用在它身上的因果效应。基本上,当 "move to" 动作正在播放时,重力不会产生影响,所以它不会慢慢落到地面,而是移动到它的最终位置,然后当 "move to" 动作停止时,它会直接落下。对场景中的重力做。

我想让球以弧形抛出并落在目标上吗?

代码:

                   func CreateBall() {
    let BallScene = SCNScene(named: "art.scnassets/Footballs.dae")
    Ball = BallScene!.rootNode.childNodeWithName("Armature", recursively: true)! //the Amature/Bones
    Ballbody = BallScene!.rootNode.childNodeWithName("Ball", recursively: true)!

    let collisionCapsuleRadius3 = CGFloat(0.01) // Width of physicsBody
    let collisionCapsuleHeight3 = CGFloat(0.01) // Height of physicsBody
    Ball.position = SCNVector3Make(Guy.position.x, Guy.position.y, Guy.position.z)
    Ball.scale = SCNVector3Make(5, 5, 5)
    Ball.rotation = SCNVector4Make(0.0,0.0,0.0,0.0) // x,y,z,w

    Ball.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius3, height: collisionCapsuleHeight3), options:nil))
    Ball.physicsBody?.affectedByGravity = true
    Ball.physicsBody?.friction = 1 //
    Ball.physicsBody?.restitution = 0 //bounceness of the object. 1.0 will boounce forever
    Ball.physicsBody?.angularDamping = 1 // ability to rotate
    Ball.physicsBody?.mass = 1
    Ball.physicsBody?.rollingFriction = 1
    Ball.physicsBody!.categoryBitMask = BitmaskCollision4
    Ball.physicsBody?.contactTestBitMask = BitmaskCollision3 //| BitmaskCollision2
    Ballbody.physicsBody?.collisionBitMask = BitmaskCollision2 | BitmaskCollision3 | BitmaskCollision//| BitmaskCollision2

    scnView.scene!.rootNode.addChildNode(Ball)
    scnView.scene!.rootNode.addChildNode(Ballbody)


    }
    CreateBall()

现在这就是奇迹发生的地方:

                   scnView.scene!.physicsWorld.gravity = SCNVector3(x: 0, y: -9.8, z: 0)

                    let location = SCNVector3(Guy2.presentationNode.position.x, 0.0, Guy2.presentationNode.position.z + Float(50) )
                    let moveAction = SCNAction.moveTo(location, duration: 2.0)
                    Ball.runAction(SCNAction.sequence([moveAction]))


                    let forceApplyed = SCNVector3(x: 0.0, y: 100.0 , z: 0.0)
                     Ball.physicsBody?.applyForce(forceApplyed, atPosition: Ball.presentationNode.position, impulse: true)

结合使用 SCNAction 和物理学不起作用,您需要使用其中之一。使用物理学,您可以计算出将节点推向目标所需的确切力。

我已经为 Unity found here and utilised an SCNVector3 extension 改编了一个解决方案,这使得一些计算更容易。

基本上你传入一个你想抛出的SCNNode,一个SCNVector3作为目标,一个angle(以弧度为单位)你希望节点被抛出.然后此函数将计算出达到目标所需的力。

func shootProjectile() {
    let velocity = ballisticVelocity(ball, target: target.position, angle: Float(0.4))
    ball.physicsBody?.applyForce(velocity, impulse: true)
}

func ballisticVelocity(projectile:SCNNode, target: SCNVector3, angle: Float) -> SCNVector3 {
        let origin = projectile.presentationNode.position
        var dir = target - origin       // get target direction
        let h = dir.y                   // get height difference
        dir.y = 0                       // retain only the horizontal direction
        var dist = dir.length()         // get horizontal distance
        dir.y = dist * tan(angle)       // set dir to the elevation angle
        dist += h / tan(angle)          // correct for small height differences
        // calculate the velocity magnitude
        let vel = sqrt(dist * -scene.physicsWorld.gravity.y / sin(2 * angle))
        return dir.normalized() * vel * Float(projectile.physicsBody!.mass)
}

同样重要的是将physicsBody的damping设置为0,否则会受到空气阻力的影响

我不会假装知道它到底是如何工作的,但维基百科上有文章解释了它背后的所有数学原理。

更新

自从使用上面的代码后,我发现它并不总是有效,尤其是当原点和目标的高度不同时。从同一个论坛来看,这个功能似乎更可靠。

func calculateBestThrowSpeed(origin: SCNVector3, target: SCNVector3, timeToTarget:Float) -> SCNVector3 {

    let gravity:SCNVector3 = sceneView.scene!.physicsWorld.gravity

    let toTarget = target - origin
    var toTargetXZ = toTarget
    toTargetXZ.y = 0

    let y = toTarget.y
    let xz = toTargetXZ.length()

    let t = timeToTarget
    let v0y = y / t + 0.5 * gravity.length() * t
    let v0xz = xz / t


    var result = toTargetXZ.normalized()
    result *= v0xz
    result.y = v0y

    return result
}