我正在使用SceneKit和加速计开发一款具有6 DOF(6自由度)的iPad太空飞船(第一人称)游戏。我有加速度计工作,但我怎么能在加速度计的角度方向(偏航,滚转,俯仰)向前移动宇宙飞船?我使用一个按钮进行前进。我无法在谷歌或其他东西上找到它。我希望有人知道如何做到这一点。
这是代码,其中cubeNode是一个测试(在3D游戏中,cubeNode是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)
}
我有这个代码,但它仍然无法正常工作,出了什么问题?
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
}
以下是一些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