如何预先计算场景套件中3D弹丸的径向重力轨迹?

时间:2018-10-22 17:50:01

标签: scenekit projectile

如何为radialGravityField计算3d射弹。我有一个径向重力场。然后我将力作为冲力施加到质量为m的物体朝向重力场中心的方向上。每件事都很简单。但是在对物体施加力之前如何预先计算轨迹路径?我尝试对冲力和非冲力都采用以下方法,但没有成功。

let impulse = false//change this to apply force as impulse
let mass : CGFloat = 100.0
let f: Float = impulse ? 50 : 4000
let direction = SCNVector3(1.0,1.0,0.0)
let force = direction.normalized() * f
let gravityCenter =  gravityNode.position
let position0 = SCNVector3(-1.0,0.5,-3.0)
let dt: Float = 1.0 / 60.0
var points: [SCNVector3] = []

if impulse {
var prevPos = position0
var v0 = force / mass.f//impulse formula, here force is regarded as impulse by scenekit. unit: m/s
var time: Float = dt
for _ in 0..<20 {
    let objectToGravityCenterVector = gravityCenter - prevPos
    let distance = objectToGravityCenterVector.length()
    var aGravity = objectToGravityCenterVector.normalized() * (gravityNode.physicsField?.strength.f)!
    //apply fallof exponent. default is 2.0
    aGravity = aGravity * powf(distance - (gravityNode.physicsField?.minimumDistance.f)!, -(gravityNode.physicsField?.falloffExponent.f)!)

    let u =  aGravity//unit is m/s^2
    let t1 = v0 * time
    let t2 = u * (0.5 * time * time)
    prevPos = position0 + t1 + t2
    let p = prevPos
    points.append(p)
    time = time + dt
}
}else {
var prevPos = position0
var v0 = SCNVector3(0,0,0)
var time: Float = dt
for _ in 0..<20 {
    let objectToGravityCenterVector = gravityCenter - prevPos
    let distance = objectToGravityCenterVector.length()
    var forceGravity = objectToGravityCenterVector.normalized() * (gravityNode.physicsField?.strength.f)! * mass.f
    //apply fallof exponent. default is 2.0
    forceGravity = forceGravity * powf(distance - (gravityNode.physicsField?.minimumDistance.f)!, -(gravityNode.physicsField?.falloffExponent.f)!)

    let netForce = (force + forceGravity)
    let u =  netForce  / mass.f
    let t1 = v0 * time
    let t2 = u * (0.5 * time * time)
    prevPos = position0 + t1 + t2
    let p = prevPos
    points.append(p)
    time = time + dt
}
  }

没有其他字段,例如阻力,摩擦力……仅场景中存在径向-重力场。 Scenekit如何计算施加力/冲量?

0 个答案:

没有答案