Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for motion sensors #3

Open
Sumolari opened this issue Apr 14, 2016 · 0 comments
Open

Add wrapper for motion sensors #3

Sumolari opened this issue Apr 14, 2016 · 0 comments

Comments

@Sumolari
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant