1

I am developing an iPad spaceship (first person) game with 6 DOF (6 degrees of freedom) with SceneKit and the accelerometer. I have the accelerometer working, but how can I move the spaceship forward in the direction of the angle of the accelerometer (yaw, roll, pitch)? I use a button for the forward direction. I can't find it on google or something. I hope someone knows how to do this.

Here is the code, where cubeNode is a test (in the 3D game cubeNode is cameraNode).

func updateMotionControl() {
motion.getAccelerometerData(interval: 0.1) { (x,y,z) in
self.motionForce = SCNVector3(x: Float(x) * 0.05, y: Float(y) * 0.05,  
  z: Float(z) * 0.05)                                              
  }
    let currentAttitude = motion.attitude
    let roll = Float(currentAttitude.roll)
    let pitch = Float(currentAttitude.pitch)
    let yaw = Float(currentAttitude.yaw)
    cubeNode.eulerAngles = SCNVector3(roll, yaw, pitch)
   }

I have this code, but it still won't work, what is wrong?

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time:   TimeInterval) {

cameraNode.eulerAngles = SCNVector3Make(Float(rot.roll + M_PI/2) * 2, -Float(rot.yaw) * 3, -Float(rot.pitch * 5))

let z = cos(rot.pitch)*sin(rot.yaw)
let y = sin(rot.pitch)
let x = cos(rot.pitch)*cos(rot.yaw)

let view = SCNVector3(x: Float(x), y : Float(y), z : Float(z))

cameraNode.position =  cameraNode.position + (view * 0.1)
//cameraNode.physicsBody?.velocity = -view
}

Here are the prints for a few x, y en z: X: 0.996798277375072, 0.996753286621399, 0.996748721234948, 0.996776960253164, 0.996809519516764, 0.996816599311414, 0.996818969166521, 0.996777228814986

Z: 0.0683379467811582, 0.0678748719764882, 0.0678313394347644, 0.0683611603110574, 0.0686781724979699, 0.0684588783890975, 0.0461781969805951, 0.0478922538280758, 0.0485691959442941, 0.0482168862439498, 0.0474367474311863

Y: 0.0437050318386564, 0.0434850468949368, 0.0432268234343706, 0.0431581009944219, 0.0433707104009568, 0.0436728563252332, 0.044357103165837, 0.0449712895273139, 0.0364369979497931, 0.0353335950917769

Johan Kornet
  • 103
  • 11
  • If you couldn't find it on Google, then you either didn't look carefully enough or were searching for the wrong thing. There are many answers to this question even here on SO - [example](https://stackoverflow.com/questions/1568568/how-to-convert-euler-angles-to-directional-vector). Assuming `cubeNode` is your spaceship, simply add a multiple of the computed vector (not `eulerAngles`) to its position. – meowgoesthedog Mar 30 '18 at 20:26
  • OK, thanx, but my question is just how to do this in SceneKit. I am testing with an applyforce-vector and also with a child of the camera. – Johan Kornet Mar 31 '18 at 12:41
  • How do I calculate the direction vector of any 3D coordinate (from the EulerAngle) with SceneKit? – Johan Kornet Mar 31 '18 at 13:54
  • See the link I provided. – meowgoesthedog Mar 31 '18 at 14:13
  • I have edited my question, with the latest code, but it still won't work, what is wrong? – Johan Kornet Mar 31 '18 at 18:06
  • Check your equations. Random sign flips? No sign of `cry` being used anywhere – meowgoesthedog Mar 31 '18 at 18:24
  • I check the equations. The x-axis is left-right, the y-axis is up-down and the z-axis is forward - backward. But i think with 2 angles it is possible the calculate the direction vector. – Johan Kornet Mar 31 '18 at 19:17
  • Still don't work, does anyone know what the correct code is for this in Scenekit and Swift? – Johan Kornet Apr 01 '18 at 19:44
  • They moved comments to the chat, how can I see them in the chat? – Johan Kornet Apr 02 '18 at 20:10

0 Answers0