Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SceneKit : SCNPhysicsBody get current velocity

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.

like image 852
Max Avatar asked Jul 03 '15 21:07

Max


2 Answers

You can get the "current" velocity with

physicsBody.velocity

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

like image 90
Toyos Avatar answered Nov 14 '22 02:11

Toyos


...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!

like image 23
nik Avatar answered Nov 14 '22 00:11

nik