Scenekit 'applyForce at' Help for a newbie!

Hi All,

I’m attempting to write a game for iOS in Swift using Scenekit. I’ve created a rocket, and when touching on the left side of the screen I apply the following force:

let force = SCNVector3(0, 1.2, 0)
let pos = SCNVector(-0.1, 0, 0)
Rocket.physicsBody?.applyForce(force, at: pos, asImpulse: false)

…and when touching the right side of the screen I apply the following force:
let force = SCNVector3(0, 1.2, 0)
let pos = SCNVector(0.1, 0, 0)
Rocket.physicsBody?.applyForce(force, at: pos, asImpulse: false)

This almost has the effect I want, as my rocket begins to fly, tilts left and right, however it is always going up on the Y axis (by the 1.2), rather than in the direction it is facing.

How do I get it to go in the direction it is facing?

You need to apply a force with an X value as well as a Y value. For example,

let force = SCNVector3(0.1, 1.2, 0)

would send it upward 1.2, but also rightward 0.1.
You can then orient the rocket to the direction it is moving with something like

Rocket.rotateToVelocity(Rocket.physicsBody!.velocity, rate: 1)

Thanks for the reply Mr Gerrard MBE, I didn’t realise you did coding since retiring from footy! :wink:

I forgot to mention my game was a physics based game, so using velocity to rotate would result in my rocket pointing down when gravity was in effect when I would like it to point in the direction of last thrust (a little like the game ‘Thrust’ from years ago)

I’ve also read that mixing actions (like rotateTo) with physics is not a good idea.

I’m currently experimenting with an empty node in front of my rocket, but am struggling to work out its vector in relation to the orientation of my rocket.

This topic was automatically closed after 166 days. New replies are no longer allowed.