Kishan Barmawala
Kishan Barmawala

Reputation: 183

How to check if ARKit camera movement is too fast in iOS

I am working on apple's ARKit with RealityKit and I want to guide the user not to move the device too fast. I am trying to detect movement using this delegate method.

func session(_ session: ARSession, 
               cameraDidChangeTrackingState camera: ARCamera) {
    switch camera.trackingState {
        case .limited(let reason):
            switch reason {
                case .excessiveMotion: print("too fast") // then update UI
                default: break
            }
        default: break
    } 
}

But the thing is, this method is not that accurate when I try to move my device quickly.

Is there any other way to detect fast movement?

Upvotes: 1

Views: 793

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58113

case .excessiveMotion

ARKit's subcase .excessiveMotion of trackingState instance property is very subjective because tracking results may vary depending on lighting conditions, environment, rich/poor textures of real-world objects, number of reflective/refractive surfaces, presence/absence of a LiDAR scanner, presence/absence of a ARWorldMap, etc. This case is used for a general (i.e. inaccurate) analysis of the tracking situation.

In my opinion, you can accurately detect fast camera movement only under ideal tracking conditions (for a novice AR user, this is an impossible task). Today's robust solution (started from iOS 13.0) is a quick preliminary tracking session with onboarding instructions to direct users toward a specific goal. It's called ARCoachingOverlayView.

Upvotes: 2

mehed1hasan
mehed1hasan

Reputation: 11

Yes, there is another way to detect fast movement in ARKit with RealityKit. You can use the accelerometer data to measure the acceleration of the device as it moves. You can access the accelerometer data by using the CoreMotion framework.

// First, import the CoreMotion framework. 
import CoreMotion
// Then, create an instance of the CMMotionManager class. 
let motionManager = CMMotionManager()
// Set the sampling rate of the motion data to a high frequency. 
motionManager.accelerometerUpdateInterval = 0.1
// Start the motion updates. 
motionManager.startAccelerometerUpdates()
// Create a CMAccelerometerHandler to get the acceleration data. 
motionManager.accelerometerUpdateHandler = { (accelerometerData, error) in
    if error == nil {


 // Use the acceleration data to measure the magnitude of the device's movement. 
        let accelerationX = accelerometerData!.acceleration.x
        let accelerationY = accelerometerData!.acceleration.y
        let accelerationZ = accelerometerData!.acceleration.z
        let acceleration = sqrt(accelerationX * accelerationX + accelerationY * accelerationY + accelerationZ * acceleration)
        
        // If the magnitude is above a certain threshold, it indicates that the device has moved quickly. 
        if acceleration > 0.1 {
            print("too fast")
            // then update UI
        }
    }
}

Upvotes: 0

Related Questions