Xcode 7 SceneKit object's position property does not update when moved?

血红的双手。 提交于 2019-11-28 11:50:44

问题


So I'm making a game and in said game, you will use a DPad to move your ball around the scene. I'm using Xcode 7 Beta 5 though the same problem existed in beta 4. Here is the code of a test view controller:

class TestController: UIViewController,DPadDelegate {
@IBOutlet var scnView:SCNView!
@IBOutlet var dpad:DPad!
var timer:NSTimer?
var update:NSTimer?
var colors = [UIColor.blueColor(),UIColor.redColor(),UIColor.whiteColor(),UIColor.yellowColor(),UIColor.cyanColor(),UIColor.orangeColor(),UIColor.magentaColor(),UIColor.purpleColor()]
var player:SCNNode!
var camera:SCNNode = SCNNode()
var i = 0
override func viewDidLoad() {
    super.viewDidLoad()
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "ColorCubeUpdate", userInfo: nil, repeats: true)
    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateCamera", userInfo: nil, repeats: true)
    dpad.delegate = self
    player = SCNNode(geometry: SCNSphere(radius: 0.25))
    player.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "PlainEyes1")
    player.geometry?.firstMaterial?.multiply.contents = UIColor(red: 1.0, green: 0.5, blue: 0.5, alpha: 1.0)
    player.position = SCNVector3Make(0, 1.5, 0)
    player.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Dynamic, shape: SCNPhysicsShape(geometry: player.geometry!, options: nil))
    scnView.scene?.rootNode.addChildNode(player)
    camera.camera = SCNCamera()
    let constraint = SCNLookAtConstraint(target: player)
    camera.constraints? = [constraint]
}

func updateCamera() {
    i += 1
    print(i)
    print("player's position \(player.position) camera position \(camera.position)")
    camera.position = player.position//SCNVector3Make(player.position.x+4, player.position.y+2, player.position.z)

}

func dPadDown() {
    player.physicsBody?.applyForce(SCNVector3(x: 1, y: 0, z: 0), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadUp() {
    player.physicsBody?.applyForce(SCNVector3(x: -1, y: 0, z: 0), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadLeft() {
    player.physicsBody?.applyForce(SCNVector3(x: 0, y: 0, z: 1), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

func dPadRight() {
    player.physicsBody?.applyForce(SCNVector3(x: 0, y: 0, z: -1), atPosition: SCNVector3(x: 0, y: 0, z: 0), impulse: false)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

The colors array is used elsewhere an the i variable is just for debug as you can see in the UpdateCamera function. This all looks alright but here's my output in Xcode shows the source of a problem I'm having where the camera does not move at all:

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 50

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 51

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 52

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 53

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 54

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 55

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 56

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 57

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 58

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 59

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 60

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 61

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 62

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 63

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 64

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 65

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 66

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 67

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 68

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 69

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 70

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 71

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 72

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 73

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 74

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 75

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 76

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 77

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 78

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 79

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 80

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 81

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 82

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 83

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 84

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0) 85

player's position SCNVector3(x: 0.0, y: 1.5, z: 0.0) camera position SCNVector3(x: 0.0, y: 1.5, z: 0.0)

However on my device, I see the ball fall on a floor (made in a scn file) and act as expected. The only problem is that I don't have access to the actual position of the player and thus cannot move my camera.

I have tried using transform instead of position and that didn't work either. Please help! Thanks in advance!


回答1:


you should print the position of the node's presentationNode.



来源:https://stackoverflow.com/questions/31927113/xcode-7-scenekit-objects-position-property-does-not-update-when-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!