gpt4 book ai didi

swift - 在 Scenekit 中计算到达角度

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:46 27 4
gpt4 key购买 nike

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

enter image description here

我创建了一个函数来返回 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
}

非常感谢任何帮助

最佳答案

您不应该“施加力”,您应该将初始速度设置为您在公式中使用的 V。

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

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

注意:如果这样做,请不要在角度计算中使用“质量”

关于swift - 在 Scenekit 中计算到达角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com