Issue with DEVICEMOTION

Hi, I am using DEVICEMOTION with v2.0.10 CC project. I use the following code, however the node I am trying to move with devicemotion is unresponsive when tested on a mobile device with tilt.

cc.systemEvent.setAccelerometerEnabled(true);
cc.systemEvent.on(cc.SystemEvent.EventType.DEVICEMOTION, this.onDeviceMotionEvent, this);

 onDeviceMotionEvent: function (event) {
        event.acc.x = this.ballAccel.x;
        event.acc.y = this.ballAccel.y; 
        cc.log(event.acc.x + "   " + event.acc.y);
    },

Even though event.acc.x and .y do not equal 0, the code does not work correctly.

You can refer to the example cases which have been created by new a project, click Examples tab . After creation, the DeviceMotion example is under asset->cases->03_gameplay->01_player_control->DeviceMotion

Actually you should adjust the target movement on the update function.

I have the movement adjusted in the update function with the code

update (dt) {
        this.node.x += this.ballAccel.y * dt * this.speed;
        this.node.y += this.ballAccel.x * dt * this.speed;
    },

Where this.ballAccel is a cc.v2, and this.speed is just a single integer. The ball node moves, but is not taking devicemotion input. I’ve tried to just do
this.node.x += this.ballAccel.y;, however the issue persists.

Have you checked the example I list in the below.

Yes. I’ve tested this on multiple mobile devices and have gotten it to work in the past, so I’m perplexed why it doesn’t now. I also tested the example’s DeviceMotionCtrl code in both v2.0.10 and v1.10 which I migrated from earlier.

So I believe I found the issue. DEVICEMOTION events require the user to be prompted for permission to use their device’s accelerometer in iOS 13 and later. I’m using a version that was released before then. Hopefully, newer 2.x versions of Cocos Creator that were released after iOS 13 support doing this natively. V2.2 was the first of these it seems, although I’m not entirely sure.

Upgraded my project to v2.2. My question is, however, how do I implement the devicemotion permissions? I don’t see any way to do this outlined in the documentation for Cocos Creator. When I run my scene in the browser, the console simply says that devicemotion permission has been denied without any dialogue box or button appearing to request it. Using the code cc.SystemEvent.EventType.DEVICEMOTION.requestPermission(); only yields a type error. Would you know how to accomplish this?

Two factors:
1.some browsers maybe dont support
2.the phone cant support the function

Yeah I don’t know at this point if DeviceMotion events can be used still on Cocos Creator v2.x. DeviceMotionEvent.requestPermission is not supported, so you’ll get an error message

I see. Maybe the v8 update from 1.x to 2.x while the support for DEVICEMOTION in the V8 engine has also changed.

Have you tried the newest Version of Cocos?

It looks like you’re logging the accelerometer data, but you’re not actually updating the node’s position in the onDeviceMotionEvent function. To move the node, you need to modify its position based on the accelerometer values, like this:

javascript

CopyEdit

onDeviceMotionEvent: function (event) {
    this.node.x += event.acc.x * this.speed; // Adjust 'this.speed' as needed
    this.node.y += event.acc.y * this.speed; // Adjust 'this.speed' as needed
    cc.log(event.acc.x + "   " + event.acc.y);
},

This should update the node’s position based on the device’s tilt. Let me know if that helps!