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