SceneKit : SCNPhysicsBody get current velocity

2019-04-29 21:07发布

问题:

How can I get the current velocity of a SCNPhyisicsBody?

The velocity property does always return (0.0, 0.0, 0.0), and the physicsBody of the prensetationNode of my SCNNode is also nil.

回答1:

You can get the "current" velocity with

physicsBody.velocity

But this is valid only within the "game loop" callbacks (updateAtTime, didApplyAnimations, didSimulatePhysics, will/didRenderScene).



回答2:

...in your view controller, you have to use SCNSceneRendererDelegate protocol like

class GameViewController: UIViewController,SCNSceneRendererDelegate{...

and in the viewDidLoad method set the SCNView delegate to scnView.delegate=self

Then you can implement for example

func renderer(renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: NSTimeInterval) {

    let scnView = self.view as! SCNView
    let scene = scnView.scene
    let compass = scene?.rootNode.childNodeWithName("ObjectName", recursively: true)

    print((compass?.physicsBody?.angularVelocity)!)
}

this works in my case. hope it helps a bit!