Search code examples
iosswiftswift2accelerometercore-motion

How to calculate the accelerometer of the last two points the device moved


I'm making game and I need to get the accelerometer of the last two points the phone moved likewise if the phone moved from point zero to point 20 in x axis, the current accelerometer in x equal 20 and I can get it, but I can't get the last point (zero point) which is before the current point (20 point) and here is code of current point:

var currentX: Double = 0.0
// and then in viewDidload :
movementManager.accelerometerUpdateInterval = 0.2
//Start Recording Data
    movementManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue())           
        { (accelerometerData: CMAccelerometerData?, NSError) -> Void in
self.getvalues((accelerometerData?.acceleration)!)
            if(NSError != nil)
            {
                print("\(NSError)")
            }
}
func getvalues(acceleratiuon:CMAcceleration)
{        
        currentX = acceleratiuon.x
}

Solution

  • There are many ways to get the last two points from the accelerometer , but I have used in my code is that - I used a thread that records all the points from sensor in a dynamic list at certain interval of time and then use it in controlling the sprite position . May be this helps you . thanks