Damiano Miazzi
Damiano Miazzi

Reputation: 2315

Apply a constant force to a SCNnode using sceneKit

using ARKit and SceneKit i'm try to make flying a Drone model.

for now successfully I can place the drone over a surface, simulate collision end detect contact between the base and the drone.

Current situation

Now I would like to try to fly this drone, apply some constant force! (I don't want to simply move the Y axis to move up the drone) but I want to simulate a force pulling it up like it happen in the real life.

I started to write some code, and call this method once I press the up button..

// hit test and find object with specific name
    func applyForceToDrone() {
        print("tapped apply force drone at position ")
        
        let nodo = arrayDrone[0] // return the scnNode of the Drone in the scene
        let force = SCNVector3(0, 15, 0)
        
        print("appply force")
        nodo.physicsBody?.applyForce(force, asImpulse: false)
            
        
    }

resulting of this code is the drone jumping up and down every time I press the up button. but this force need to be apply constant in the time ...

How can I constant apply a force to keep the drone up in the air? any tips how I can approach this problem ...

I can't find any example where a constant force is apply to an object.

thanks for the help.

Upvotes: 0

Views: 254

Answers (1)

xaled
xaled

Reputation: 156

You have to apply force inside one of loop functions. To do so, define a variable var applyForce=false. Then on for example buttonUp touch begin, set applyForce=true, on touch end set applyForce=false. Then inside loop function : if applyForce then do apply force. You have to tweak amount of force. Also you can add more parameters.

Upvotes: 1

Related Questions