ios - Map device tilt to physicsWorld gravity? -


I am creating a "marble" maze game to learn the spritekit basics.

I want to map the gravity of the game to the inclination of the device. I am trying to understand how to do it, but I am only able to map the Y axis successfully:

  class func receipt (motionManager: CMMotionManager) {var vec = CGVectorMake 0, 0) If the attitude = motionManager.device Motion? .attitude? {Let's do = CGFLOT (-attitude.pitch * 2 / M_PI) // It works, it gives 1/1 when the device is vertical (1 when the Home button is inverted) x = CGFloat (attitude.roll * 2 / M_PI) // This does not work for physics. World.gravity = CGVectorMake (x, y)}}  

I can map the Y axis, which is called "up" or " Down "(relative to portrait mode) though I do not understand how to map the X axis (pull from the edge of the device). For example, when the device should be (0,0) when placing it flat on the table (x, y), and also should be (0,0) even when it is placed below the screen. Roll Return -179 In addition, if I keep my device vertical (on portrait mode) and keep the device running on my feet, then gravity should be (X: 0, Y: 1) however X continues to change because it Is based on attitude. Roll

How can this be done?

The easiest way to get an accelerometer update, not to speed up the device, and to direct the gravity vector Reads - It's okay that the accelerometer captures: a measure of gravity in the device's coordinate system.

Unfortunately I am a swift thickness, therefore the code can not be given the code, but you are looking to provide a block of type CMAccelerometerHandler , which will allow you to CMAccelerationData from where you can get the CMAcceleration string and that is, to apply the gravity vector directly.

  if data = motionmanager.accelerometerData? {Vec = CGVectorMake (CGFloat (data.acceleration.x), CGFloat (data.acceleration.y)}}  

Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -