我有一个移动的男人和一张3D地图,我希望这个角色应该在这张地图下移动,但是角色总是移动到地图之外。我希望角色应该在地图中移动,如同在房间中考虑但是它不在正确的地方。有人可以帮我如何设置相机位置,以便地图和移动角色都应该可见,角色应该在地图内移动吗?这是我的代码
// create a new scene
scene = SCNScene(named: "art.scnassets/Messi.dae")!
// create and add a camera to the scene
cameraNode.camera = SCNCamera()
cameraNode.camera?.zNear = 0.01
cameraNode.camera?.zFar = Double(max(map.width, map.height))
stillCamera(self)
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
在这里,我创建了一个移动角色的子节点,并根据具有SCNPhysicsShape的地图实体修复其位置
messiArmtr = scene.rootNode.childNodeWithName("Armtr", recursively: true)!
mountNode.addChildNode(messiArmtr)
for entity in map.entities {
switch entity.type {
case .Hero:
mountNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: SCNCylinder(radius: 0.2, height: 1), options: nil))
if #available(iOS 9.0, *) {
mountNode.physicsBody?.contactTestBitMask = ~0
}
mountNode.position = SCNVector3(x: entity.x, y: 0.5, z: entity.y)
scene.rootNode.addChildNode(mountNode)
case .Monster:
let monsterNode = SCNNode()
monsterNode.position = SCNVector3(x: entity.x, y: 1, z: entity.y)
monsterNode.geometry = SCNCylinder(radius: 0.04, height: 0.01)
monsterNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: SCNPhysicsShape(geometry: monsterNode.geometry!, options: nil))
}
}
现在我设置移动角色跟随相机
func followCamera(sender:AnyObject){
mountNode.addChildNode(cameraNode)
cameraNode.constraints = nil
cameraNode.rotation = SCNVector4(0,1,0,M_PI)
cameraNode.position = SCNVector3(0,10,-15)
}