我正在使用Scenekit,我将一个摄像头连接到一个节点,并将一个速度应用到父节点的物理主体。但只有父节点移动并且相机保持原位。关于为什么会发生这种情况的任何想法?我需要相机在移动时仍然附着在父母身上。
代码:
class GameViewController: UIViewController, UIGestureRecognizerDelegate, SCNSceneRendererDelegate{
var playerObj : Player?
var panGesture : UIPanGestureRecognizer?
override func viewDidLoad() {
//create view and add scene
let sceneView = self.view as SCNView
let scene = SCNScene()
sceneView.delegate = self
sceneView.scene = scene
//create camera
// let cameraNode = SCNNode()
// cameraNode.camera = SCNCamera()
//create player
playerObj = Player()
scene.rootNode.addChildNode(playerObj!.node)
//create floor
let floorGeometry = SCNFloor()
let floorNode = SCNNode(geometry: floorGeometry)
floorNode.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: SCNPhysicsShape(geometry: floorGeometry, options: nil))
floorNode.position = SCNVector3Make(0, 0, 0)
scene.rootNode.addChildNode(floorNode)
playerObj?.node.camera = SCNCamera()
//other setup
sceneView.showsStatistics = true
sceneView.allowsCameraControl = true
scene.physicsWorld.gravity = SCNVector3Make(0, -50, 0)
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
sceneView.gestureRecognizers?.append(tapGesture)
panGesture = UIPanGestureRecognizer(target: self, action: "handlePan:")
sceneView.gestureRecognizers?.append(panGesture!)
}
func handlePan(gestureRecognize: UIPanGestureRecognizer){
//take coordinates and transform into direction
if let gesture = panGesture?{
playerObj?.node.physicsBody?.velocity = SCNVector3Make(Float(gesture.translationInView(self.view).x), 0, Float(gesture.translationInView(self.view).y))
print(gesture.translationInView(self.view))
if(gesture.state == UIGestureRecognizerState.Ended){
playerObj?.node.physicsBody?.velocity = SCNVector3Make(0, 0, 0)
gestureRecognize.setTranslation(CGPointZero, inView: self.view)
}
}
}
答案 0 :(得分:0)
你的相机可能不需要自己的物理机构。
如果只需跟随其父级,您可以实现-renderer:didSimulatePhysicsAtTime:
并根据其父级presentationNode
的位置不断更新相机的位置。