gpt4 book ai didi

swift - 物理形状在接触时与对象的形状不匹配

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:03 25 4
gpt4 key购买 nike

我有两个物体,我希望当它们都接触时发生一些事情。一个对象是 SCNSphere,另一个是 SCNCylinder。唯一的问题是,当我将球扔向圆柱体时,即使有间隙,它们似乎也在接触。如果我把它扔得很远,它就会按预期工作。我怎样才能使联系准确并留下任何空白?看起来 PhysicsShape 与我的对象的形状不匹配。我希望它是准确的。有帮助吗?

我的圆柱体代码:

let scorer = SCNCylinder(radius: 0.02, height: 0.01)
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "basketballSkin.png")
scorer.materials = [material]

let scorerNode = SCNNode(geometry: scorer)

scorerNode.worldPosition = SCNVector3(x: 0, y: -1.35, z: -1.4)

let physicsShapesc = SCNPhysicsShape(node: scorerNode, options:[SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron])

let physicsBodysc = SCNPhysicsBody(type: .static, shape: physicsShapesc)

scorerNode.physicsBody = physicsBodysc

scorerNode.physicsBody?.categoryBitMask = BodyType.scorer.rawValue
scorerNode.physicsBody?.collisionBitMask = BodyType.scorer.rawValue | BodyType.ball.rawValue
scorerNode.physicsBody?.contactTestBitMask = BodyType.scorer.rawValue | BodyType.ball.rawValue

我的球代码:

let ball = SCNSphere(radius:0.04)
// Bucketnode.scale = SCNVector3Make(0.2,0.2,0.2);
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "basketballSkin.png")
ball.materials = [material]

let ballNode = SCNNode(geometry: ball)

ballNode.position = cameraPosition

let physicsShape = SCNPhysicsShape(node: ballNode, options:nil)

let physicsBody = SCNPhysicsBody(type: .dynamic, shape: physicsShape)

ballNode.physicsBody = physicsBody


let forceVector:Float = 2.7

ballNode.physicsBody?.applyForce(SCNVector3Make(cameraPosition.x * forceVector, cameraPosition.y * forceVector, cameraPosition.z*forceVector), asImpulse: true)

ballNode.physicsBody?.categoryBitMask = BodyType.ball.rawValue
//ballNode.physicsBody?.collisionBitMask = BodyType.ball.rawValue | BodyType.scorer.rawValue
ballNode.physicsBody?.contactTestBitMask = BodyType.ball.rawValue | BodyType.scorer.rawValue

sceneView.scene.rootNode.addChildNode(ballNode)

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { // change 2 to desired number of seconds
ballNode.removeFromParentNode()


}

最佳答案

考虑到 SCNPhysicsShape.ShapeType 结构的定义将几何体视为“指定 SceneKit 在基于几何体 创建物理形状时使用的详细程度的类型键的值。”选项集可能不会按预期工作:

let physicsShapesc = SCNPhysicsShape(node: scorerNode, options:[SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron])

除此之外,球向桶移动的速度有多快? “concavePolyhedron”似乎是对渲染要求最高的类型

关于swift - 物理形状在接触时与对象的形状不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50964677/

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