john doe
john doe

Reputation: 9660

SceneKit – Apply a Force in the Direction of SCNNode is facing

I have an ARKit application where I have a car model. I want to apply force to the car the direction car is facing.

func accelerate() {           
    // force should be applied in the direction.
    let force = SCNVector3(0,0,-1)
    self.physicsBody?.applyForce(force, asImpulse: true)
}

func turnRight() {
    self.physicsBody?.applyTorque(SCNVector4(0,1.0,0,-0.1), asImpulse: true)
}

func turnLeft() {      
    self.physicsBody?.applyTorque(SCNVector4(0,1.0,0,0.1), asImpulse: true)
}

The accelerate function is the one I need to implement so I can apply the force in the correct direction.

Upvotes: 1

Views: 745

Answers (2)

user11320799
user11320799

Reputation: 1

Use this code for moving forward:

node.physicsBody?.velocity += force

Upvotes: 0

harry
harry

Reputation: 85

You should add an invisible node in front of the car as a child of the car. That way it will not move and you will always have a point of reference in front of the car.

Upvotes: 1

Related Questions