Accelerometer data lagging

I’m sorry if there’re is already topic about this (I didn’t find one). I’m experiencing some “lag” when using accelerometer in cocos2dx v3.2. The data coming from the queue seems to slow down progressively with the time. Is there something I’m doing wrong?:

Device::setAccelerometerEnabled(true);
auto accListener = EventListenerAcceleration::create(CC_CALLBACK_2(GameScene::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(accListener, this);

I’m getting the data with the event dispatcher. Is there a way I can get the current acceleration values and skip the queued ones?

I also tried to set the time interval with:

Device::setAccelerometerInterval(1/60);

but no luck there either

As i dig deeper in this, seems like the block approach (CCDevice.mm:89) of getting the acceleration data from CMMotionManager is adding overhead. Apple suggests polling the data periodically by reading CMMotionManager’s accelerometerData.

If anyone is having the same problem and needs a quick solution, I’ve made a quick patch JUST WORKAROUND, NOT A FIX:

That’s on cocos2d/cocos/platform/ios/CCDevice.mm

I think the clean solution would be to add a way to get the data from the cocos2d API. ( getAccelerometerData() ? ) The queued data may still used since it may have other purposes. But in my case I need it realtime. If anyone have nicer solution/workaround please write it here.

4 Likes

Hi! I’m using your solution on my game. It worked very well. Why do you say it’s a hack that needs to be rewritten? Will I have future problems by using it?

Thanks!

disclaimer: my code was perfectly fine on cocos2dx 2.2, but accelerometer wasn’t working at all on arm64 devices (iPhone 5s and iPhone 6), so I ported everything to cocos2d-x 3.2, and for my surprise, bam!, lot’s of problems on all devices :confused: But thanks again for your solution, it helped a lot!

Hi, if you are using the data to move some object you should have no problems with that code.

I commented it as a workaround and not a fix because it’s getting the data in real time and will drop some changes, however for gaming purposes I don’t see why anyone would need the data cached. So this may be considered a fix. Maybe someone from the cocos team can comment on this?

1 Like

Awesome thanks for the fix!

Wow, thanks a lot! Hack or not, that solved my problem perfectly.

On a related note, does anybody else get a memory error after each call to onAcceleration? Probably has something to do with the Acceleration and Event objects passed to the callback function.

cocos2d: [memory] CORRUPTION: Attempting to free (N7cocos2d3RefE) with invalid ref tracking record.

Hi,

I am also facing the same problem and tried your solution, but i wont work for me. I copied and replaced the code in CCDevice.mm like below

- (void)accelerometer:(CMAccelerometerData *)accelerometerData
{
_acceleration->x = _motionManager.accelerometerData.acceleration.x;
_acceleration->y = _motionManager.accelerometerData.acceleration.y;
_acceleration->z = _motionManager.accelerometerData.acceleration.z;
_acceleration->timestamp = _motionManager.accelerometerData.timestamp;

can you please tell me where i made mistake.

I added same work around but had issue where accelerometer values will spike randomly. I found a better fix by doing below and works awesome for me.

Device::setAccelerometerInterval(1.0 / 60);

I also noticed someone tried Device::setAccelerometerInterval(1/60); but didn’t work. The reason is 1/60 == 0 :smile:

This workaround indeed works and is a life saver. Thanks a lot!

I’ve just submitted a cocos2d-x issue for this bug, with a link to dimi_t_d’s fix.

Interestingly, this bug is still present in cocos2d-x version 3.4 (at the time of this post) and probably present in the current cocos2d-x 4 development branch as well (I just skimmed the code to see if the accelerometer handling had changed).

I’m not sure why dimi_t_d calls it an ugly hack. It looks like a perfectly acceptable and clean solution to me.

Hi,
As of today, the bug is still there in the latest cocos2d-x V3 branch.
Does all the cocos2d-x games on iOS that uses the accelerometer implements the workaround ?
Perhaps it is time to update the sources accordingly.