如何解决ARKit 物理——为什么这两个对象不相互作用?
为什么这 2 个对象相互穿过而不相互作用,我做错了什么? 在每个对象上,我应用了应该允许物理引擎工作的属性 PhysicsBody。
我使用点击手势添加方形对象,创建名为“base”的锚点,并在渲染中添加对象基础。
对于球,我使用光线投射查询将球定位在平面上。
一切正常,只是动态看起来不对,它们相互传递。
// my square base
func renderer(_ renderer: SCNSceneRenderer,didAdd node: SCNNode,for anchor: aranchor) {
if let nome = anchor.name,nome == "base" {
let geometry = SCNPlane(width: 1,height: 1)
let material = SCNMaterial()
material.diffuse.contents = UIColor(red: 90/255,green: 200/255,blue: 250/255,alpha: 0.50)
geometry.materials = [material]
// physics
let physSchape = SCNPhysicsShape(geometry: geometry,options: nil)
let planePhysic = SCNPhysicsBody(type: .static,shape: physSchape)
planePhysic.restitution = 0.0
planePhysic.friction = 1.0
let nodo = SCNNode(geometry: geometry)
nodo.eulerAngles.x = -.pi / 2
nodo.physicsBody = planePhysic
node.addChildNode(nodo)
}
}
还有这个球:
func addBall(recognizer: UITapGestureRecognizer){
let tapLocation = recognizer.location(in: view)
guard let query = view.raycastQuery(from: tapLocation,allowing: .existingPlaneGeometry,alignment: .horizontal) else {return}
// ottengo posizione real world
guard let translation = view.castRay(for: query).first?.worldTransform.translation else {return}
let x = translation.x
let y = translation.y
let z = translation.z
let ballGeometry = SCNSphere(radius: 0.1)
let mat = SCNMaterial()
mat.diffuse.contents = UIImage(named: "ball")
ballGeometry.materials = [mat]
//Physics
let physSchape = SCNPhysicsShape(geometry: ballGeometry,options: nil)
let ballPhysic = SCNPhysicsBody(type: .dynamic,shape: physSchape)
ballPhysic.mass = 0.2
ballPhysic.friction = 0.8
let nodeBall = SCNNode(geometry: ballGeometry)
nodeBall.position = SCNVector3(x,y+0.3,z)
nodeBall.physicsBody = ballPhysic
view.scene.rootNode.addChildNode(nodeBall)
}
他们应该互相影响,但实际上我的球穿过了基地。
解决方法
如果您要模拟两个对象的碰撞,首先,您必须实现名为 SCNPhysicsContactDelegate
的协议及其委托:
weak var contactDelegate: SCNPhysicsContactDelegate? { get set }
使用它的三个可选实例方法:
func physicsWorld(_ world: SCNPhysicsWorld,didBegin contact: SCNPhysicsContact)
func physicsWorld(_ world: SCNPhysicsWorld,didUpdate contact: SCNPhysicsContact)
func physicsWorld(_ world: SCNPhysicsWorld,didEnd contact: SCNPhysicsContact)
当然,category bit mask
和 collision bit mask
是必需的。
如果您需要更详细的信息,请查看THIS POST。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。