{"id":1870,"date":"2015-07-22T17:19:21","date_gmt":"2015-07-22T16:19:21","guid":{"rendered":"http:\/\/denbeke.be\/blog\/?p=1870"},"modified":"2015-07-22T17:19:21","modified_gmt":"2015-07-22T16:19:21","slug":"swiftios-moving-objects-with-device-motion-tilt-screen","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/","title":{"rendered":"Swift\/iOS: Moving objects with device motion (tilt screen)"},"content":{"rendered":"<p>One of the cool things of games on smartphones, is that you can control objects using the device motion. But how do you move an object using device tilt (using Swift 2)?<\/p>\n<p>To do so you need to use the\u00a0<code>CMMotionManager<\/code> (which is part of Apple\u2019s\u00a0<code>CoreMotion<\/code>\u00a0framework). So don&#8217;t forget to include the framework at the top of your code: <code>import CoreMotion<\/code>.<\/p>\n<p>You must also create one (single) CMMotionManager instance.<\/p>\n<pre><code class=\"swift\">let motionManager: CMMotionManager = CMMotionManager()<\/code><\/pre>\n<p>And start the motion manager in the <code>didMoveToView()<\/code> function:<\/p>\n<pre><code class=\"swift\">motionManager.startAccelerometerUpdates()<\/code><\/pre>\n<p>Once the motion manager is up and running, we can retrieve motion data from it using the\u00a0<code>accelerometerData<\/code> member.<\/p>\n<pre><code class=\"swift\">\/\/ Get node of object to move\r\nlet paddle = childNodeWithName(PaddleCategoryName) as! SKSpriteNode\r\n\r\n\/\/ Get MotionManager data\r\nif let data = motionManager.accelerometerData {\r\n    \r\n    \/\/ Only get use data if it is \"tilt enough\"\r\n    if (fabs(data.acceleration.x) &gt; 0.2) {\r\n        \r\n        \/\/ Apply force to the moving object\r\n        paddle.physicsBody!.applyForce(CGVectorMake(40.0 * CGFloat(data.acceleration.x), 0))\r\n        \r\n    }\r\n}<\/code><\/pre>\n<p>If you want to actually move the object, you need to make sure that the object has some physical properties:<\/p>\n<pre><code class=\"swift\">\/\/ Rectangular physics body around the object\r\npaddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)\r\n \r\n\/\/ Make it dynamic\r\npaddle.physicsBody!.dynamic = true\r\n \r\n\/\/ We don't want the object to fall of the screen\r\npaddle.physicsBody!.affectedByGravity = false\r\n \r\n\/\/ Add some mass to it\r\npaddle.physicsBody!.mass = 0.02<\/code><\/pre>\n<p><code>The data.acceleration<\/code>\u00a0member is a struct of type <code>CMAcceleration<\/code> which contains the acceleration along the x-, y- and z-axis.<\/p>\n<pre><code class=\"swift\">struct CMAcceleration {\r\n    var x: Double\r\n    var y: Double\r\n    var z: Double\r\n}<\/code><\/pre>\n<p>The following image (<a href=\"http:\/\/nshipster.com\/cmdevicemotion\/\" target=\"_blank\">source<\/a>) explains the orientation of those axes.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1874\" src=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/07\/CoreMotion-CMAcceleration-axes.png\" alt=\"CoreMotion CMAcceleration axes\" width=\"450\" height=\"340\" srcset=\"https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/07\/CoreMotion-CMAcceleration-axes.png 450w, https:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/07\/CoreMotion-CMAcceleration-axes-300x227.png 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the cool things of games on smartphones, is that you can control objects using the device motion. But how do you move an object using device tilt (using Swift 2)? To do so you need to use the\u00a0CMMotionManager (which is part of Apple\u2019s\u00a0CoreMotion\u00a0framework). So don&#8217;t forget to include the framework at the top [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[235],"tags":[124,129,257,258],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift\/iOS: Moving objects with device motion (tilt screen) &ndash; DenBeke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift\/iOS: Moving objects with device motion (tilt screen) &ndash; DenBeke\" \/>\n<meta property=\"og:description\" content=\"One of the cool things of games on smartphones, is that you can control objects using the device motion. But how do you move an object using device tilt (using Swift 2)? To do so you need to use the\u00a0CMMotionManager (which is part of Apple\u2019s\u00a0CoreMotion\u00a0framework). So don&#8217;t forget to include the framework at the top [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-22T16:19:21+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/07\/CoreMotion-CMAcceleration-axes.png\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/denbeke.be\/blog\/wp-content\/uploads\/2015\/07\/CoreMotion-CMAcceleration-axes.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/\",\"name\":\"Swift\/iOS: Moving objects with device motion (tilt screen) &ndash; DenBeke\",\"isPartOf\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/#primaryimage\"},\"datePublished\":\"2015-07-22T16:19:21+00:00\",\"dateModified\":\"2015-07-22T16:19:21+00:00\",\"author\":{\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/programming\/swiftios-moving-objects-with-device-motion-tilt-screen\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1870"}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":5,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":1876,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions\/1876"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}