添加 physicsBody 使 ar 对象消失
Adding physicsBody makes the ar object disappear
我正在尝试将简单的 3D 对象添加到 sceneView。将对象添加到视图相当简单,并且进行得很顺利。但是,当我尝试添加主体以进行碰撞时 (boxNode.physicsBody = boxBody),立方体完全消失了,我再也找不到立方体了。
我错过了什么吗?我在下面附上了我的代码。项目的其余部分由选择“AR app”的项目设置生成
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate, ARCoachingOverlayViewDelegate {
@IBOutlet var sceneView: ARSCNView!
let coachingOverlay = ARCoachingOverlayView()
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
let box = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0)
let boxNode = SCNNode(geometry: box)
let boxBody = SCNPhysicsBody(type: .dynamic, shape: nil)
box.firstMaterial?.diffuse.contents = UIColor.red
boxNode.position = SCNVector3(0.1,0,0)
// boxBody.mass = 1
//// boxBody.categoryBitMask = CollisionBitmask.box.rawValue
boxNode.physicsBody = boxBody
// boxNode.rotation = SCNVector4Make(1, 1, 1, 1)
// boxNode.name = "box"
sceneView.scene.rootNode.addChildNode(boxNode)
let scene = SCNScene()
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
/*
// Override to create and configure nodes for anchors added to the view's session.
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
return node
}
*/
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}
你的物体可能会无限期地下降,除非有一个 SCNFloor 或它下面有运动模式的东西。
尝试添加一些东西来记录您的 SCNNode 的位置,点击一次或每秒一次,您会看到它在那一刻的位置;它可能有一个非常负的 y 坐标。
使用isAffectedByGravity实例属性摆脱物体的重力:
var isAffectedByGravity: Bool { get set }
您需要 false
值(默认为 true
):
let sphere = SCNSphere(radius: 1)
let node = SCNNode(geometry: sphere)
let physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
node.physicsBody = physicsBody
physicsBody.isAffectedByGravity = false
sceneView.scene.rootNode.addChildNode(node)
我正在尝试将简单的 3D 对象添加到 sceneView。将对象添加到视图相当简单,并且进行得很顺利。但是,当我尝试添加主体以进行碰撞时 (boxNode.physicsBody = boxBody),立方体完全消失了,我再也找不到立方体了。
我错过了什么吗?我在下面附上了我的代码。项目的其余部分由选择“AR app”的项目设置生成
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate, ARCoachingOverlayViewDelegate {
@IBOutlet var sceneView: ARSCNView!
let coachingOverlay = ARCoachingOverlayView()
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
let box = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0)
let boxNode = SCNNode(geometry: box)
let boxBody = SCNPhysicsBody(type: .dynamic, shape: nil)
box.firstMaterial?.diffuse.contents = UIColor.red
boxNode.position = SCNVector3(0.1,0,0)
// boxBody.mass = 1
//// boxBody.categoryBitMask = CollisionBitmask.box.rawValue
boxNode.physicsBody = boxBody
// boxNode.rotation = SCNVector4Make(1, 1, 1, 1)
// boxNode.name = "box"
sceneView.scene.rootNode.addChildNode(boxNode)
let scene = SCNScene()
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
// MARK: - ARSCNViewDelegate
/*
// Override to create and configure nodes for anchors added to the view's session.
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
return node
}
*/
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}
你的物体可能会无限期地下降,除非有一个 SCNFloor 或它下面有运动模式的东西。
尝试添加一些东西来记录您的 SCNNode 的位置,点击一次或每秒一次,您会看到它在那一刻的位置;它可能有一个非常负的 y 坐标。
使用isAffectedByGravity实例属性摆脱物体的重力:
var isAffectedByGravity: Bool { get set }
您需要 false
值(默认为 true
):
let sphere = SCNSphere(radius: 1)
let node = SCNNode(geometry: sphere)
let physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
node.physicsBody = physicsBody
physicsBody.isAffectedByGravity = false
sceneView.scene.rootNode.addChildNode(node)