It would be nice to have a wrapper for different smartphone sensors like accelerometer and gyroscope.
- On iOS it could use CoreMotion framework.
- On OS X it could just return a fixed value representing unavailable data.
- On Android it could return a proper value if the corresponding sensor is available or a fixed value.
Here is an API proposal:
const cocos2d::Vec3 MK::Motion::getCurrentAcceleration();
void MK::Motion::getCurrentAcceleration( std::function<void( cocos2d::Vec3 )> callback );
void MK::Motion::startGettingAccelerationMeasurements(
std::function<void( cocos2d::Vec3 )> callback,
const std::string &key = ""
);
void MK::Motion::stopGettingAccelerationMeasurements( const std::string &key = "" );
const cocos2d::Vec3 MK::Motion::getCurrentRotationRate();
void MK::Motion::getCurrentRotationRate( std::function<void( cocos2d::Vec3 )> callback );
void MK::Motion::startGettingRotationRateMeasurements(
std::function<void( cocos2d::Vec3 )> callback,
const std::string &key = ""
);
void MK::Motion::stopGettingRotationRateMeasurements( const std::string &key = "" );
Looking at iOS documentation it seems that first time retrieving data from sensors might take a while (that's why I added an asynchronous function) although I've tested on an iPhone 6 and it tooks less than a millisecond. If it is really that fast in old iOS devices and Android the async function should be removed.
There are also two additional functions to start getting new measurements as soon as possible. Those two methods could be a noop on OS X or devices without the proper sensor or just call the callback periodically with a zero vector.
On f9fb5a4 I've implemented this basic API in iOS and OS X.
It would be nice to have a wrapper for different smartphone sensors like accelerometer and gyroscope.
Here is an API proposal:
Looking at iOS documentation it seems that first time retrieving data from sensors might take a while (that's why I added an asynchronous function) although I've tested on an iPhone 6 and it tooks less than a millisecond. If it is really that fast in old iOS devices and Android the async function should be removed.
There are also two additional functions to start getting new measurements as soon as possible. Those two methods could be a noop on OS X or devices without the proper sensor or just call the callback periodically with a zero vector.
On f9fb5a4 I've implemented this basic API in iOS and OS X.