1

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

我试图让球以弧线投掷并落在目标上?

代码:

                   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)
4

1 回答 1

3

将 SCNActions 和物理结合起来是行不通的,你需要使用其中的一个。使用物理学,您可以计算将节点推向目标所需的确切力。

我已经为这里找到的 Unity 调整了一个解决方案,并使用了一个SCNVector3 扩展,这使得一些计算变得更加容易。

基本上你传入一个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的 设置为0也很重要damping,否则会受到空气阻力的影响。

我不会假装确切地知道它是如何工作的,但是 Wikipedia 有文章解释了它背后的所有数学。

更新

由于使用上面的代码,我注意到它并不总是有效,尤其是当原点和目标的高度不同时。从同一个论坛这个功能似乎更可靠。

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
}
于 2016-09-03T13:50:44.470 回答