【发布时间】:2022-01-24 05:50:51
【问题描述】:
在ARkit中,我有子弹节点和目标节点两个节点,
子弹节点在下方
let body = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: bullet, options: nil))
body.isAffectedByGravity = false
bullet.physicsBody = body
bullet.physicsBody?.applyForce(SCNVector3(orientation.x*power, orientation.y*power, orientation.z*power), asImpulse: true)
bullet.physicsBody?.categoryBitMask = BitMaskCategory.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = BitMaskCategory.target.rawValue
bullet.physicsBody?.collisionBitMask = BitMaskCategory.target.rawValue
目标节点在下方
let boundingSphere = targetNode!.boundingSphere;
let radius = boundingSphere.radius;
let center = boundingSphere.center;
let sphereShape = SCNSphere(radius: CGFloat(radius));
var shape = SCNPhysicsShape(geometry: sphereShape, options: [SCNPhysicsShape.Option.scale : targetNodeScale * 1.1])
targetNode?.physicsBody = SCNPhysicsBody(type: .dynamic,shape: shape)
targetNode?.physicsBody?.isAffectedByGravity = false
targetNode?.physicsBody?.categoryBitMask = BitMaskCategory.target.rawValue
targetNode?.physicsBody?.contactTestBitMask = BitMaskCategory.bullet.rawValue | BitMaskCategory.target.rawValue
targetNode?.physicsBody?.collisionBitMask = BitMaskCategory.target.rawValue | BitMaskCategory.bullet.rawValue
targetNode?.setValue(0, forKey: "hitCount")
我发现当这两个节点相互碰撞时,下面的方法几乎同时连续调用了两次,但应该只发生一次。
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
}
有时会发生,有时没有,所以我认为可能应该添加一些碰撞精度,有人知道如何做那部分吗?谢谢
【问题讨论】:
-
这就解释了。它的预期。 stackoverflow.com/questions/39424122/…
-
谢谢史蒂夫和哈桑
标签: xcode sprite-kit augmented-reality game-physics arkit