gpt4 book ai didi

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

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

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

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

代码:

                   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)

最佳答案

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

我已经为 Unity 改编了一个解决方案 found here并利用了 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
}

关于ios - 如何将 SCNAction 和冲量力应用于 SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39306792/

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