在 Scenekit 中计算到达角度

Calculating Angle of Reach in Scenekit

我正在尝试计算在 scenekit 场景中击中目标的到达角度。我的数学能力大约是 3/Potato,但在可汗学院学习了几个小时后,我想我已经设法从维基百科中梳理出这个 "Angle of Reach" 公式的逻辑,(我在这个 Stack Exchange answer)...

我为 returns SCNVector3 创建了一个函数,用于向具有给定质量的球体施加力,以击中目标。

据我了解,公式中的±表示有两个可能的角度,我将其称为angleAangleB,分别使用加号和减号。

我想出的功能很接近,但是有两个问题。

  1. angleB 有时会有点偏离标准。
  2. angleA总是功亏一篑。

我可以忍受 angleB 有时有点不对劲。我将其归因于 SceneKit 的物理引擎不是确定性的。有人请告诉我这是否不正确。

但我真的很想知道 angleA 出了什么问题。 AngleA 应该提供更大的角度来产生我正在寻找的高大、宽阔的抛物线轨迹。

这是完整的功能。

func launchVectorFor(
        target: SCNVector3,
        fromPosition origin: SCNVector3, 
        withProjectileMass mass: Float) -> SCNVector3 {

        let g: Float =  -1 * (scene?.physicsWorld.gravity.y)!; //m/s

        // Distance (Displacemet)
        let Dx: Float = (target.x - origin.x)
        let Dy: Float = (target.y - origin.y)

        // Velocity
        // I'm just fudging a Velocity here, using the distance
        // between he origin and the target
        let V: Float= sqrt(Dx * Dx + Dy * Dy)

        // Useful powers
        let V2: Float = pow(V, 2)
        let V4: Float = pow(V, 4)
        let Dx2 = pow(Dx, 2)

        // All the math
        let sqrtInput: Float = V4 - ( g * ( g * Dx2 + 2 * Dy * V2))

        let numeratorPlus: Float = V2 + sqrt(sqrtInput)
        let numeratorMinus: Float = V2 - sqrt(sqrtInput)

        let angleInRadiansA = atan(numeratorPlus / ( g * Dx ) )
        let angleInRadiansB = atan(numeratorMinus / ( g * Dx ) )

        let angleA = angleInRadiansA * (Float(180) / Float.pi)
        let angleB = angleInRadiansB * (Float(180) / Float.pi)
        print("solutions A: \(angleA), -- B \(angleB)")

        // Get X & Y components of force * angle * mass
        let Fx = (V * cos(angleInRadiansA)) * mass
        let Fy = (V * sin(angleInRadiansA)) * mass
        let multiplier: Float = Dx > 0 ? 1 : -1

        return SCNVector3(Fx * multiplier, Fy * multiplier, 0)

    }

这是调用我的函数的代码,以防它有任何相关性...

func handleTap(_ gestureRecognize: UIGestureRecognizer) {
        let launchPosition = convertTouchToWorldCoords(location: gestureRecognize.location(in: self.view))
        let target: SCNNode = (scene?.rootNode.childNode(withName: "target", recursively: false))!

        let ball: SCNNode = createBall(atLocation: launchPosition)
        scene?.rootNode.addChildNode(ball)
        let launchVector = launchVectorFor(target: target.position, fromPosition: launchPosition, withProjectileMass: 0.5)
        ball.physicsBody?.applyForce(launchVector, asImpulse: true)
        print(launchVector)
    }

    func createBall(atLocation location: SCNVector3) -> SCNNode {
        let sphere = SCNSphere(radius: 0.5)
        let shape = SCNPhysicsShape(geometry: sphere, options: [:])
        let ball = SCNNode(geometry: sphere);
        let pBody = SCNPhysicsBody(type: .dynamic, shape: shape)
        pBody.restitution = 0.95
        pBody.mass = 0.5
        ball.physicsBody = pBody
        ball.position = location
        ball.castsShadow = true
        return ball;
    }

    func convertTouchToWorldCoords(location: CGPoint) -> SCNVector3 {
        let hits = scnView?.hitTest(location, options: nil)
        if let hitResult = hits?.first {
            return hitResult.worldCoordinates
        }

        return SCNVector3Zero
    }

非常感谢任何帮助

你不应该"apply a force",你应该将初始速度设置为你在公式中使用的 V。

如果您返回的向量是单位向量(幅值为 1),则将每个分量乘以 V 并直接设置对象的速度。

此外,这些公式没有考虑阻尼(空气摩擦)——所以要准确击中目标,请将球的 linearDampingangularDamping 设置为 0.0(默认值为 0.1) .您会注意到更长的轨迹上的阻尼更大。

注意:如果这样做,请不要在角度计算中使用 "mass"