Inside SwiftShot: Creating an AR Game

WWDC 2018

Posted by Den on August 16, 2018 · 10 mins read
Inside SwiftShot: Creating an AR Game

Inside SwiftShot: Creating an AR Game

WWDC 2018

Inside SwiftShot: Creating an AR Game

WWDC 2018

SwiftShot Internals

  • Establishing a shared coordinate space
  • Networking and state sharing
  • Physics
  • Asset import and management
  • Flag simulation
  • Dynamic audio

ARKit — Establishing a shared Coordinate Space

  • Image detection
  • Object detection
  • World Map sharing
  • iBeacons for fixed installations

Sharing a World Map

1. Saving

  • First device scans area, captures features
  • Asks ARSessionfor world map
  • Serializes to dist

2. Ad-hoc gaming

Share over network

  • Peer-to-peer network connnection
  • Encrypt data in flight
  • UI guidance to aid delocalization

3. Fixed installations

  • Prerecord world maps for specific installation areas
  • Distribute to managed devices
  • Use iBeacons to automatically select correct world map for each location

4. Loading

5. Privacy

ARWorldMap uses features of the world around you

  • No latitude/ longitude information
  • May include personally identifiable information

Treat serialized ARWorldMap as user-private data

  • Encrypt at rest and in motion
  • Obtain user consent for extended usage

6. ARAnchor

ARAnchor represents a location and orientation in the physical world

  • ARKit adds anchors for planes, objects, and images
  • Also, created and added via API

Networking with Multipeer Connectivity

  • Peer-to-peer connectivity, no central server
  • Encryption and authentication are built in
  • Advertisement and discovery
  • Device starting the game creates a session, starts advertising
  • Other devices see session listed in menu
  • User selects game to join
    - Device sends request
    - Advertising device accepts or denies
  • Once session set up, devices are peers in the network
  • API to send
    - Data packets
    - Resources as URLs
    - Streams
  • UDP for transport

Physics

  • SceneKit physics
  • All peers run physics simulation
  • Server sends updates to client to ensure synchronization
  • Only game state relevant information is shared
  • Object scaled approximately 10x for better performance

Physics Data Optimization

Physics Data

Performance: Optimized BitStreamCodable

Encoding — BitStream

  • Bit-packed encoding of values
  • Minimum size, fast serialization and deserialization
  • Purpose-built for network communications of binary data
    - Not suitable for persistence
    - Not robust in face of changing data scheme

Combining Encodings

  • Physics data encoded in BitStreams
  • Other commands encoded with Swift Codable, binary property lists
How to combine

Assets for Game Levels

Use level of detail to optimize for different distance

  • Nearby objects get chamfered edges on blocks
  • Distant objects get less detailed textures, fewer polygons

Physics bodies are the same

  • Use predefined types (box, sphere, etc) wherever possible for performance
  • SceneKit will build convex hull if not otherwise specified

Flag Simulation

  • SceneKit static asset
  • Swift class 
    -
    Build metal
    - Apply results back to SceneKit model
  • Metal compute shader
    - Computes forces in a mesh
    - Produces vertices and shadows

Audio in SwiftShot

  • Positions sounds in the world
  • Limit app size
  • Leverage MIDI instrument support from AVFoundation

Sample