ARKit Adventures
Tags: 3dmodeling, swift4, arkit, drones
Published: 2018-01-24 14:12:45

---
title: "ARKit Adventures"
published: true
tags: 3d-modeling,swift-4,arkit,drones
canonical_url: https://medium.com/journey-of-one-thousand-apps/aarkit-adventures-697dfbe7779e
---

#### Making A Remote Control&nbsp;Drone

![](https://cdn-images-1.medium.com/max/300/1*LpNpWVlYHaa00I_66QAfAg.gif)

```
Demo Code
```

In the last few posts on ARKit, most of the content we placed was pretty static. For this example, I want to switch gears and make something in ARKit that is a bit more interactive.

#### **Send in the&nbsp;drones!**

If you’ve ever wanted to play around with a remote-controlled drone but have never had the opportunity to do so, this tutorial is for you. I know the controls look a bit stupid at the moment, but the point of this post isn’t to create a genuinely impressive interface, it’s to get you started down the road where you’re interacting with the augmented world.

### Getting Started

![](https://cdn-images-1.medium.com/max/1024/1*zCcFVio1j-FzFhXGzVUPfA.jpeg)

Given the complexity presented to us in rendering a drone, the best place for us to start would be by finding a good 3D drone model to use. I’ve found that COLLADA files are the easiest for interfacing with Apple’s SceneKit and are the easiest file type to convert to&nbsp;.scn files. COLLADA files use the file extension&nbsp;.dae and can be viewed in Apple’s preview application.

> A file with the DAE [file extension](https://www.lifewire.com/what-is-a-file-extension-2625879) is a Digital Asset Exchange file. As the name implies, it’s used by various graphics programs to exchange digital assets under the same format. They may be images, textures, 3D models, etc. — [Source](https://www.lifewire.com/dae-file-2620544)

For this example, we’re going to be using a 3D model of a drone that found on [free3d.com](http://free3d.com). If you’re wary of signing up, you can download the&nbsp;.dae file from the link I will post below, or alternatively, you can you can get the file that I’ve converted to&nbsp;.scn in the finished project&nbsp;files.

Here is a link to the&nbsp;.dae&nbsp;file:

[DropBox](https://www.dropbox.com/s/ba7c2ha18cy6uwa/Drone_dae.dae)

#### Converting From&nbsp;.dae To&nbsp;.scn

![](https://cdn-images-1.medium.com/max/1024/1*tIRXN3MaY3uRsCrFFrAOgA.png)

While SceneKit can handle working with&nbsp;.dae files just fine, for simplicity, we’ll just go ahead and convert it to a&nbsp;.scn file. There might be a better way to do this, I stumbled across this feature while playing around with SceneKit’s settings. To convert to a&nbsp;.scn file, select the&nbsp;.dae file, and in the inspector, on the right side of the screen, there should be a tab for the physics inspector like&nbsp;below:

![](https://cdn-images-1.medium.com/max/516/1*t0v0USS2M_rf2JsgiIg1OQ.png)

Once you select a physics body, XCode should pop up a prompt asking you whether you want to convert the document to SCN&nbsp;format.

![](https://cdn-images-1.medium.com/max/904/1*LPVrqLe7dR2vuQ2b9dbkFw.png)

### DroneSceneView

Now that we have our model we can start coding our drone. I want to start off by creating a subclass of ARSCNView called DroneSceneView. In this view, we’ll try to encapsulate the behaviors and properties of our drone. We’ll use the controller to connect these behaviors to the&nbsp;UI.

#### Setup

We should give our DroneSceneView five properties which will be of type SCNNodes. The helicopterNode gives us a hook into the main body of the drone, the blade1 and blade2 nodes hook us into the the framework surrounding the drones left or right rotors and the rotorR and rotorL nodes hooks into the blade themselves.

{% gist https://gist.github.com/chriswebb09/f212aea61ca2d8d66dfbd7d6b0efe8a5 %}

The first method we should create will be the overall setup method which I called setupDrone. In setupDrone, we should assign the appropriate parts of our drone model to these nodes. The names passed to the method childNode(withName:, recursively:) match up with the names in our model’s scene&nbsp;graph.

![](https://cdn-images-1.medium.com/max/1024/1*cS13I5c9A8WTbjp_QLD5Ng.png)

#### Style

Next we’ll add a bit of styling to our drone. This part isn’t hugely important but it can help make it seem more realistic, which is generally a key aspect of augmented reality and it gives you a better visual sense of the different parts.

{% gist https://gist.github.com/chriswebb09/c87009501e402bf914026c6790381879 %}

#### SpinBlades

Because we want our drone to look like the real thing, we’ll need to animate the rotors to rotate. To simplify things, I’m setting it so that the drones rotors are going constantly. To do this need to access the blades themselves, and manipulate them along their&nbsp;z-axis.

```
let rotate = SCNAction.rotateBy(x: 0, y: 0, z: 200, duration: 0.5)
```

{% gist https://gist.github.com/chriswebb09/9ff8b6b0f7d2a4780088a5b98b50c6d9 %}

The drones rotators will now spin, but it doesn’t actually make any difference in terms of&nbsp;motion.

### Controls

The spinning blades are purely cosmetic. Let’s actually make it do something. To create an animation sequence we’re going to call SCNTransaction.begin

```
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5

// Animation sequence

SCNTransaction.commit()
```

#### Lateral

![](https://cdn-images-1.medium.com/max/225/1*4ijWIzcTOG3TjqXif7WStA.gif)

```
// Right
helicopterNode.position = SCNVector3(helicopterNode.position.x + 0.5, helicopterNode.position.y, helicopterNode.position.z)

// Left
helicopterNode.position = SCNVector3(helicopterNode.position.x - 0.5, helicopterNode.position.y, helicopterNode.position.z)
```

To make our drone move to left or right we’ll manipulate the x element in its position property. To move to the left we’ll subtract a float value of -0.5 from x and conversely to move to the right we’ll add a float value of 0.5 to&nbsp;x.

{% gist https://gist.github.com/chriswebb09/2e9a67903e7246c65b4db23e0b2917d4 %}

#### Forward/Back

![](https://cdn-images-1.medium.com/max/225/1*9N2crnX-5jfzyIKzPFurww.gif)

```
// Forward 
helicopterNode.position = SCNVector3(helicopterNode.position.x, helicopterNode.position.y, helicopterNode.position.z - 0.5)

// Backward 
helicopterNode.position = SCNVector3(helicopterNode.position.x, helicopterNode.position.y, helicopterNode.position.z + 0.5)
```

To make our drone move forward or backward we’ll manipulate the z element in its position property. To move to the left we’ll subtract a float value of -0.5 from z and conversely to move to the right we’ll add a float value of 0.5 to&nbsp;z.

{% gist https://gist.github.com/chriswebb09/89ac1045c80159e2f72e2bbc77ab8ee4 %}

#### Altitude

![](https://cdn-images-1.medium.com/max/225/1*AuCC5cLuwIZ9QvHeWjXu0A.gif)

```
helicopterNode.position = SCNVector3(helicopterNode.position.x, value, helicopterNode.position.z)
```

To make our drone move up or down we’ll manipulate the y element in its position property. By add a flow value to the drones y position property value the drone will move up, if you subtract a float value from the drones y position property value it will&nbsp;descend.

{% gist https://gist.github.com/chriswebb09/11accd6703ef182cc295a257fa4e93cb %}

#### Special Effects

These animations are mainly for decoration. They tilt the drone when it moves and then resets it to normal resting position. This is more an experiment of mine to try to make the flight look as natural as possible.

```
blade2Node.runAction(SCNAction.rotateBy(x: 0.3, y: 0, z: 0, duration: 1.5)) 
blade1Node.runAction(SCNAction.rotateBy(x: 0.3, y: 0, z: 0, duration: 1.5)) 
SCNTransaction.commit() 
blade2Node.runAction(SCNAction.rotateBy(x: -0.3, y: 0, z: 0, duration: 0.25)) 
blade1Node.runAction(SCNAction.rotateBy(x: -0.3, y: 0, z: 0, duration: 0.25))
```

### Controls UI

![](https://cdn-images-1.medium.com/max/472/1*w2eZTn7WbzunABoy7iRmAw.png)

As you can tell from the picture, I’ve constructed rudmentary UI to get us going. It’s basically some arrows and a slider. We’ll use the slider to control our altitude and the arrows to control the direction that our drone moves&nbsp;in.

{% gist https://gist.github.com/chriswebb09/79f85eb63df38a4987575f371c2c3ba2 %}

#### Sources:

- [3DHaupt](https://3dhaupt.com)
- [Controllable Drone - Blender Game Engine - 3d model - .3ds, .obj, .dae, .blend, .fbx, .mtl, .dxf, .stl](https://free3d.com/3d-model/controllable-drone-blender-game-engine-67381.html)

* * *